import ybc_game
import pgzero
import math
from collections import deque
ybc_game.mobile_type(2)
ybc_game.size(800, 800)
ybc_game.title('逃离文字迷宫-跑酷游戏')
# ybc_game.play_sound('bgm', -1)
me = ybc_game.actor('board')
ghost = ybc_game.actor('board')
weapon = ybc_game.actor('board')
cube_size = 60
level = -1
# 0:空地,1:墙,2:门,3:我,4:鬼,5:符
walls_str = ['''
11111111111
13000000001
11111011111
00001010000
00001011111
00001000021
00001111111
''',
'''
11111111111
10000000001
10111110111
10010000001
11010111111
10010010121
10111010101
13010010101
11010110101
10010000101
10111011101
10000000041
11111111111
''',
'''
11111111
10000001
10111101
10011001
11011011
15011001
10111141
13111121
11111111
''',
'''
111111111111111111111
100013001000100010001
101010101010101010101
101000100010001000101
101111111111111111101
101000000000000000101
101111110000001110101
101000010111111000101
101011010100000011101
101012010110101000101
101010010000001110101
101011110110101000101
101000000111111101101
101111110000001100001
140000000000051101101
111111111111111111111
'''
]
walls = []
state = '准备'
time = 4
me.speed = 15
me.dir = ''
me.new_dir = ''
ghost.pause_time = 16
ghost.speed = 30
ghost.dir = ''
ghost.timer = ghost.pause_time
ghost.energy = 0
def start():
global state, time, walls, level
if time > 1:
time -= 1
ybc_game.delay(start, 1)
else:
level += 1
state = '运行'
walls = []
lines = walls_str[level].split()
ghost.enable = False
weapon.enable = False
weapon.follow_me = False
if level == 3:
ghost.pause_time = 8
for y in range(len(lines)):
walls.append([])
for x in range(len(lines[y])):
n = int(lines[y][x])
if n in (0, 1, 2):
walls[y].append(n)
else:
if n == 3:
me.x = x * cube_size
me.y = y * cube_size
me.dir = me.new_dir = ''
if n == 4:
ghost.x = x * cube_size
ghost.y = y * cube_size
ghost.enable = True
if n == 5:
weapon.x = x * cube_size
weapon.y = y * cube_size
weapon.enable = True
walls[y].append(0)
ybc_game.delay(start, 2)
def update():
if state == '运行':
move()
if weapon.enable and weapon.follow_me:
weapon.x = me.x
weapon.y = me.y
if ghost.enable:
ghost_move()
collide()
draw()
def draw():
screen.fill('black')
if state in ('运行'):
for i in range(len(walls)):
for j in range(len(walls[i])):
if walls[i][j] == 1:
if abs(j * cube_size - me.x) < 440 and abs(i * cube_size - me.y) < 440:
screen.draw.rect((j * cube_size + 402 - me.x - cube_size / 2,
i * cube_size + 402 - me.y - cube_size / 2, cube_size - 4, cube_size - 4),
'white')
ybc_game.text('墙', [j * cube_size + 400 - me.x, i * cube_size + 400 - me.y], 'white', 28)
if walls[i][j] == 2:
screen.draw.rect((j * cube_size + 402 - me.x - cube_size / 2,
i * cube_size + 402 - me.y - cube_size / 2, cube_size - 4, cube_size - 4),
'green')
ybc_game.text('门', [j * cube_size + 400 - me.x, i * cube_size + 400 - me.y], 'green', 28)
screen.draw.rect((402 - cube_size / 2, 402 - cube_size / 2, cube_size - 4, cube_size - 4), 'blue')
ybc_game.text('我', [400, 400], 'blue', 28)
if ghost.enable:
screen.draw.rect((ghost.x - me.x + 402 - cube_size / 2, ghost.y - me.y + 402 - cube_size / 2, cube_size - 4,
cube_size - 4), 'orange')
ybc_game.text('猫', [ghost.x - me.x + 400, ghost.y - me.y + 400], 'orange', 28)
if weapon.enable:
screen.draw.rect((weapon.x - me.x + 402 - cube_size / 2, weapon.y - me.y + 402 - cube_size / 2, cube_size - 4,
cube_size - 4), 'yellow')
ybc_game.text('盾', [weapon.x - me.x + 400, weapon.y - me.y + 400], 'yellow', 28)
if state == '胜利':
ybc_game.text('恭喜胜利', [400, 400], 'white', 50)
ybc_game.text('点个赞吧, 欢迎写信给我提供新关卡的创意', [400, 460], 'white', 20)
if state == '失败':
ybc_game.text('惨了,你被抓住了! 再试试吧', [400, 400], 'white', 50)
if state == '准备':
if time > 3:
ybc_game.text('点一下屏幕,然后用方向键控制', [400, 400], 'white', 50)
else:
ybc_game.text(str(time), [400, 400], 'white', 50)
def move():
# 根据new_dir判断是否要改变方向
if me.new_dir != '':
if can_move(me, me.new_dir):
me.dir = me.new_dir
me.new_dir = ''
# 根据dir判断是否可以移动
if me.dir != '':
if can_move(me, me.dir):
if me.dir == 'left':
me.x -= me.speed
if me.dir == 'right':
me.x += me.speed
if me.dir == 'up':
me.y -= me.speed
if me.dir == 'down':
me.y += me.speed
else:
me.dir = ''
ybc_game.play_sound('hit')
def ghost_move():
if ghost.dir in ['left', 'right', 'up', 'down']:
if can_move(ghost, ghost.dir) and ghost.energy > 0:
if ghost.dir == 'left':
ghost.x -= ghost.speed
if ghost.dir == 'right':
ghost.x += ghost.speed
if ghost.dir == 'up':
ghost.y -= ghost.speed
if ghost.dir == 'down':
ghost.y += ghost.speed
ghost.energy -= ghost.speed
else:
ghost.dir = ''
if ghost.dir == '' or ghost.energy == 0:
ghost.dir = '无'
ghost.energy = -1
ghost.timer = ghost.pause_time
if ghost.timer == 0:
restart_ghost()
ghost.timer -= 1
def restart_ghost():
path = bfs_maze_solver(walls)
if len(path) < 2:
return
if path[1][0] > path[0][0]:
ghost.dir = 'down'
if path[1][0] < path[0][0]:
ghost.dir = 'up'
if path[1][1] > path[0][1]:
ghost.dir = 'right'
if path[1][1] < path[0][1]:
ghost.dir = 'left'
ghost.energy = -2 * cube_size
for y, x in path:
ghost.energy += cube_size
if y != path[0][0] and x != path[0][1]:
break
else:
ghost.energy = 1000 * cube_size
def can_move(obj, direct):
x = obj.x
y = obj.y
if direct == 'left':
x -= obj.speed
x2 = x
y2 = y + cube_size - 1
if direct == 'right':
x += obj.speed + cube_size - 1
x2 = x
y2 = y + cube_size - 1
if direct == 'up':
y -= obj.speed
x2 = x + cube_size - 1
y2 = y
if direct == 'down':
y += obj.speed + cube_size - 1
x2 = x + cube_size - 1
y2 = y
return walls[int(y / cube_size)][int(x / cube_size)] != 1 and walls[int(y2 / cube_size)][int(x2 / cube_size)] != 1
def collide():
global state, time
x = me.x + cube_size / 2
y = me.y + cube_size / 2
if walls[int(y / cube_size)][int(x / cube_size)] == 2:
if level < len(walls_str) - 1:
state = '准备'
time = 3
ybc_game.delay(start, 1)
ybc_game.play_sound('win')
if level == len(walls_str) - 1:
state = '胜利'
ybc_game.play_sound('win')
if weapon.enable and abs(me.x - weapon.x) < cube_size and abs(me.y - weapon.y) < cube_size:
weapon.follow_me = True
if weapon.enable and weapon.follow_me and abs(ghost.x - weapon.x) <= cube_size and abs(ghost.y - weapon.y) <= cube_size:
ghost.enable = False
weapon.enable = False
if ghost.enable and abs(me.x - ghost.x) < cube_size-1 and abs(me.y - ghost.y) < cube_size-1:
state = '失败'
def on_key_down(key):
if key in ['left', 'right', 'up', 'down']:
me.new_dir = key
def bfs_maze_solver(maze):
# 定义移动方向(上,下,左,右)
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
start_x = int((ghost.x + cube_size / 2) / cube_size)
start_y = int((ghost.y + cube_size / 2) / cube_size)
# 队列用来存储在探索中待访问的位置
queue = deque([(start_y, start_x, [])]) # 存储坐标和到该点的路径
visited = set([(start_y, start_x)]) # 存储已访问的点
x = int((me.x + cube_size / 2) / cube_size)
y = int((me.y + cube_size / 2) / cube_size)
# BFS循环
while queue:
row, col, path = queue.popleft()
# 当到达迷宫出口
if row == y and col == x:
return path + [(row, col)]
# 探索当前点四周的点
for dr, dc in directions:
r, c = row + dr, col + dc
if 0 <= r < len(maze) and 0 <= c < len(maze[0]) and maze[r][c] == 0 and (r, c) not in visited:
queue.append((r, c, path + [(row, col)]))
visited.add((r, c))
# 如果没有找到路径,则返回空列表
return []
ybc_game.go()
迷宫游戏,点赞鼓励一下,更新了大地图和武器
操作方式
方向键