大家好,我是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()