帮我写python烟花代码
时间: 2023-03-06 07:43:38 浏览: 42
我可以给你一些参考代码:import turtle
import randomcolors = ["red", "blue", "green", "orange", "yellow", "purple"]t = turtle.Pen()
turtle.bgcolor("black")
t.speed(0)for x in range(50):
t.pencolor(random.choice(colors))
size = random.randint(10, 40)
x = random.randrange(-turtle.window_width()//2,
turtle.window_width()//2)
y = random.randrange(-turtle.window_height()//2,
turtle.window_height()//2)
t.penup()
t.setpos(x, y)
t.pendown()
for i in range(size):
t.forward(i*2)
t.left(91)
相关问题
b帮我写一个炫酷的Python烟花代码
好的,以下是一个简单的Python烟花代码,它使用了turtle模块和random模块来创建随机的烟花效果:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(width=600, height=600)
turtle.bgcolor("black")
# 创建烟花函数
def firework(x, y):
# 设置画笔颜色和形状
turtle.color(random.choice(["red", "blue", "green", "yellow", "white"]))
turtle.shape("circle")
turtle.shapesize(0.5)
turtle.penup()
# 设置起始位置和速度
turtle.goto(x, y)
turtle.speed(0)
# 绘制烟花
for i in range(50):
turtle.pendown()
turtle.forward(i*2)
turtle.right(144)
turtle.penup()
# 隐藏画笔
turtle.hideturtle()
# 绑定鼠标左键点击事件
turtle.onscreenclick(firework)
# 运行主循环
turtle.mainloop()
```
当你运行这个代码时,你可以点击鼠标左键来创建随机颜色和形状的烟花效果。希望你喜欢这个简单的烟花代码!
写段python烟花代码
当然,这个问题可以直接回答的。以下是一个简单的Python烟花代码,供参考:
```
import random
import time
import turtle
# 创建画布
turtle.speed("fastest")
turtle.color("white")
turtle.bgcolor("black")
# 绘制火花
def draw_star(x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(5):
turtle.forward(size)
turtle.right(144)
turtle.forward(size)
turtle.left(72)
# 绘制烟花
def draw_firework():
# 随机生成位置、颜色和大小
x = random.randint(-300, 300)
y = random.randint(-200, 200)
color = random.choice(["red", "blue", "yellow", "green", "purple", "white", "orange"])
size = random.randint(10, 30)
# 绘制烟花
for i in range(30):
draw_star(x, y, size)
turtle.color(color)
size -= 1
time.sleep(0.05)
turtle.clear()
# 循环绘制烟花
while True:
draw_firework()
```
希望这个简单的Python烟花代码可以满足你的需求!
阅读全文