我要精美炫酷的圣诞树代码编程python
时间: 2023-12-18 17:30:19 浏览: 132
python圣诞树编写实例详解
以下是一个用Python编写的精美炫酷的圣诞树代码示例:
```python
import turtle
import random
# 设置画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
# 绘制圣诞树
def draw_tree(branch_len, pen_size):
if branch_len < 5:
return
else:
pen.pensize(pen_size)
pen.forward(branch_len)
pen.left(30)
draw_tree(branch_len - random.randint(5, 15), pen_size - 1)
pen.right(60)
draw_tree(branch_len - random.randint(5, 15), pen_size - 1)
pen.left(30)
pen.backward(branch_len)
# 绘制星星
def draw_star():
pen.color("gold")
pen.penup()
pen.goto(random.randint(-200, 200), random.randint(100, 200))
pen.pendown()
pen.begin_fill()
for _ in range(5):
pen.forward(25)
pen.right(144)
pen.end_fill()
# 绘制圣诞树装饰
def draw_decorations():
colors = ["red", "green", "blue", "yellow", "purple", "orange"]
for _ in range(50):
pen.penup()
pen.goto(random.randint(-200, 200), random.randint(-200, 0))
pen.pendown()
pen.color(random.choice(colors))
pen.begin_fill()
pen.circle(random.randint(1, 5))
pen.end_fill()
# 主函数
def main():
pen.penup()
pen.goto(0, -200)
pen.pendown()
pen.left(90)
draw_tree(100, 10)
draw_star()
draw_decorations()
turtle.done()
# 调用主函数
main()
```
阅读全文