烟花python圣诞树
时间: 2023-12-15 15:32:48 浏览: 37
以下是使用Python绘制烟花和圣诞树的代码示例:
1. 绘制烟花
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('black')
# 定义烟花函数
def draw_fireworks(x, y):
# 设置画笔颜色和大小
turtle.pencolor(random.choice(['red', 'yellow', 'blue', 'green', 'purple', 'white', 'orange']))
turtle.pensize(3)
# 画烟花
for i in range(30):
turtle.fd(30)
turtle.rt(150)
turtle.fd(30)
turtle.rt(210)
# 画烟花爆炸后的星星
turtle.pencolor(random.choice(['red', 'yellow', 'blue', 'green', 'purple', 'white', 'orange']))
turtle.pensize(5)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(12):
turtle.fd(20)
turtle.rt(150)
turtle.fd(20)
turtle.rt(210)
# 点击鼠标左键画烟花
turtle.onscreenclick(draw_fireworks, 1)
# 隐藏画笔
turtle.hideturtle()
# 运行画布
turtle.done()
```
2. 绘制圣诞树
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('black')
# 定义圣诞树函数
def draw_tree(x, y):
# 设置画笔颜色和大小
turtle.pencolor('green')
turtle.pensize(3)
# 画圣诞树
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fd(100)
turtle.rt(120)
turtle.fd(100)
turtle.rt(120)
turtle.fd(100)
turtle.rt(120)
turtle.penup()
turtle.goto(x, y + 100)
turtle.pendown()
turtle.circle(30)
turtle.penup()
turtle.goto(x - 20, y + 70)
turtle.pendown()
turtle.circle(10)
turtle.penup()
turtle.goto(x + 20, y + 70)
turtle.pendown()
turtle.circle(10)
# 点击鼠标左键画圣诞树
turtle.onscreenclick(draw_tree, 1)
# 隐藏画笔
turtle.hideturtle()
# 运行画布
turtle.done()
```