利用python如何实现猫捉老鼠小游戏
(编辑:jimmy 日期: 2024/12/31 浏览:3 次 )
python实现猫捉老鼠小游戏
首界面
开始游戏界面
然后键盘操作小老鼠上下左右移动,猫自己去追,当猫追上老鼠则游戏结束
这里用时3.2秒,最后将游戏时长与猫和老鼠都显示在主页面上
下面我把猫与老鼠的小图片也放上来供大家使用
以下是程序源代码
import turtle import time import random def up(): jerry.setheading(90) jerry.forward(20) def down(): jerry.setheading(270) jerry.forward(20) def left(): jerry.setheading(180) jerry.forward(20) def right(): jerry.setheading(0) jerry.forward(20) playground=turtle.Screen() playground.register_shape('tom.gif') playground.register_shape('jerry.gif') playground.onkey(up,'Up') playground.onkey(down,'Down') playground.onkey(left,'Left') playground.onkey(right,'Right') #监听 playground.listen() writer=turtle.Turtle() writer.color('brown') writer.hideturtle() writer.penup() writer.home() writer.write("Tom & JERRY",align='center',font=("Comic sans MS",50,"bold")) writer.goto(0,-50) writer.write("READY",align='center',font=("Comic sans MS",20,"bold")) time.sleep(3) writer.clear() tom=turtle.Turtle() tom.shape('tom.gif') tom.penup() tom.goto(random.randint(-200,200),random.randint(-200,200)) tom.pendown() tom.pensize(3) tom.color('blue') jerry=turtle.Turtle() jerry.shape('jerry.gif') jerry.speed(0) jerry.penup() jerry.goto(random.randint(-200,200),random.randint(-200,200)) jerry.color('brown') start=time.time() while True: tom.setheading(tom.towards(jerry)) tom.forward(5) if tom.distance(jerry)<10: end=time.time() playground.clear() jerry.goto(0,0) jerry.write("GAME OVER",align='center',font=("Comic sans MS",50,"bold")) jerry.goto(0,-50) jerry.write("YOU SURVIVED {:.1f} SECONDS".format(end-start),align='center',font=("Comic sans MS",20,"bold")) tom.pu() tom.goto(-50,-70) tom.stamp() jerry.pu() jerry.goto(50,-70) jerry.stamp() break
总结
下一篇:Python爬虫之Selenium警告框(弹窗)处理