用python画猫和老鼠_一个Python游戏:猫和老鼠

点击上方关注订阅黑码教主获取更多精彩内容

写出一个猫和老鼠的游戏。游戏者使用方向键来控制老鼠,使其保持在猫的前方(由计算机控制猫)。保持时间越长,得分越高。

import turtle

import time

boxsize = 200

caught = False

score = 0

# functions that are called on keypresses

def up():

mouse.forward(10)

checkbound()

def left():

mouse.left(45)

def right():

mouse.right(45)

def back():

mouse.backward(10)

checkbound()

def quitTurtles():

window.bye()

# stop the mouse from leaving the square set by box size

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)

# set up screen

window = turtle.Screen()

mouse = turtle.Turtle()

cat = turtle.Turtle()

mouse.penup()

mouse.penup()

mouse.goto(100, 100)

# add key listeners

window.onkeypress(up, 'Up')

window.onkeypress(left, 'Left')

window.onkeypress(right, 'Right')

window.onkeypress(back, 'Down')

window.onkeypress(quitTurtles, 'Escape')

difficulty = window.numinput('Difficulty',

'Enter a difficulty from easy (1), for hard(5)',

minval=1, maxval=5)

window.listen()

# main loop

# note how it changes with difficulty

while not caught:

cat.setheading(cat.towards(mouse))

cat.forward(8 + difficulty)

score = score + 1

if cat.distance(mouse) < 5:

caught = True

time.sleep(0.2 - (0.01 * difficulty))

window.textinput('Game Over',

'Well done. You scored: ' +

str(score * difficulty))

window.bye()

如果你觉得本篇还不错,请点赞关注!

文章由黑码教主创作,配图源于网络版权归原作者所有,如有侵权联系删除!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
老鼠游戏是一种经典的童年游戏,也是一个很好的编程练习项目。下面是一个简单的Python老鼠游戏的实现步骤: 1. 导入必要的模块 ```python import random import os import time ``` 2. 定义游戏地图 ```python MAP_WIDTH = 40 MAP_HEIGHT = 20 map_data = [] for y in range(MAP_HEIGHT): row = [] for x in range(MAP_WIDTH): if x == 0 or x == MAP_WIDTH-1 or y == 0 or y == MAP_HEIGHT-1: row.append('#') else: row.append(' ') map_data.append(row) ``` 3. 定义老鼠的初始位置 ```python cat_x = random.randint(1, MAP_WIDTH-2) cat_y = random.randint(1, MAP_HEIGHT-2) mouse_x = random.randint(1, MAP_WIDTH-2) mouse_y = random.randint(1, MAP_HEIGHT-2) ``` 4. 游戏主循环 ```python while True: # 清屏 os.system('cls' if os.name == 'nt' else 'clear') # 更新地图 map_data[cat_y][cat_x] = 'C' map_data[mouse_y][mouse_x] = 'M' # 输出地图 for row in map_data: print(''.join(row)) # 判断胜负 if cat_x == mouse_x and cat_y == mouse_y: print('抓到了老鼠!') break # 的移动 if cat_x < mouse_x: cat_x += 1 elif cat_x > mouse_x: cat_x -= 1 if cat_y < mouse_y: cat_y += 1 elif cat_y > mouse_y: cat_y -= 1 # 老鼠的移动 mouse_dx = random.randint(-1, 1) mouse_dy = random.randint(-1, 1) if 0 < mouse_x + mouse_dx < MAP_WIDTH-1: mouse_x += mouse_dx if 0 < mouse_y + mouse_dy < MAP_HEIGHT-1: mouse_y += mouse_dy # 暂停一会儿 time.sleep(0.1) ``` 这样就完成了一个简单的Python老鼠游戏。你可以在此基础上进行更多的扩展,比如添加障碍物、增加多只老鼠等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值