pycharm元旦烟花代码
时间: 2023-12-31 22:24:53 浏览: 87
Pycharm学习教程(3) 代码运行调试
以下是使用PyCharm编写的元旦烟花代码示例:
```python
import turtle
import random
# 设置窗口大小和标题
window = turtle.Screen()
window.setup(width=800, height=600)
window.title("元旦烟花")
# 定义烟花的颜色和数量
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
num_fireworks = 10
# 定义烟花的形状
firework_shapes = [
((0, 0), (0, 10), (0, 20), (0, 30), (0, 40), (0, 50), (0, 60), (0, 70), (0, 80), (0, 90), (0, 100)),
((0, 0), (10, 0), (20, 0), (30, 0), (40, 0), (50, 0), (60, 0), (70, 0), (80, 0), (90, 0), (100, 0)),
((0, 0), (-10, 0), (-20, 0), (-30, 0), (-40, 0), (-50, 0), (-60, 0), (-70, 0), (-80, 0), (-90, 0), (-100, 0)),
((0, 0), (0, -10), (0, -20), (0, -30), (0, -40), (0, -50), (0, -60), (0, -70), (0, -80), (0, -90), (0, -100)),
((0, 0), (10, 10), (20, 20), (30, 30), (40, 40), (50, 50), (60, 60), (70, 70), (80, 80), (90, 90), (100, 100)),
((0, 0), (-10, -10), (-20, -20), (-30, -30), (-40, -40), (-50, -50), (-60, -60), (-70, -70), (-80, -80), (-90, -90), (-100, -100))
]
# 注册烟花的形状
for shape in firework_shapes:
turtle.register_shape(shape)
# 定义烟花类
class Firework(turtle.Turtle):
def __init__(self, color):
turtle.Turtle.__init__(self)
self.shape("circle")
self.color(color)
self.penup()
self.speed(0)
self.goto(random.randint(-300, 300), random.randint(-200, 200))
self.setheading(random.randint(0, 360))
self.shapesize(stretch_wid=0.5, stretch_len=1)
def update(self):
self.forward(2)
# 创建烟花并添加到列表中
fireworks = []
for _ in range(num_fireworks):
color = random.choice(colors)
firework = Firework(color)
fireworks.append(firework)
# 动画循环
while True:
for firework in fireworks:
firework.update()
# 相关问题:
阅读全文