如何实现上图效果呢?让我们开始吧!
首先,导入turtle和random
from turtle import *
import random as rd
然后,写一个待会儿要用到的函数,用于随机生成True和False
def true_or_false(percent=50):
n=rd.randint(1,100)
if n<=percent:
return True
else:
return False
然后初始化一下,设置颜色模式为255(RGB模式),设置画布,初始位置,绘图速度等
# initial
colormode(255)
screensize(600,600,"darkblue")
seth(90)
pu()
bk(180)
speed(0)
tracer(2)
一些常量
# constants
BROWN=160,82,45
TRUNK_LENGTH=350
TRUNK_PENSIZE=15
DECORATE_BALL_PENSIZE=4
DECORATE_COLORS=[
(200,0,0),
(220,220,0),
(255,97,3)
]
树干
# trunk
pu()
seth(90)
xx=0
goto(xx,-180)
pd()
pensize(TRUNK_PENSIZE)
pencolor(BROWN)
fd(TRUNK_LENGTH)
bk(TRUNK_LENGTH)
两边的叶子,左边的叶子朝向右边(随机度数),右边的叶子朝向左边(随机度数),这段代码不难理解
# leaf
pu()
x,y=pos()
for n in range(18): # left
seth(90)
if n==0:
fd(100)
else:
fd(20)
seth(180)
length=200-n*10
fd(length)
seth(0)
for i in range(length):
fd(1)
if i%6==0:
pd()
seth(rd.randint(45,75))
pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
pensize(10)
fd(30)
bk(30)
seth(0)
pu()
pu()
goto(x,y)
for n in range(19): # right
seth(90)
if n==0:
fd(100)
else:
fd(20)
seth(0)
length=200-n*10
fd(length)
seth(280)
for i in range(length):
fd(1)
if i%6==0:
pd()
seth(rd.randint(105,135))
pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
pensize(10)
fd(30)
bk(30)
seth(180)
pu()
伯利恒之星,一开始画位置不太对,经过多次调整后确定为如下代码
# star
pu()
goto(xx,pos()[1])
seth(180)
fd(20) # micro moving
seth(90)
fd(20) # micro moving
pd()
color((255,0,0))
seth(72)
begin_fill()
for i in range(5):
fd(80)
right(180-180/5)
end_fill()
一些装饰
# ball (decorate)
positions=[(-159,-55),(-91,14),(-83,-36),(85,-54),(-22,-40),(-3,34),(0,160),(-47,152),(19,215),(115,22),(68,107),(141,-48)]
pensize(DECORATE_BALL_PENSIZE)
for x,y in positions:
pu()
goto(x,y)
r=rd.randint(12,22)
seth(90)
fd(r)
seth(0)
fillcolor(rd.choice(DECORATE_COLORS))
pencolor(rd.choice(DECORATE_COLORS))
pd()
begin_fill()
circle(r)
end_fill()
DONE!!
done()
好啦,看一看全部代码吧!
from turtle import *
import random as rd
def true_or_false(percent=50):
n=rd.randint(1,100)
if n<=percent:
return True
else:
return False
# initial
colormode(255)
screensize(600,600,"darkblue")
seth(90)
pu()
bk(180)
speed(0)
tracer(2)
# constants
BROWN=160,82,45
TRUNK_LENGTH=350
TRUNK_PENSIZE=15
DECORATE_BALL_PENSIZE=4
DECORATE_COLORS=[
(200,0,0),
(220,220,0),
(255,97,3)
]
# trunk
pu()
seth(90)
xx=0
goto(xx,-180)
pd()
pensize(TRUNK_PENSIZE)
pencolor(BROWN)
fd(TRUNK_LENGTH)
bk(TRUNK_LENGTH)
# leaf
pu()
x,y=pos()
for n in range(18): # left
seth(90)
if n==0:
fd(100)
else:
fd(20)
seth(180)
length=200-n*10
fd(length)
seth(0)
for i in range(length):
fd(1)
if i%6==0:
pd()
seth(rd.randint(45,75))
pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
pensize(10)
fd(30)
bk(30)
seth(0)
pu()
pu()
goto(x,y)
for n in range(19): # right
seth(90)
if n==0:
fd(100)
else:
fd(20)
seth(0)
length=200-n*10
fd(length)
seth(280)
for i in range(length):
fd(1)
if i%6==0:
pd()
seth(rd.randint(105,135))
pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
pensize(10)
fd(30)
bk(30)
seth(180)
pu()
# star
pu()
goto(xx,pos()[1])
seth(180)
fd(20) # micro moving
seth(90)
fd(20) # micro moving
pd()
color((255,0,0))
seth(72)
begin_fill()
for i in range(5):
fd(80)
right(180-180/5)
end_fill()
# ball (decorate)
positions=[(-159,-55),(-91,14),(-83,-36),(85,-54),(-22,-40),(-3,34),(0,160),(-47,152),(19,215),(115,22),(68,107),(141,-48)]
pensize(DECORATE_BALL_PENSIZE)
for x,y in positions:
pu()
goto(x,y)
r=rd.randint(12,22)
seth(90)
fd(r)
seth(0)
fillcolor(rd.choice(DECORATE_COLORS))
pencolor(rd.choice(DECORATE_COLORS))
pd()
begin_fill()
circle(r)
end_fill()
done()
里面的参数可以自己修改,喜欢的话就来个3连吧~~~