最近闲(mang)来(dao)无(fei)事(qi),喜欢研究一些小游戏,本篇文章我主要介绍使用 turtle 写的一个很简单的猫捉老鼠的小游戏,主要是通过鼠标控制老鼠(Tom)的移动,躲避通过电脑控制的猫(Jerry)的追捕。
游戏主体思考逻辑:输入1-5的数字选择难度,使用方向键盘控制老鼠的方向及移动,让它不要被猫抓住,最后被猫抓住时游戏结束,结束时会弹出得分。
import time
import turtle
window = turtle.Screen()
# 猫 系统默认的黑色
cat = turtle.Turtle()
# 老鼠 红色
mouse = turtle.Turtle()
mouse.color('red')
# 将老鼠的画笔提起,使不会出现轨迹
mouse.penup()
# 老鼠的初始地址放置在坐标(150,150)
mouse.goto(150, 150)
# 老鼠前进 30 像素
def up():
mouse.forward(30)
# 老鼠左转 45 度
def left():
mouse.left(45)
# 老鼠右转 45 度
def right():
mouse.right(45)
# 老鼠后退 30 像素
def back():
mouse.backward(30)
# 按动方向键 Up 则执行函数up ...
window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
# 窗口可用范围为 300x300 像素
boxsize = 300
# 检测老鼠是否跑过界,如再前进便过界,让老鼠卡在边界处
def checkbound():
global boxsize
if mouse.xcor() > boxsize:
mouse.goto(boxsize, mouse.ycor())
if mouse.xcor() < -boxsize:
mouse.goto(-boxsize, mouse.ycor())
if mouse.ycor() > boxsize:
mouse.goto(mouse.xcor(), boxsize)
if mouse.ycor() < -boxsize:
mouse.goto(mouse.xcor(), -boxsize)
# 老鼠前进 30 像素
def up():
mouse.forward(30)
checkbound()
# 老鼠后退 30 像素
def back():
mouse.backward(30)
checkbound()
# 结束游戏
def quitTurtle():
window.bye()
# 空格键结束游戏
window.onkeypress(quitTurtle, "Escape")
# 产生一个输入难度的对话框
difficulty = window.numinput("难度", "请输入游戏的难度(1~5)", minval=1, maxval=5)
# 窗体监听按键
window.listen()
# 是否被抓住:默认未被抓住
caught = False
# 游戏开始
while not caught:
# 猫调整自己方向,使自己正对老鼠
cat.setheading(cat.towards(mouse))
# 猫前进
cat.forward(8+difficulty)
# 老鼠与猫的距离小于 5 个像素就输了
if cat.distance(mouse) < 5:
caught = True
# 难度越高,运行速度越快
time.sleep(0.2-(0.01*difficulty))
score = 0
# 游戏开始
while not caught:
# 猫调整自己方向,使自己正对老鼠
cat.setheading(cat.towards(mouse))
# 猫前进
cat.forward(8+difficulty)
# 分数增加
score += 1
# 老鼠与猫的距离小于 5 个像素就输了
if cat.distance(mouse) < 5:
caught = True
# 难度越高,运行速度越快
time.sleep(0.2-(0.01*difficulty))
# 游戏结束及得分
window.textinput("GAME OVER", "游戏得分:"+str(score*difficulty))
window.bye()
import time
import turtle
# 一些常量
# 窗口可用范围为 300x300 像素
boxsize = 300
# 是否被抓住:默认未被抓住
caught = False
score = 0
# 老鼠前进 30 像素
def up():
mouse.forward(30)
checkbound()
# 老鼠左转 45 度
def left():
mouse.left(45)
# 老鼠右转 45 度
def right():
mouse.right(45)
# 老鼠后退 30 像素
def back():
mouse.backward(30)
checkbound()
# 检测老鼠是否跑过界,如再前进便过界,让老鼠卡在边界处
def checkbound():
global boxsize
if mouse.xcor() > boxsize:
mouse.goto(boxsize, mouse.ycor())
if mouse.xcor() < -boxsize:
mouse.goto(-boxsize, mouse.ycor())
if mouse.ycor() > boxsize:
mouse.goto(mouse.xcor(), boxsize)
if mouse.ycor() < -boxsize:
mouse.goto(mouse.xcor(), -boxsize)
# 结束游戏
def quitTurtle():
window.bye()
window = turtle.Screen()
# 猫 系统默认的黑色
cat = turtle.Turtle()
# 老鼠 红色
mouse = turtle.Turtle()
mouse.color('red')
# 将老鼠的画笔提起,使不会出现轨迹
mouse.penup()
# 老鼠的初始地址放置在坐标(150,150)
mouse.goto(150, 150)
# 按动方向键 Up 则执行函数up ...
window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
# 空格键结束游戏
window.onkeypress(quitTurtle, "Escape")
# 产生一个输入难度的对话框
difficulty = window.numinput("难度", "请输入游戏的难度(1~5)", minval=1, maxval=5)
# 窗体监听按键
window.listen()
# 游戏开始
while not caught:
# 猫调整自己方向,使自己正对老鼠
cat.setheading(cat.towards(mouse))
# 猫前进
cat.forward(8+difficulty)
# 分数增加
score += 1
# 老鼠与猫的距离小于 5 个像素就输了
if cat.distance(mouse) < 5:
caught = True
# 难度越高,运行速度越快
time.sleep(0.2-(0.01*difficulty))
# 游戏结束及得分
window.textinput("GAME OVER", "游戏得分:"+str(score*difficulty))
window.bye()
以上就是简单的猫捉老鼠的小游戏,总计代码不过60多行,感兴趣的各位可以动手试(wan)一(yi)试(xia)。