写一个3d粒子圣诞树的python代码
时间: 2023-12-18 18:30:13 浏览: 516
以下是一个简单的3D粒子圣诞树的Python代码示例:
```python
import turtle
from random import randint
# 设置画布和画笔
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("3D Particle Christmas Tree")
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
# 定义绘制粒子的函数
def draw_particle():
x = randint(-200, 200)
y = randint(-200, 200)
size = randint(1, 5)
pen.goto(x, y)
pen.pendown()
pen.begin_fill()
pen.circle(size)
pen.end_fill()
pen.penup()
# 绘制圣诞树
def draw_tree():
pen.goto(0, -200)
pen.pendown()
pen.color("green")
pen.begin_fill()
pen.goto(-50, 0)
pen.goto(50, 0)
pen.goto(0, -200)
pen.end_fill()
pen.penup()
# 绘制雪花
def draw_snow():
pen.color("white")
pen.pensize(2)
for i in range(80):
x = randint(-248, 248)
y = randint(-100, 248)
pen.goto(x, y)
petal_number = 6
snow_size = randint(2, 10)
for j in range(petal_number):
pen.pendown()
pen.forward(snow_size)
pen.backward(snow_size)
pen.right(360 / petal_number)
pen.penup()
# 主函数
def main():
draw_tree()
for _ in range(100):
draw_particle()
draw_snow()
turtle.done()
# 调用主函数
main()
```
阅读全文