Drawing a Mid‑Autumn Festival Mooncake with Python Turtle
This tutorial demonstrates how to use Python's turtle graphics library to draw a festive mooncake for the Mid‑Autumn Festival, explaining common turtle parameters, providing a complete code example, and showing the resulting image.
This article introduces the Python turtle library and explains several frequently used functions such as setup , begin_fill , end_fill , goto , bk , fd , circle , seth , left , right , penup , pendown , pencolor , pensize , colormode , and done . A table lists each function with a brief Chinese description.
Parameter
Description
turtle.setup(width,height,startx,starty)
Starting coordinates: top‑left corner relative to the screen, default is screen centre
turtle.begin_fill()
Prepare to fill a shape
turtle.end_fill()
Complete the fill
turtle.goto(x,y)
Move the turtle to an absolute coordinate
turtle.bk(d)
Move the turtle backward by distance d
turtle.fd(d)
Move the turtle forward by distance d
turtle.circle(r,angle)
Draw a circle with radius r (optional angle)
turtle.seth(angle)
Set the turtle’s heading to an absolute angle
turtle.left(angle)
Turn left by angle degrees
turtle.right(angle)
Turn right by angle degrees
penup()
Lift the pen
pendown()
Put the pen down
pencolor()
Set pen colour
pensize()
Set pen thickness
turtle.colormode(mode)
Change RGB mode; mode=1.0 for decimal, mode=255 for integer values (e.g., white = 255,255,255)
turtle.done()
Keep the drawing window open after completion
The full script below creates a class MidAutumnFestival that draws a mooncake, its decorative edge, and a festive message using the turtle graphics commands described above.
<code>import turtle
class MidAutumnFestival(object):
def __init__(self):
self.turtle = turtle.Pen()
self.turtle.speed(10)
def __del__(self):
turtle.done() # Used to keep the disappearance of the screen at the end
self.turtle.down()
def goto(self, x, y):
self.turtle.penup()
self.turtle.goto(x, y)
self.turtle.pendown()
def circular(self):
self.turtle.color("#D1C185", "#839F26")
self.goto(0, -200)
self.turtle.begin_fill()
self.turtle.circle(200)
self.turtle.end_fill()
def color_edge(self):
self.goto(0, 0)
self.turtle.color("#FFA100")
for _ in range(20):
self.turtle.right(18)
self.turtle.begin_fill()
self.turtle.forward(220)
self.turtle.circle(40, 180)
self.turtle.goto(0, 0)
self.turtle.right(180)
self.turtle.end_fill()
def in_chart(self):
self.turtle.color('#D1C185')
self.goto(0, -25)
for _ in range(12):
self.turtle.begin_fill()
self.turtle.circle(150, 60)
self.turtle.left(90)
self.turtle.circle(150, 60)
self.turtle.end_fill()
def wirte_words(self, words):
self.goto(-40, 10)
self.turtle.color("red")
self.turtle.write(words, font=("Time", 23, "bold"))
self.turtle.pendown()
def run(self):
self.color_edge()
self.circular()
self.in_chart()
self.wirte_words("祝大家中秋节快乐!")
if __name__ =='__main__':
maf = MidAutumnFestival()
maf.run()
</code>Running the script produces the illustrated mooncake image shown in the article, and the author wishes everyone a happy Mid‑Autumn Festival.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.