python做一个圣诞树。
时间: 2023-03-26 07:01:16 浏览: 60
可以使用turtle库来绘制一个圣诞树,代码如下:
import turtle
def draw_tree(t, trunk_length, height):
if height <= :
return
t.forward(trunk_length)
t.right(30)
draw_tree(t, trunk_length*.8, height-1)
t.left(60)
draw_tree(t, trunk_length*.8, height-1)
t.right(30)
t.backward(trunk_length)
def draw_star(t, size):
t.color("yellow")
t.begin_fill()
for _ in range(5):
t.forward(size)
t.right(144)
t.end_fill()
def main():
t = turtle.Turtle()
t.speed()
t.left(90)
t.penup()
t.backward(200)
t.pendown()
draw_tree(t, 100, 5)
t.penup()
t.forward(50)
t.right(90)
t.forward(50)
t.left(90)
t.pendown()
draw_star(t, 30)
turtle.done()
if __name__ == "__main__":
main()
阅读全文