python画生日蛋糕
时间: 2023-09-12 13:04:39 浏览: 119
好的,以下是一个使用 Python 的 turtle 模块画生日蛋糕的例子:
```python
import turtle
# 设置画笔
turtle.penup()
turtle.goto(-200, 0)
turtle.pendown()
turtle.pensize(5)
turtle.pencolor('red')
turtle.speed(4)
# 画蜡烛
for i in range(5):
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
turtle.penup()
turtle.forward(10)
turtle.pendown()
# 画火苗
turtle.fillcolor('orange')
turtle.begin_fill()
turtle.forward(10)
turtle.right(135)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.right(135)
turtle.forward(30)
turtle.right(135)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.right(135)
turtle.forward(10)
turtle.end_fill()
turtle.penup()
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(60)
turtle.right(180)
# 画蛋糕
turtle.penup()
turtle.goto(-100, -100)
turtle.pendown()
turtle.fillcolor('pink')
turtle.begin_fill()
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
# 画字
turtle.penup()
turtle.goto(-50, -50)
turtle.pendown()
turtle.pencolor('white')
turtle.write('Happy Birthday!', font=('Arial', 18, 'normal'))
# 隐藏画笔
turtle.hideturtle()
# 等待点击关闭窗口
turtle.exitonclick()
```
这段代码使用 turtle 模块画了一个生日蛋糕,包括蜡烛、火苗、蛋糕和生日快乐的字样。你可以在终端中运行这段代码,看看效果如何。
阅读全文