python生日快乐烟花代码
时间: 2023-10-21 22:06:58 浏览: 672
以下是Python实现生日快乐烟花的代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('black')
# 定义烟花函数
def firework():
# 设置画笔颜色和大小
turtle.pencolor(random.choice(['red', 'yellow', 'blue', 'white', 'green', 'purple', 'orange']))
turtle.pensize(3)
# 随机生成烟花起点和终点坐标
x = random.randint(-350, 350)
y = random.randint(-250, 250)
end_x = random.randint(-50, 50)
end_y = random.randint(200, 400)
# 移动画笔到起点
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
# 绘制烟花
turtle.speed(0)
turtle.goto(end_x, end_y)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.goto(end_x, end_y)
# 循环绘制烟花
while True:
firework()
# 点击关闭窗口
turtle.exitonclick()
```
阅读全文