生日蛋糕和小猪佩奇搭配 Python乌龟(turtle库)
前言
画画这辈子是不可能画画的,看到很多视频平板画,咱用来用乌龟库来试试,论坛很多大佬用这个花了很多奇特的东西,咋也花点时间搞搞Turtle是什么?
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
turtle名称含义为“海龟”,我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形。
海龟的运动是由程序控制的,它可以变换颜色,改变大小(宽度)等
内容方面
废话不多说,主函数代码如下:
from turtle import*
import drawbirth
drawbirth.initial_background()
pencolor("white")
drawbirth.cake() # 蛋糕画线填充
drawbirth.ranges() # 随机彩点
drawbirth.texts() # 文字
ts = getscreen()
ts.getcanvas().postscript(file="ts1.eps")
drawbirth.setting() # 画布设置
drawbirth.brandon()
tss = getscreen()
tss.getcanvas().postscript(file="tss1.eps")
done()
对进行初始设置,定义划线函数
比较基本的一些准备
def initial_background():
# 设置背景颜色,窗口位置以及大小
bgcolor("#d3dae8")
setup(1000, 800) # set(width, height):输入宽和高为整数时,表示像素;为小数时,表示占据电脑屏幕的比例
speed(500)
penup() # 画笔抬起,移动时不绘制图形
goto(150, 0) # 将画笔移动到坐标为(x,y)的位置
pendown()
def draw_y(period, angle):
radian = math.radians(angle)
return period * math.sin(radian)
def draw_x(period, angle):
# 定义x,y的划线函数
radian = math.radians(angle)
return period * math.cos(radian)
画蛋糕
画蛋糕步骤比较多,主要控制划线想X,Y(数学真的很重要QAQ!!)
def cake():
for i in range(360):
x = draw_x(150, i)
y = draw_y(60, i)
goto(x, y)
fillcolor("#fef5f7")
end_fill()
begin_fill()
for i in range(180):
x = draw_x(150, -i)
y = draw_y(70, -i)
goto(x, y)
for i in range(180, 360):
x = draw_x(150, i)
y = draw_y(60, i)
goto(x, y)
fillcolor("#f2d7dd")
end_fill()
pu()
goto(120, 0)
pd()
begin_fill()
for i in range(360):
x = draw_x(120, i)
y = draw_y(48, i)
goto(x, y)
fillcolor("#cbd9f9")
end_fill()
begin_fill()
pencolor("#fee48c")
for i in range(540):
x = draw_x(120, i)
y = draw_y(48, i) + 70
goto(x, y)
goto(-120, 0)
fillcolor("#cbd9f9")
end_fill()
pu()
goto(120, 70)
pd()
pencolor("#fff0f3")
begin_fill()
for i in range(360):
x = draw_x(120, i)
y = draw_y(48, i) + 70
goto(x, y)
fillcolor("#fff0f3")
end_fill()
pu()
goto(110, 70)
pd()
pencolor("#fff9fb")
begin_fill()
for i in range(360):
x = draw_x(110, i)
y = draw_y(44, i) + 70
goto(x, y)
fillcolor("#fff9fb")
end_fill()
pu()
goto(120, 0)
pd()
begin_fill()
pencolor("#ffa79d")
for i in range(180):
x = draw_x(120, -i)
y = draw_y(48, -i) + 10
goto(x, y)
goto(-120, 0)
for i in range(180, 360):
x = draw_x(120, i)
y = draw_y(48, i)
goto(x, y)
fillcolor("#ffa79d")
end_fill()
pu()
goto(120, 70)
pd()
begin_fill()
pensize(4)
pencolor("#fff0f3")
for i in range(1800):
x = draw_x(120, 0.1 * i)
y = draw_y(-18, i) + 10
goto(x, y)
goto(-120, 70)
pensize(1)
for i in range(180, 360):
x = draw_x(120, i)
y = draw_y(48, i) + 70
goto(x, y)
fillcolor("#fff0f3")
end_fill()
pu()
goto(80, 70)
pd()
begin_fill()
pencolor("#6f3732")
goto(80, 120)
for i in range(180):
x = draw_x(80, i)
y = draw_y