2021-02-15

import turtle#海龟库
import time
import random

#定义上下左右按键内容
def up():
b.setheading(90)#按X-Y坐标算,转90°
b.forward(30)#30个像素
def down():
b.setheading(270)
b.forward(30)
def left():
b.setheading(180)
b.forward(30)
def right():
b.setheading(0)
b.forward(30)

#定义屏幕
playground = turtle.Screen()#先定义playground为屏幕
playground.size=turtle.screensize(canvwidth=1920,canvheight=1080,bg=“yellow”)
#英文可不写,注意格式,长宽背景色
playground.register_shape(“a.gif”)
playground.register_shape(“b.gif”)
playground.onkey(up,“Up”)
playground.onkey(down,“Down”)
playground.onkey(left,“Left”)
playground.onkey(right,“Right”)
playground.listen()#监听海龟

h=turtle.Turtle()
h.color(“pink”)
h.penup()
h.home()
h.write(“Helloworld”,align =‘center’,font=(“Comic Sans MS”,50,“bold”))
#居中,粗体
h.goto(0,-50)
h.write(“START”,align =‘center’,
font=(“Comic Sans MS”,30,“bold”))
time.sleep(2)

h.clear()

a=turtle.Turtle()
a.shape(“a.gif”)
a.penup()
a.goto(random.randint(-200,200),random.randint(-200,200))
a.pendown()
a.pensize(2)
a.color(“pink”)

b=turtle.Turtle()
b.shape(“b.gif”)
b.penup()
b.goto(random.randint(-200,200),random.randint(-200,200))
b.speed(0)

start= time.time()
while True:
a.setheading(a.towards(b))
a.forward(5)
if a.distance(b) < 10:
playground.clear()
b.goto(-30,-100)
a.goto(0,0)
end=time.time()
b.write(“GAME OVER”,align=‘center’,font=(“Comic Sans MS”,50))
a.write(“你活了{:.1f}秒”.format(end-start),align=‘center’,font=(“Comic Sans MS”,50))
time.sleep(2)
break

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值