python画生日蛋糕代码
时间: 2023-11-16 12:57:56 浏览: 141
Python生日蛋糕代码
5星 · 资源好评率100%
以下是一个简单的 Python 生日蛋糕绘制代码,使用了 turtle 库:
```python
import turtle
# 绘制蛋糕底部
def draw_cake_bottom():
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.begin_fill()
turtle.color("pink")
turtle.circle(200)
turtle.end_fill()
# 绘制蛋糕上面的奶油
def draw_cream():
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.begin_fill()
turtle.color("white")
turtle.circle(150)
turtle.end_fill()
# 绘制蜡烛
def draw_candle(num):
turtle.penup()
turtle.goto(0, 150)
turtle.pendown()
for i in range(num):
turtle.color("yellow")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.forward(50)
turtle.pendown()
# 绘制生日快乐字样
def draw_happy_birthday():
turtle.penup()
turtle.goto(-200, 250)
turtle.pendown()
turtle.color("red")
turtle.write("Happy Birthday!", font=("Arial", 36, "normal"))
# 主函数
def main():
turtle.speed(10)
draw_cake_bottom()
draw_cream()
draw_candle(5)
draw_happy_birthday()
turtle.done()
if __name__ == '__main__':
main()
```
你可以运行这个代码,看看它画出来的生日蛋糕长什么样子。如果你想要更多的细节和效果,可以自己尝试修改代码。
阅读全文