用python写一个生日快乐的动画
时间: 2023-06-12 21:05:25 浏览: 398
好的,我可以帮您写一个简单的生日快乐动画,使用Python turtle模块。
下面是代码:
```python
import turtle
import time
# 设置画笔和窗口大小
turtle.setup(800, 600)
turtle.pensize(10)
# 绘制生日快乐字体
turtle.color("red")
turtle.penup()
turtle.goto(-200, 0)
turtle.pendown()
turtle.write("Happy Birthday!", font=("Arial", 40, "bold"))
# 绘制蛋糕
turtle.color("pink")
turtle.penup()
turtle.goto(-150, -100)
turtle.pendown()
turtle.begin_fill()
turtle.forward(300)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.forward(300)
turtle.left(90)
turtle.forward(200)
turtle.end_fill()
# 绘制蜡烛
turtle.color("yellow")
turtle.penup()
turtle.goto(100, 100)
turtle.pendown()
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 绘制火焰
turtle.color("red")
turtle.penup()
turtle.goto(100, 120)
turtle.pendown()
turtle.pensize(5)
turtle.circle(10)
# 点燃火焰
for i in range(10):
turtle.pensize(i + 1)
turtle.color("yellow")
turtle.circle(10 + i)
# 祝福语
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()
turtle.color("purple")
turtle.write("Best wishes for you!", font=("Arial", 30, "bold"))
turtle.hideturtle()
time.sleep(5) # 显示 5 秒钟
```
这段代码会绘制一个生日蛋糕,并在上面点燃一根蜡烛。当蜡烛点燃时,火焰会逐渐变大。最后,程序会在画布上显示祝福语,持续5秒钟。
您可以运行这段代码,然后在终端或命令行中输入 `python birthday_animation.py` 来运行它。
阅读全文