圣诞树代码pyhton飘雪花
时间: 2023-12-25 08:30:05 浏览: 92
以下是一个用Python绘制圣诞树并飘雪花的代码示例:
```python
import turtle
import random
# 绘制圣诞树
def draw_tree():
height = 5
stars = 1
for i in range(height):
print((' ' * (height - i)) + ('*' * stars))
stars += 2
# 飘雪花
def snowfall():
turtle.speed(0)
turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.color("white")
turtle.shape("turtle")
turtle.shapesize(0.5)
for _ in range(100):
x = random.randint(-400, 400)
y = random.randint(-300, 300)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.forward(10)
turtle.backward(10)
turtle.left(90)
turtle.forward(10)
turtle.backward(10)
turtle.right(90)
# 主函数
def main():
draw_tree()
snowfall()
turtle.done()
# 调用主函数
main()
```
阅读全文