1. 创建画布和画笔
初始化turtle画布并设置画笔属性:
# 创建画布和画笔
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#f8f5e9")
screen.title("中秋月饼 - Python turtle绘制")
pen = turtle.Turtle()
pen.speed(0) # 最快速度
pen.hideturtle()
使用turtle库绘制中秋月饼图案
中秋节是中国的传统节日,月饼是中秋节的象征。使用Python的turtle库绘制月饼图案不仅有趣,还能帮助你:
在开始之前,请确保你已安装Python(推荐Python 3.6+)。turtle库是Python标准库的一部分,无需额外安装。
我们将使用以下Python模块:
import turtle import math
初始化turtle画布并设置画笔属性:
# 创建画布和画笔
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#f8f5e9")
screen.title("中秋月饼 - Python turtle绘制")
pen = turtle.Turtle()
pen.speed(0) # 最快速度
pen.hideturtle()
绘制一个金黄色的圆形作为月饼主体:
# 绘制月饼主体
def draw_mooncake_body(x, y, radius, color):
pen.penup()
pen.goto(x, y - radius)
pen.pendown()
pen.fillcolor(color)
pen.begin_fill()
pen.circle(radius)
pen.end_fill()
# 调用函数绘制月饼主体
draw_mooncake_body(0, 0, 180, "#e6b325") # 金黄色
在月饼边缘添加传统花纹装饰:
# 绘制月饼花纹
def draw_patterns(x, y, radius):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color("#b34700") # 深棕色
# 绘制外圈花纹
pen.width(3)
for _ in range(36):
pen.forward(radius * 0.85)
pen.backward(radius * 0.85)
pen.left(10)
# 绘制内圈花纹
pen.width(2)
for _ in range(24):
pen.circle(radius * 0.65, 30)
pen.dot(10, "#b34700")
# 调用函数添加花纹
draw_patterns(0, 0, 180)
在月饼中心添加"中秋"文字:
# 添加文字
def add_text(x, y, text, size, color):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color(color)
pen.write(text, align="center", font=("楷体", size, "bold"))
# 添加"中秋"文字
add_text(0, -20, "中\n秋", 48, "#b30000")
以下是绘制月饼的完整Python代码:
import turtle
import math
# 创建画布和画笔
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#f8f5e9")
screen.title("中秋月饼 - Python turtle绘制")
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
# 绘制月饼主体
def draw_mooncake_body(x, y, radius, color):
pen.penup()
pen.goto(x, y - radius)
pen.pendown()
pen.fillcolor(color)
pen.begin_fill()
pen.circle(radius)
pen.end_fill()
# 绘制月饼花纹
def draw_patterns(x, y, radius):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color("#b34700")
# 外圈花纹
pen.width(3)
for _ in range(36):
pen.forward(radius * 0.85)
pen.backward(radius * 0.85)
pen.left(10)
# 内圈花纹
pen.width(2)
for _ in range(24):
pen.circle(radius * 0.65, 30)
pen.dot(10, "#b34700")
# 添加文字
def add_text(x, y, text, size, color):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color(color)
pen.write(text, align="center", font=("楷体", size, "bold"))
# 绘制月饼
draw_mooncake_body(0, 0, 180, "#e6b325")
draw_patterns(0, 0, 180)
add_text(0, -20, "中\n秋", 48, "#b30000")
# 完成绘制
turtle.done()
运行上述代码后,你将看到类似下面的月饼图案:
本文由JiaZong于2025-07-19发表在吾爱品聚,如有疑问,请联系我们。
本文链接:https://www.521pj.cn/20256031.html
发表评论