用Python绘制月饼前,首先祝大家中秋节快乐,在月圆之夜,有一种古老的传统让我们停下脚步,围在一起,共享美食和欢乐。是的,我们正在谈论中秋节。这个节日,就像一颗闪烁的星星,点亮了我们的天空,让我们的心灵充满了温暖和爱,在这个节日里,月饼是不可或缺的一部分。月饼,不仅仅是一种食物,它更是中秋节的象征,是我们对家人深深思念的象征。而今天,我们要用Python编程语言来绘制出我们自己的月饼,在开始之前,让我们利用Python的基础绘图库——turtle模块,它可以帮助我们轻松地绘制出各种形状和图像,包括我们即将要绘制的月饼,我们直接看代码吧,
import turtle as t
import turtle
import random
import time
t = t.Pen()
#配速
t.speed(0)
#标题
t.screen.title('中秋快乐')
#背景
def funa(a, b,color):
turtle.bgcolor(f'pink')
t.speed(0)
t.penup()
t.goto(a,b)
t.pendown()
t.color(f'{color}')
t.begin_fill()
t.left(45)
t.fd(10)
t.circle(5,180)
t.right(90)
t.circle(5,180)
t.fd(10)
t.end_fill()
t.hideturtle()
for i in range(100):
r = random.randint(0, 255)
g = random.randint(0, 255)
d = random.randint(0, 255)
color = "#{:02x}{:02x}{:02x}".format(r, g,d)
a = random.randint(-500,500)
b = random.randint(-1000,1000)
funa(a, b,color)
#月饼大圆外圈实心
def outfill_flower(flower_num,flower_color):
for i in range(flower_num):
t.pensize(20)
t.left(i * (360 // flower_num))
t.color(flower_color)
t.penup()
t.forward(200)
t.pendown()
t.fillcolor(flower_color)
t.begin_fill()
t.circle(60)
t.end_fill()
t.penup()
t.home()
#月饼大圆外圈空心
def out_line_flower(flower_num, flower_color):
for i in range(flower_num):
t.pensize(10)
t.left(i * (360 // 18))
t.color(flower_color)
t.penup()
t.forward(192)
t.pendown()
t.circle(60)
t.penup()
t.home()
# 画内测的大圆
def big_circle(circle_color, circle_fill_color, circle_size):
t.goto(circle_size, 0)
t.left(90)
t.pendown()
t.pensize(8)
t.color(circle_color)
t.fillcolor(circle_fill_color)
t.begin_fill()
t.circle(circle_size)
t.end_fill()
t.penup()
t.home()
# 饼上印花文字内容
def write_font(text_content, text_color, size):
t.color(text_color)
for x in text_content:
t.penup()
t.goto(text_content[x])
t.write(x, align='center', font=('simhei', size, 'bold'))
t.penup()
t.home()
t.color('#F29407')
# 饼身中间矩形条纹部分
def body_center_line(width, height):
t.penup()
t.home()
t.pensize(4)
t.pendown()
t.backward(width / 2)
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height * 2)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)
t.penup()
t.home()
# 矩形条纹两侧的四个花纹
def center_flower(start_point, start_angle, angle_direction_change,
rectangle_height, circle_direction):
t.penup()
t.goto(start_point)
t.pendown()
t.setheading(start_angle)
t.forward(10)
t.setheading(angle_direction_change)
t.forward(20)
t.backward(rectangle_height * 2)
t.forward(rectangle_height * 2)
t.setheading(start_angle)
t.circle(circle_direction * 70, 90)
t.setheading(start_angle + 180)
t.forward(60)
t.setheading(angle_direction_change)
t.forward(30)
t.penup()
t.home()
t.penup()
t.goto(400,124)
t.pendown()
# 饼身上下左右的花纹
def out_flower(begin_x, begin_y, start_angle):
t.penup()
t.goto(begin_x, begin_y)
t.pendown()
t.setheading(start_angle)
t.forward(20)
t.right(90)
t.circle(-100, 20)
t.penup()
t.goto(begin_x, begin_y)
t.pendown()
t.setheading(start_angle)
t.right(90)
t.circle(-100, 30)
t.left(90)
t.forward(45)
t.left(95)
t.circle(190, 50)
t.left(95)
t.forward(45)
t.left(90)
t.circle(-100, 31)
t.setheading(start_angle)
t.forward(20)
t.left(90)
t.circle(100, 20)
t.penup()
t.home()
#祝福语
def zhufu ():
list =[['月', '满', '中', '秋', ',', '情', '深', '意', '浓'],['愿', '你', '的', '生', '活', '如', '同', '这', '良', '宵', '美', '景'],['欢', '乐', '无', '比', ',', '祝', '你', '的', '事', '业', '一', '帆', '风', '顺'],['鹏', '程', '万', '里', ',', '在', '这', '个', '温', '馨', '的', '夜', '晚'],['让', '明', '月', '传', '递', '我', '们', '的', '思', '念'],['愿', '你', '与', '家', '人', '共', '度', '一', '个', '愉', '快', '的', '中', '秋', '佳', '节'],['幸', '福', '满', '满', '!']]
def wenzi(text, i, j, color):
time.sleep(0.1)
t.penup()
t.goto(x, y)
t.pendown()
t.color(color)
t.write(text, font=('楷体', 9, 'bold'))
y= -400
for i in range(len(list)):
x = -500
y-=55
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = "#{:02x}{:02x}{:02x}".format(r, g, b)
for j in range(len(list[i])):
if i < len(list) and j < len(list[i]):
wenzi(list[i][j], x, y, color)
x+=60
'''
#这段代码可加可不加
#色彩闪光屏
def bengdi():
while True:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = "#{:02x}{:02x}{:02x}".format(r, g, b)
turtle.bgcolor(f'{color}')
time.sleep(0.05)
'''
# 主程序
def main():
outfill_flower(18, '#F29407')
out_line_flower(18, '#FFDEAD')
big_circle('#FFDEAD', '#F8B51D', 200)
body_center_line(12, 80)
out_flower(6, 110, 90)
out_flower(-110, 6, 180)
out_flower(-6, -110, 270)
out_flower(110, -6, 360)
t.speed(1)
text_content2 = {'中': (-95, 60), '秋': (95, 60), '乐': (100, -133), '快': (-100, -133)}
write_font(text_content2, '#FCE6C9',10)
time.sleep(1)
t.speed (5)
text="祝大家中秋节快乐"
t.penup ()
t.goto(120,240)
t.pendown()
for i in text:
t.write(i,align="center",font=("黑体",11,"normal"))
t.right(360/len(text))
t.penup()
t.forward(233)
t.hideturtle()
zhufu()
t.hideturtle()
#bengdi()
turtle.done()
if __name__ == "__main__":
main()
效果如下:
以上是适应手机的效果,如果是使用电脑的话,可自行更改参数以及坐标
散会