python炫酷烟花表白源代码
时间: 2023-05-04 13:04:54 浏览: 170
Python炫酷烟花表白程序的源代码如下:
```
import random
import time
import turtle
def love():
t = turtle.Turtle()
t.hideturtle()
t.getscreen().bgcolor('#000')
colors = ['#ff1e1e', '#ff8e1e', '#ffff1e', '#4cff1e', '#1effff', '#941eff', '#ff1e81']
hearts = []
for i in range(20):
hearts.append(make_heart())
while True:
for h in hearts:
t.penup()
x, y = h[0], h[1]
t.goto(x, y)
t.pendown()
t.pencolor(random.choice(colors))
t.begin_fill()
for i in range(3):
t.left(120)
t.forward(80)
t.end_fill()
t.penup()
t.clear()
time.sleep(0.2)
def make_heart():
a = random.uniform(-200, 200)
b = random.uniform(-200, 200)
return [a, b]
if __name__ == '__main__':
love()
```
在这个程序中,我们使用了turtle库和random库来生成烟花和颜色。通过make_heart函数生成随机坐标,在while True循环中使用for循环来遍历所有的心形图案并使用随机选择的颜色在屏幕上绘制真实的爱心。每次绘制前要清空屏幕,并且使用time库让程序休息一会来达到动画的效果。在这个程序中,每种颜色、每次运行都有可能是不同的,使得表白更加独特和多变。