Python's built-in turtle module enables developers to create animated graphical content through simple command sequences. This guide demonstrates generating Lunar New Year greetings using turtle's capabilities, including geometric character rendering, text composition, and interactive animations.
Geometric Character Rendering
The following implementation creates a traditional "Fu" character on red paper with shadow effects. Key operations include background setup, shape drawing, and text placement using coordinate transformations.
import turtle
turtle.bgcolor("#ffffff")
turtle.speed(7)
# Create shadow effect
turtle.color("#404040")
side_length = 100
diagonal = 2 ** 0.5 * side_length
turtle.penup()
turtle.goto(-210, 180)
turtle.forward(side_length)
turtle.pendown()
turtle.begin_fill()
turtle.left(135)
turtle.forward(diagonal)
turtle.left(90)
turtle.forward(diagonal)
turtle.left(90)
turtle.forward(diagonal)
turtle.left(90)
turtle.forward(diagonal)
turtle.end_fill()
# Draw red paper
turtle.color("#ea182a")
turtle.penup()
turtle.home()
turtle.goto(-205, 180)
turtle.forward(side_length)
turtle.pendown()
turtle.begin_fill()
turtle.left(135)
turtle.forward(diagonal)
turtle.left(90)
turtle.forward(diagonal)
turtle.left(90)
turtle.forward(diagonal)
turtle.left(90)
turtle.forward(diagonal)
turtle.end_fill()
# Place character
turtle.color("black")
turtle.penup()
turtle.goto(-210, 175)
turtle.setheading(-90)
turtle.forward(50)
turtle.pendown()
turtle.write("Fu", align="center", font=("SimHei", 90, "bold"))
turtle.hideturtle()
turtle.done()
Text Composition for Seasonal Greetings
This section demonstrates multi-column text rendering with custom spacing and styling. The implemantation uses systematic coordinate adjustments to position verses and signatures in traditional Chinese format.
import turtle
turtle.bgcolor("#b1352b")
turtle.speed(0)
turtle.pensize(1)
turtle.pencolor("#f2ea99")
font_name = "SimHei"
font_size = 35
verse_lines = [
"Blasts of firecrackers mark the old year's end",
"Warm spring breezes enter the wine cup",
"Thousands of doors greet the bright morning sun",
"New peach wood charms replace the old"
]
signature = ["Song of Wang Anshi, Yuanri", "Happy New Year!"]
column_width = 100
# Draw vertical dividers
x_pos = 245
y_pos = 230
for _ in range(6):
turtle.penup()
turtle.goto(x_pos, y_pos)
turtle.pendown()
turtle.setheading(-90)
turtle.forward(460)
x_pos -= column_width
# Render verses
turtle.speed(5)
x_pos = 200
y_pos = 160
for line in verse_lines:
for char in line:
turtle.penup()
turtle.goto(x_pos, y_pos)
turtle.pendown()
turtle.write(char, align="center", font=(font_name, font_size))
y_pos -= 60
x_pos -= column_width
y_pos = 160
# Add signature
x_pos -= 80
y_pos = 100
for line in signature:
for char in line:
turtle.penup()
turtle.goto(x_pos, y_pos)
turtle.pendown()
turtle.write(char, align="center", font=(font_name, 20))
y_pos -= 30
x_pos -= 40
y_pos = 100
turtle.done()
Interactive Fireworks Animasion
This implementation creates mouse-triggered fireworks using turtle's animation control features. The solution manages particle effects through coordinate tracking and progressive rendering with undo operations.
import turtle
import time
import random
from turtle import *
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']
turtle.tracer(False)
turtle.hideturtle()
screen = turtle.getscreen()
def generate_firework(particle_count, distance, angle):
for _ in range(particle_count):
turtle.forward(distance)
turtle.left(angle)
def trigger_firework(x, y):
firework_specs = []
color_choice = random.choice(colors)
size = random.randint(50, 80)
rotation = 171 if size <= 60 else random.choice([174, 175, 176])
step_increment = (size - 30) / 10
particle_amount = int(360 / (180 - rotation))
firework_specs.append([x, y, color_choice, size, rotation, step_increment, particle_amount])
render_firework_sequence(firework_specs)
def render_firework_sequence(fireworks):
turtle.pensize(2)
for frame in range(5):
for spec in fireworks:
x, y, color, size, rotation, step, particles = spec
turtle.penup()
turtle.goto(x - frame * step / 2, y)
turtle.pendown()
turtle.color('#b1352b', color)
turtle.begin_fill()
turtle.setheading(0)
generate_firework(particles, frame * step + 30, rotation)
turtle.end_fill()
turtle.update()
time.sleep(0.015)
turtle.tracer(particles * 2 + 8, 1)
for _ in range(particles * 2 + 8):
turtle.undo()
turtle.hideturtle()
turtle.pensize(5)
for frame in range(10):
for spec in fireworks:
x, y, color, size, rotation, step, particles = spec
particle_count = int(particles / 4)
turtle.penup()
turtle.goto(x - step * 5 + 10, y)
turtle.pendown()
turtle.setheading(-90)
radius = size / 2 - 10
turtle.penup()
turtle.left(90)
turtle.backward(2 * frame)
turtle.right(90)
for _ in range(particle_count):
turtle.penup()
turtle.pencolor(color)
turtle.circle(radius + frame * 2, 360 / particle_count - 1)
turtle.pendown()
turtle.circle(radius + frame * 2, 1)
turtle.update()
time.sleep(0.03)
turtle.tracer(particle_count * 4, 1)
for _ in range(particle_count * 4):
turtle.undo()
screen.onclick(trigger_firework)
turtle.done()