原理介绍
游戏规则:
玩家通过↑↓←→键管制配角口头,使配角从出发点 (左上角) 绕出迷宫,达到起点 (右下角) 即为游戏胜利。
逐渐实现:
首先,当然是创立迷宫啦,为了不便,这里采纳随机生成迷宫的形式 (人工设计真的费眼睛,弄到一半不想弄了,有趣味的能够自行尝试。)。思路其实很简略,就是把游戏界面划分成多个 cell,相似这样子:
而后设计算法遍历所有的 cell,每个被遍历到的 cell 在某几个随机的方向上关上一堵墙 (就是去掉宰割 cell 的线条) 就 ok 啦~
具体而言,代码实现如下:
'''随机生成迷宫类'''
class RandomMaze():
def __init__(self, maze_size, block_size, border_size, **kwargs):
self.block_size = block_size
self.border_size = border_size
self.maze_size = maze_size
self.blocks_list = RandomMaze.createMaze(maze_size, block_size, border_size)
self.font = pygame.font.SysFont('Consolas', 15)
'''画到屏幕上'''
def draw(self, screen):
for row in range(self.maze_size[0]):
for col in range(self.maze_size[1]):
self.blocks_list[row][col].draw(screen)
# 终点和起点标记
showText(screen, self.font, 'S', (255, 0, 0), (self.border_size[0]-10, self.border_size[1]))
showText(screen, self.font, 'D', (255, 0, 0), (self.border_size[0]+(self.maze_size[1]-1)*self.block_size, self.border_size[1]+self.maze_size[0]*self.block_size+5))
'''创立迷宫'''
@staticmethod
def createMaze(maze_size, block_size, border_size):
def nextBlock(block_now, blocks_list):
directions = ['top', 'bottom', 'left', 'right']
blocks_around = dict(zip(directions, [None]*4))
block_next = None
count = 0
# 查看上边 block
if block_now.coordinate[1]-1 >= 0:
block_now_top = blocks_list[block_now.coordinate[1]-1][block_now.coordinate[0]]
if not block_now_top.is_visited:
blocks_around['top'] = block_now_top
count += 1
# 查看下边 block
if block_now.coordinate[1]+1 < maze_size[0]:
block_now_bottom = blocks_list[block_now.coordinate[1]+1][block_now.coordinate[0]]
if not block_now_bottom.is_visited:
blocks_around['bottom'] = block_now_bottom
count += 1
# 查看右边 block
if block_now.coordinate[0]-1 >= 0:
block_now_left = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]-1]
if not block_now_left.is_visited:
blocks_around['left'] = block_now_left
count += 1
# 查看左边 block
if block_now.coordinate[0]+1 < maze_size[1]:
block_now_right = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]+1]
if not block_now_right.is_visited:
blocks_around['right'] = block_now_right
count += 1
if count > 0:
while True:
direction = random.choice(directions)
if blocks_around.get(direction):
block_next = blocks_around.get(direction)
if direction == 'top':
block_next.has_walls[1] = False
block_now.has_walls[0] = False
elif direction == 'bottom':
block_next.has_walls[0] = False
block_now.has_walls[1] = False
elif direction == 'left':
block_next.has_walls[3] = False
block_now.has_walls[2] = False
elif direction == 'right':
block_next.has_walls[2] = False
block_now.has_walls[3] = False
break
return block_next
blocks_list = [[Block([col, row], block_size, border_size) for col in range(maze_size[1])] for row in range(maze_size[0])]
block_now = blocks_list[0][0]
records = []
while True:
if block_now:
if not block_now.is_visited:
block_now.is_visited = True
records.append(block_now)
block_now = nextBlock(block_now, blocks_list)
else:
block_now = records.pop()
if len(records) == 0:
break
return blocks_list
接下来就是定义角色类啦,角色类须要依据用户的操作进行上下左右的挪动,同时,保障挪动是不能逾越墙的就 OK 了~ 具体而言,代码实现如下:
'''定义 hero'''
class Hero(pygame.sprite.Sprite):
def __init__(self, imagepath, coordinate, block_size, border_size, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.scale(self.image, (block_size, block_size))
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = coordinate[0] * block_size + border_size[0], coordinate[1] * block_size + border_size[1]
self.coordinate = coordinate
self.block_size = block_size
self.border_size = border_size
'''挪动'''
def move(self, direction, maze):
blocks_list = maze.blocks_list
if direction == 'up':
if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[0]:
return False
else:
self.coordinate[1] = self.coordinate[1] - 1
return True
elif direction == 'down':
if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[1]:
return False
else:
self.coordinate[1] = self.coordinate[1] + 1
return True
elif direction == 'left':
if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[2]:
return False
else:
self.coordinate[0] = self.coordinate[0] - 1
return True
elif direction == 'right':
if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[3]:
return False
else:
self.coordinate[0] = self.coordinate[0] + 1
return True
else:
raise ValueError('Unsupport direction <%s> in Hero.move...' % direction)
'''绑定到屏幕'''
def draw(self, screen):
self.rect.left, self.rect.top = self.coordinate[0] * self.block_size + self.border_size[0], self.coordinate[1] * self.block_size + self.border_size[1]
screen.blit(self.image, self.rect)
最初,就是写下游戏主循环,这个其实也很简略,只有每次载入一个随机生成的迷宫地图和实例化一个配角,而后一直进行按键检测,并依据按键检测的后果挪动配角,最初依据口头后果更新界面数据就 OK 了~ 当然,若配角达到了起点,则进入关卡切换界面。
具体而言,代码实现如下:
'''主函数'''
def main(cfg):
# 初始化
pygame.init()
pygame.mixer.init()
pygame.font.init()
pygame.mixer.music.load(cfg.BGMPATH)
pygame.mixer.music.play(-1, 0.0)
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('Maze - 微信公众号: Charles 的皮卡丘')
font = pygame.font.SysFont('Consolas', 15)
# 开始界面
Interface(screen, cfg, 'game_start')
# 记录关卡数
num_levels = 0
# 记录起码用了多少步通关
best_scores = 'None'
# 关卡循环切换
while True:
num_levels += 1
clock = pygame.time.Clock()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
# -- 随机生成关卡地图
maze_now = RandomMaze(cfg.MAZESIZE, cfg.BLOCKSIZE, cfg.BORDERSIZE)
# -- 生成 hero
hero_now = Hero(cfg.HEROPICPATH, [0, 0], cfg.BLOCKSIZE, cfg.BORDERSIZE)
# -- 统计步数
num_steps = 0
# -- 关卡内主循环
while True:
dt = clock.tick(cfg.FPS)
screen.fill((255, 255, 255))
is_move = False
# ----↑↓←→管制 hero
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(-1)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
is_move = hero_now.move('up', maze_now)
elif event.key == pygame.K_DOWN:
is_move = hero_now.move('down', maze_now)
elif event.key == pygame.K_LEFT:
is_move = hero_now.move('left', maze_now)
elif event.key == pygame.K_RIGHT:
is_move = hero_now.move('right', maze_now)
num_steps += int(is_move)
hero_now.draw(screen)
maze_now.draw(screen)
# ---- 显示一些信息
showText(screen, font, 'LEVELDONE: %d' % num_levels, (255, 0, 0), (10, 10))
showText(screen, font, 'BESTSCORE: %s' % best_scores, (255, 0, 0), (210, 10))
showText(screen, font, 'USEDSTEPS: %s' % num_steps, (255, 0, 0), (410, 10))
showText(screen, font, 'S: your starting point D: your destination', (255, 0, 0), (10, 600))
# ---- 判断游戏是否胜利
if (hero_now.coordinate[0] == cfg.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == cfg.MAZESIZE[0] - 1):
break
pygame.display.update()
# 更新最优问题
if best_scores == 'None':
best_scores = num_steps
else:
if best_scores > num_steps:
best_scores = num_steps
# 关卡切换
Interface(screen, cfg, mode='game_switch')
成果展现