目录
前言
来啦来啦来啦,小伙伴们快快来领取七彩花瓣雨吧!!
小海龟
老生常谈啦,在用python画樱花树前,我们先来了解一下turtle吧!
小海龟(Turtle)是Python中画图的一个重要的包(内置包),里面包含丰富的画图工具以及画图的各种功能,当你学会了用Turtle画图后,你可以画任何你想画的图案哦。
1.1 Turtle画板
Turtle的画板大小可以用turtle.setup()函数来设置
turtle.setup(width,height)
设置画板的大小,包含宽和高,width为宽,height为高。
1.2 Turtle画笔
Turtle的画笔有几个常用的函数:
1.3 Turtle画图
在画图的过程中,我们经常要使用一些简单的移动函数:
1.4 Turtle填色
在画好图后,我们经常需要对其进行填色,这里可以用turtle.fillcolor()函数,括号里写入你想填充的颜色即可。
在使用turtle.fillcolor()函数要注意其基本格式:
1.5 Turtle写字
在完成整个画图后,我们可以使用turtle.write()函数进行写字
turtle.write(" ",move,align,font)
花朵类
class Flower(): #每个花朵(花朵类)
def __init__(self):
self.r = ra.randint(8,12) #花朵的半径
self.x = ra.randint(-1000,1000) #花朵的横坐标
self.y = ra.randint(-500,500) #花朵的纵坐标
self.f = ra.uniform(-3.14,3.14) #花朵左右移动呈正弦函数
self.speed = ra.randint(5,10) #花朵移动速度
self.color = ra.choice(colors) #花朵的颜色
self.outline = 1 #花朵的外框大小(可不要)
移动函数
def move(self): #花朵移动函数
if self.y >= -500: #当花朵还在画布中时
self.y -= self.speed #设置上下移动速度
self.x += self.speed * math.sin(self.f) #设置左右移动速度
self.f += 0.1 #可以理解成标志,改变左右移动的方向
else: #当花朵漂出了画布时,重新生成一个花朵
self.r = ra.randint(8,12)
self.x = ra.randint(-1000,1000)
self.y = 500
self.f = ra.uniform(-3.14,3.14)
self.speed = ra.randint(5,10)
self.color = ra.choice(colors)
self.outline = 1
画花朵
def draw(self): #画花朵函数,就是用turtle画花朵
t.penup()
t.goto(self.x,self.y)
t.setheading(self.x)
t.pendown()
t.left(36)
t.color(self.color)
t.begin_fill()
t.fillcolor(self.color)
for i in range(5):
t.left(-72)
t.circle(self.r,extent=144)
t.end_fill()
#t.right(36)
#t.begin_fill()
#t.fillcolor("red")
#t.color("white")
#t.circle(12)
#t.end_fill()
尾声
快要到六一啦,小伙伴们准备怎么过呢?