猫捉老鼠(python)解析

编程入门 行业动态 更新时间:2024-10-08 22:18:34

猫捉<a href=https://www.elefans.com/category/jswz/34/1763230.html style=老鼠(python)解析"/>

猫捉老鼠(python)解析

import turtle#海龟库
import time
import random

#定义上下左右按键内容,此处b为老鼠
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”)#将图片a.gif(猫)与py文件放在同一个文件夹
playground.register_shape(“b.gif”)
playground.onkey(up,“Up”)#定义up(上)的动作由“Up键”完成
playground.onkey(down,“Down”)
playground.onkey(left,“Left”)
playground.onkey(right,“Right”)
playground.listen()#监听海龟

h=turtle.Turtle()#定义海龟h
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

分享一些指令/解析


更多推荐

猫捉老鼠(python)解析

本文发布于:2024-03-13 17:40:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734486.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:老鼠   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!