Whenever a friend has a birthday, a birthday cake is naturally indispensable. Today we will take a look at how to draw a birthday cake with Python.
The Python libraries we use in this article include turtle, math, and random.
The main code implemented is as follows:
import math as m
import random as r
import turtle as t
t.speed(0)
t.delay(0)
# 设置背景颜色及窗口
t.bgcolor("#FFFFFF")
t.setup(800, 600)
t.penup()
t.goto(150, 0)
t.pendown()
t.pencolor("white")
t.begin_fill()
for i in range(360):
x = drawX(150, i)
y = drawY(60, i)
t.goto(x, y)
t.fillcolor("#fef5f7")
t.end_fill()
t.begin_fill()
for i in range(180):
x = drawX(150, -i)
y = drawY(70, -i)
t.goto(x, y)
for i in range(180, 360):
x = drawX(150, i)
y = drawY(60, i)
t.goto(x, y)
t.fillcolor("#f2d7dd")
t.end_fill()
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
for i in range(360):
x = drawX(120, i)
y = drawY(48, i)
t.goto(x, y)
t.fillcolor("#33CCFF")
t.end_fill()
t.begin_fill()
t.pencolor("#fee48c")
for i in range(540):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.goto(-120, 0)
t.fillcolor("#99FFFF")
t.end_fill()
t.pu()
t.goto(120, 70)
t.pd()
t.pencolor("#fff0f3")
t.begin_fill()
for i in range(360):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
t.pu()
t.goto(110, 70)
t.pd()
t.pencolor("#fff9fb")
t.begin_fill()
for i in range(360):
x = drawX(110, i)
y = drawY(44, i) + 70
t.goto(x, y)
t.fillcolor("#FFCCCC")
t.end_fill()
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
t.pencolor("#ffa79d")
for i in range(180):
x = drawX(120, -i)
y = drawY(48, -i) + 10
t.goto(x, y)
t.goto(-120, 0)
for i in range(180, 360):
x = drawX(120, i)
y = drawY(48, i)
t.goto(x, y)
t.fillcolor("#ffa79d")
t.end_fill()
for i in range(50):
t.pu()
x = r.randint(-500, 500)
y = r.randint(120, 300)
t.goto(x, y)
t.pd()
t.dot(r.randint(3, 5),
color[r.randint(0, 7)])
t.penup()
t.goto(-130, 230)
t.pencolor("#FF0000")
t.write("Happy Birthday",
font=("Curlz MT", 30))
t.hideturtle()
t.done()
The effect is as follows:
The source code is obtained by replying to the py cake in the background of the public account Python Xiaoer~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。