首页 > 编程语言 >python爱心泡泡

python爱心泡泡

时间:2022-11-21 10:35:10浏览次数:48  
标签:泡泡 turtle randint python self random colors 爱心 import

import turtle
import random
import math

# 初始化
turtle.setup(1280, 720)
t = turtle.Pen()
t.ht()

# 颜色
colors = []
t_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]

for i in t_list:
    t_str = "#ff00"
    for j in t_list:
        colors.append(t_str+i+j)


class Love():
    def __init__(self):
        # 定义变量
        self.r = random.randint(4, 10)
        self.x = random.randint(-900, 700)
        self.y = random.randint(-400, 400)
        self.i = random.randint(0, 10)
        self.color = random.choice(colors)
        self.speed = random.randint(1, 8)

    def move(self):
        # 通过y坐标来控制爱心
        if self.y <= 500:
            self.y += 2.5*self.speed
            self.x = self.x + 1.5*math.sin(self.i)*math.sqrt(self.i)*self.speed
            self.i = self.i + 0.1
        else:
            self.y = -700
            self.r = random.randint(5, 20)
            self.x = random.randint(-900, 700)
            self.i = 0
            self.color = random.choice(colors)
            self.speed = random.randint(1, 8)

    def draw(self):
        # 绘制爱心
        t.pensize(self.r/2)
        t.penup()
        t.color(self.color, self.color)
        t.goto(self.x, self.y)
        t.pendown()
        # 设置角度
        t.setheading(60)
        t.circle(self.r, 255)
        t.fd(2.4*self.r)
        t.left(90)
        t.fd(2.4*self.r)
        t.circle(self.r, 255)


love = []
for i in range(100):
    love.append(Love())
turtle.bgcolor("#000000")


while 1:
    turtle.tracer(0)
    t.clear()
    for i in range(80):
        love[i].move()
        love[i].draw()
    turtle.tracer(1)

  

标签:泡泡,turtle,randint,python,self,random,colors,爱心,import
From: https://www.cnblogs.com/jingzaixin/p/16910561.html

相关文章

  • Python基础语法
    注释#标注的文本数字整数intPython3开始不再区分long、int,long被重命名为int,所以只有int类型了进制表示:​ 十进制10​ 十六进制0xa​ 八进制0o10​ 二进制0b10......
  • Python数据的封装与解构
    Python等式右侧出现逗号分隔的多值的时候,就会将这几个值封装到元组中。这种操作称为封装packing。封装t1=1,2type(t1)Out[3]:tuplet1Out[4]:(1,2)封装和解构基......
  • python时间模块
    datetime模块时间高级类datetime.date:表示日期的类datetime.datetime:表示日期时间的类datetime.time:表示时间的类datetime.timedelta:表示时间间隔,即两个时间......
  • python部分内建函数
    标识id​ 返回对象的唯一标识,CPython返回内存地址哈希hash()​ 返回一个对象的哈希值类型type()​ 返回对象的类型类型转换​ float()--》浮点数​ int()-......
  • Python当中的pip常用的一些命令
    安装如果是需要自己另外安装pip包的,在已经配置好Python的环境当中运行下面这个命令行py-mensurepip--upgrade升级要是你觉得自己的pip版本有点低,想要升级一下的话......
  • python 图片点击左键标注序号
    importcv2#引用opencvimportnumpyasnp#图片路径img=cv2.imread('Images\CAD2.png')a=[]b=[]counts=0#生成序号方法deffun():globalcounts#添加......
  • python except 自动无交互登录
    python+expect实现脚本自动登录远程服务器起因最近在折腾Ubuntu系统,有时候需要连接远程服务器,使用ssh命令连接较为复杂,需要记住每台机器的ip和密码,当然Ubuntu(lin......
  • 6000字带你初识Python面向对象
    ​......
  • 基于python中的静态方法,类方法以及类变量的用法
    基于python中的静态方法,类方法以及类变量的用法最近在做scada系统的自动化部署,采用python语言,在开发过程中用到了,静态方法,类方法,以及类变量,下面就这个特殊的用法做个......
  • python-opencv抓取RTMP
    opencv安装sudoapt-getinstallpython3-opencv源码安装https://blog.csdn.net/u011922698/article/details/123268143pip3installopencv-python#安装opencvpip3......