来自一个程序员的浪漫,一个人的圣诞节

前进的一小步

今天也是我第一次使用CSDN上传自己的东西,除了激动还是激动,哈哈哈。

对未来的展望

我也希望在以后我会上传更多有意思的作品到CSDN上面来,我也会分享我的学习历程。希望能和CSDN上面的小伙伴一起成长,一起加油。

用python创作一颗圣诞树

这也是我在圣诞节的时候,一个人闲来无聊想做一点有意思的东西玩玩。

效果图

一个用python乌龟做圣诞树

核心代码

其主要是由这两种构成,希望大家可以依照我的核心代码,自己也去尝试一下。

我们可以根据图片发现,其实“圣诞树”就是由一个又一个的小块块组成的。(我自己代码中,每个小块间距是25)说到这里我相信大家应该对接下来如何去自己完成创造一个圣诞树,有了一定的思路了。

# 画红色圆
circle = turtle.Turtle()  #可以根据这个代码创造你想要的图形
circle.shape('circle')
circle.color('red')
circle.up()
circle.goto(0,255)
circle.stamp()

第二个关键的代码就是,我们应该怎样用小块块做出一个三角形。(应该我们可以从图中知道我们做出来的圣诞树都是三角形状的)
提醒一下:主要就是依靠for循环完成。
我希望大家能够独立思考完成。

下面我给出我创造三角形的一个思路

# 创建画板
import turtle
screen = turtle.Screen()
screen.setup(800,600)
def demo(number):         #传入第一行有几个绿色
    for i in range(4):      #几行
        square.goto(0,255)     #移动至第几行中间
        for left in range(i+1+int(number/2)):  #先从左边画
            square.goto((-25)*left,255-i*25)
            square.stamp()
        for right in range(i+1+int(number/2)):
            square.goto((25)*right,255-i*25)
            square.stamp()
# 画绿色方块
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.up()
square0 = turtle.Turtle()
height = 255
demo(1)
# 暂停,点击后退出
turtle.exitonclick()

源代码

以下我自己编写的源代码:

# 创建画板
import turtle
screen = turtle.Screen()
screen.setup(800,600)
def demo(number,h):         #传入第一行有几个绿色
    for i in range(4):      #几行
        square.goto(0,h)     #移动至第几行中间
        h=h-25 
        for left in range(i+1+int(number/2)):  #先从左边画
            square.goto((-25)*left,h)
            square.stamp()
            if i==2 and left ==i+int(number/2):
                circle0.goto((-25)*(left+1),h)
                circle0.stamp()
            elif i==3 and left ==i+int(number/2):
                circle.goto((-25)*(left+1),h)
                circle.stamp()
        for right in range(i+1+int(number/2)):
            square.goto((25)*right,h)
            square.stamp()
            if i==2 and right == i+int(number/2):
                circle0.goto((25)*(right+1),h)
                circle0.stamp()
            elif i==3 and right ==i+int(number/2):
                circle.goto((25)*(right+1),h)
                circle.stamp()
def demo2(number,h):
    for i in range(4):      #几行
        square0.goto(0,h)     #移动至第几行中间
        h=h-25 
        for left in range(int(number/2)):  #先从左边画
            square0.goto((-25)*left,h)
            square0.stamp()
        for right in range(int(number/2)):
            square0.goto((25)*right,h)
            square0.stamp()
# 画黄色圆
circle0 = turtle.Turtle()
circle0.shape('circle')
circle0.color('yellow')
circle0.up()
# 画红色圆
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.up()
circle.goto(0,255)
circle.stamp()
# 画绿色方块
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.up()
square0 = turtle.Turtle()
height = 255
for i in range(4):
    demo(4*i+1,height)
    height=height-4*25
# 画棕色块
square0.shape('square')
square0.color('brown')
square0.up()
square0.goto(0,height)
demo2(5,height)
# 暂停,点击后退出
turtle.exitonclick()

Thanks for reading!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值