Python turtle で誕生日ケーキ
3 段のケーキ、クリーム、火のついたろうそく、「お誕生日おめでとう!」。ろうそくを年の数にして、画像を送りましょう。

コード全体(59 行)
import turtle
import random
screen = turtle.Screen()
screen.bgcolor("#fff4e6")
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
def box(x, y, w, h, color):
# 塗りつぶした長方形(左下の角が (x, y))
t.penup()
t.goto(x, y)
t.setheading(0)
t.pendown()
t.color(color)
t.begin_fill()
for side in [w, h, w, h]:
t.forward(side)
t.left(90)
t.end_fill()
# お皿
box(-200, -170, 400, 12, "lightgray")
# 3 段、上の段ほど細く
layers = [("#f7c6d9", 320, 90), ("#fbe3a1", 240, 75), ("#c9e8f5", 160, 60)]
y = -158
for color, w, h in layers:
box(-w / 2, y, w, h, color)
# 各段の上にクリーム
t.penup()
for x in range(int(-w / 2) + 10, int(w / 2), 20):
t.goto(x, y + h)
t.dot(16, "white")
y += h
# ろうそくと炎
candles = 5
for i in range(candles):
x = -60 + i * 120 / (candles - 1) if candles > 1 else 0
box(x - 5, y, 10, 50, random.choice(["tomato", "deepskyblue", "gold", "orchid"]))
t.penup()
t.goto(x, y + 62)
t.dot(16, "orange")
t.goto(x, y + 64)
t.dot(8, "yellow")
# カラースプレー
t.penup()
for i in range(60):
t.goto(random.randint(-150, 150), random.randint(-150, 60))
t.dot(5, random.choice(["red", "green", "blue", "orange", "purple"]))
t.goto(0, 190)
t.color("#d6336c")
t.write("お誕生日おめでとう!", align="center", font=("Arial", 36, "bold"))どうやって描いているか
box(x, y, 幅, 高さ, 色) は塗りつぶした長方形を描きます。お皿も各段もろうそくもこの関数です。
段は (色, 幅, 高さ) のリストで、for color, w, h in layers で下から順に描き、y += h で次の段の高さを覚えます。
クリームは各段の上に 20 ごとに打つ白い点、炎は外がオレンジ、中が黄色の 2 つの点です。
-60 + i * 120 / (candles - 1) で、何本でもろうそくを等間隔に並べます。
よくある質問
ろうそく 12 本だと窮屈です
120 の幅に並べています。「編集」で 140 に広げ、-60 を -70 に変えてください。
名前を入れるには?
「編集」で write(...) の「お誕生日おめでとう!」を「さくらちゃん お誕生日おめでとう!」に変えます。
ハート半円 2 つでできたハート
バラ1 層ずつ回して重ねた花びら
花火1 コマずつ描くアニメーション
クリスマスツリー電飾、星、雪、メッセージ