圣诞节快到了,用python画一棵圣诞树送给你



圣诞节马上就要到了,好多小伙伴已经开始用scratch炫耀自己制作的圣诞树了。今天给大家分享通过Pythoy用Turtle库来画圣诞树,如果你有兴趣不妨一起试试,下面是代码:


import turtle
screen = turtle.Screen()
screen.setup(800, 900)
#红色圆乌龟
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
#绿色正方形乌龟
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
#圆移到树顶利用图章画一个圆
circle.goto(0, 280)
circle.stamp()
#树冠部分
k = 0
for i in range(1, 13):
    y = 30 * i
    for j in range(i - k):
        x = 30 * j
        square.goto(x, -y + 280)
        square.stamp()
        square.goto(-x, -y + 280)
        square.stamp()
    if i % 4 == 0: #两端红色圆
        x = 30 * (j + 1)
        circle.color('red')
        circle.goto(-x, -y + 280)
        circle.stamp()
        circle.goto(x, -y + 280)
        circle.stamp()
        k += 3
    if i % 4 == 3:#两端黄色圆
        x = 30 * (j + 1)
        circle.color('yellow')
        circle.goto(-x, -y + 280)
        circle.stamp()
        circle.goto(x, -y + 280)
        circle.stamp()
#圣诞树底部树干
square.color('brown')
for i in range(13, 17):
    y = 30 * i
    for j in range(2):
        x = 30 * j
        square.goto(x, -y + 280)
        square.stamp()
        square.goto(-x, -y + 280)
        square.stamp()


文章内容属作者个人观点,不代表本站立场,如有侵权立删。

   口袋儿题库-青少儿编程自测题库