Python|Small game of cat and mouse! ! !

Recently I have been idle (dao) with nothing (fei) to do (qi), and I like to study some small games. In this article, I mainly introduce a very simple cat and mouse game written using turtle, which is mainly controlled by the mouse. The mouse (Tom) moves to avoid being chased by the computer-controlled cat (Jerry).

The main body of the game thinks about the logic: enter a number from 1 to 5 to select the difficulty, use the direction keyboard to control the direction and movement of the mouse, so that it does not get caught by the cat. The game ends when it is caught by the cat, and the score will pop up at the end.

1. Import relevant modules

import time
import turtle

2. Create the overall game window

window = turtle.Screen()

 3. Draw cat and mouse

# 猫 系统默认的黑色
cat = turtle.Turtle()
# 老鼠 红色
mouse = turtle.Turtle()
mouse.color('red')
# 将老鼠的画笔提起,使不会出现轨迹
mouse.penup()    
# 老鼠的初始地址放置在坐标(150,150)               
mouse.goto(150, 150)

 4. Control the tiger's rotation direction and movement through the direction keyboard

# 老鼠前进 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")

 5. Set a boundary to prevent the mouse from leaving the game window

# 窗口可用范围为 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()

 6. Manually end the game and difficulty control

# 结束游戏
def quitTurtle():
    window.bye()
# 空格键结束游戏
window.onkeypress(quitTurtle, "Escape")

# 产生一个输入难度的对话框
difficulty = window.numinput("难度", "请输入游戏的难度(1~5)", minval=1, maxval=5)
# 窗体监听按键
window.listen()

 7.The game starts

# 是否被抓住:默认未被抓住
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))

 8.End game and score

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()

 9. Complete code

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()

The above is a simple cat-and-mouse game. The total code is only more than 60 lines. If you are interested, you can try (wan) and (yi) try (xia).

Guess you like

Origin blog.csdn.net/m0_59595915/article/details/132639019