发射爱心代码Python
时间: 2023-11-20 11:59:32 浏览: 196
以下是使用Python画出小人发射爱心的代码:
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('pink')
# 画小人
def draw_person():
# 画头
turtle.penup()
turtle.goto(-50, 100)
turtle.pendown()
turtle.circle(50)
# 画身体
turtle.penup()
turtle.goto(-50, 50)
turtle.pendown()
turtle.forward(100)
# 画左腿
turtle.penup()
turtle.goto(-50, 50)
turtle.pendown()
turtle.left(45)
turtle.forward(50)
# 画右腿
turtle.penup()
turtle.goto(50, 50)
turtle.pendown()
turtle.right(90)
turtle.forward(50)
# 画左手
turtle.penup()
turtle.goto(-50, 0)
turtle.pendown()
turtle.left(45)
turtle.forward(50)
# 画右手
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
turtle.right(90)
turtle.forward(50)
# 画爱心
def draw_heart():
# 画左半边心
turtle.penup()
turtle.goto(-200, -100)
turtle.pendown()
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
# 画右半边心
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
# 发射爱心
def launch_heart():
# 小人移动到发射位置
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
# 小人跳跃
for i in range(3):
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.right(30)
turtle.forward(50)
turtle.left(60)
turtle.forward(50)
turtle.right(30)
# 爱心发射
turtle.penup()
turtle.goto(-150, 200)
turtle.pendown()
turtle.color('red')
turtle.pensize(5)
turtle.right(150)
turtle.forward(250)
# 调用函数画小人和爱心
draw_person()
draw_heart()
launch_heart()
# 隐藏画笔
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
阅读全文