python新年快乐
时间: 2023-12-11 08:34:02 浏览: 218
很高兴为您解答。以下是一个使用Python实现烟花效果的例子,祝您新年快乐!
```python
import random
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('black')
# 定义烟花爆炸的函数
def explode(x, y):
# 定义烟花颜色和大小
colors = ['red', 'yellow', 'blue', 'green', 'purple', 'white', 'orange']
size = random.randint(10, 30)
# 绘制烟花
for i in range(50):
# 随机选择颜色和半径
color = random.choice(colors)
radius = random.randint(5, 15)
# 绘制圆形
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
# 随机选择角度和距离
angle = random.uniform(0, 360)
distance = random.randint(50, 150)
# 绘制线条
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(angle)
turtle.color(color)
turtle.pensize(3)
turtle.forward(distance)
# 绑定鼠标点击事件
turtle.onscreenclick(explode)
# 隐藏画笔
turtle.hideturtle()
# 显示画布
turtle.done()
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)