Python實現過迷宮小遊戲示例詳解

前言

今天為大傢帶來解悶用的過迷宮小遊戲分享給大傢好瞭。讓我們愉快地開始吧~

開發工具

Python版本: 3.6.4

相關模塊:

pygame模塊;

以及一些Python自帶的模塊。

環境搭建

安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。

原理簡介

遊戲規則:

玩傢通過↑↓←→鍵控制主角行動,使主角從出發點(左上角)繞出迷宮,到達終點(右下角)即為遊戲勝利。

逐步實現:

首先,當然是創建迷宮啦,為瞭方便,這裡采用隨機生成迷宮的方式(人工設計真的費眼睛,弄到一半不想弄瞭,有興趣的可以自行嘗試。)。思路其實很簡單,就是把遊戲界面劃分成多個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')

到此這篇關於Python實現過迷宮小遊戲示例詳解的文章就介紹到這瞭,更多相關Python過迷宮小遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: