python画圣诞树

大家好,我是python的小白,最近刚开始上手python的海龟画图!

首先得先安装海龟Turtle插件,然后给大家分享一波如何使用 Python 来画一颗简单易上手的圣诞

树和平安果,由最基本的图形(圆形和正方形)组合而成,其中在边角的地方设置不同的颜色圆形。此外在最底部还设置了字体Merry Christmas字样,并且带有雪花图案!


上图: 

 

 上代码:


import turtle as t
import random as r
screen=t.Screen()

screen.screensize(bg='black')
screen.setup(750,700)
circle = t.Turtle ()
circle.shape ('circle')
circle.color ('red')
circle.speed ('fastest')
circle.up ()

square = t.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=i*30
    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(3):
        x=30*j
        square.goto(x,-y+280)
        square.stamp()
        square.goto(-x,-y+280)
        square.stamp()


def drawsnow ():  # 定义画雪花的方法
    t.ht ()  # 隐藏笔头,ht=hideturtle
    t.pensize (2)  # 定义笔头大小
    for i in range (200):  # 画多少雪花
        t.pencolor ("white")  # 定义画笔颜色为白色,其实就是雪花为白色
        t.pu ()  # 提笔,pu=penup
        t.setx (r.randint (-350, 350))  # 定义x坐标,随机从-350到350之间选择
        t.sety (r.randint (-100, 350))  # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始
        t.pd ()  # 落笔,pd=pendown
        dens = 6  # 雪花瓣数设为6
        snowsize = r.randint (1, 10)  # 定义雪花大小
        for j in range (dens):  # 就是6,那就是画5次,也就是一个雪花五角星
            t.fd (int (snowsize))
            t.backward (int (snowsize))
            t.right (int (360 / dens))
drawsnow ()
t.done()

# 平安夜 送 平安果

上图:

上代码:


import turtle as t
screen=t.Screen()
screen.screensize(bg="white")
screen.setup(800,800)
t.speed(3)
t.pensize(3) 
        
t.goto(0,-200)    //以(0,-200)为圆心画半径150的红色圆
t.begin_fill()
t.color("red")
t.circle(radius=150)
t.end_fill()


t.color("brown")     //画果实的梗
t.up()
t.goto(-90,0)
t.pd()
t.circle(180,40)
t.seth(105)
t.up()
t.goto(-20,20)
t.pd()
t.circle(180,50)
t.pd()
 
t.begin_fill()      //左边的叶子
t.color("green")
t.circle(180,50)
t.seth(-30)
t.circle(180,55)
t.end_fill()

t.begin_fill()      //右边的叶子
t.seth(0)
t.circle(180,50)
t.seth(-180)
t.circle(180,50)
t.end_fill()

t.done()

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自由学者×

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值