原文: https://pythonspot.com/maze-in-pygame/

在本教程中,您将学习如何构建迷宫游戏。 这个想法只是用箭头键在迷宫中移动。

入门:基本结构和事件处理。

我们定义了一个Player类,它可以保持玩家在屏幕上的位置及其移动的速度。 另外,我们定义Player实例可以执行的动作(动作):

  1. class Player:
  2. x = 10
  3. y = 10
  4. speed = 1
  5. def moveRight(self):
  6. self.x = self.x + self.speed
  7. def moveLeft(self):
  8. self.x = self.x - self.speed
  9. def moveUp(self):
  10. self.y = self.y - self.speed
  11. def moveDown(self):
  12. self.y = self.y + self.speed

可以使用移动方法创建玩家对象,并可以修改变量。 我们将这些方法链接到事件。 在 Pygame 中,我们可以使用以下代码获得非阻塞式键盘输入:

  1. pygame.event.pump()
  2. keys = pygame.key.get_pressed()
  3. if (keys[K_RIGHT]):
  4. print "Right arrow pressed."

完整的代码使我们能够在屏幕上移动播放器:

  1. from pygame.locals import *
  2. import pygame
  3. class Player:
  4. x = 10
  5. y = 10
  6. speed = 1
  7. def moveRight(self):
  8. self.x = self.x + self.speed
  9. def moveLeft(self):
  10. self.x = self.x - self.speed
  11. def moveUp(self):
  12. self.y = self.y - self.speed
  13. def moveDown(self):
  14. self.y = self.y + self.speed
  15. class App:
  16. windowWidth = 800
  17. windowHeight = 600
  18. player = 0
  19. def __init__(self):
  20. self._running = True
  21. self._display_surf = None
  22. self._image_surf = None
  23. self.player = Player()
  24. def on_init(self):
  25. pygame.init()
  26. self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
  27. pygame.display.set_caption('Pygame pythonspot.com example')
  28. self._running = True
  29. self._image_surf = pygame.image.load("pygame.png").convert()
  30. def on_event(self, event):
  31. if event.type == QUIT:
  32. self._running = False
  33. def on_loop(self):
  34. pass
  35. def on_render(self):
  36. self._display_surf.fill((0,0,0))
  37. self._display_surf.blit(self._image_surf,(self.player.x,self.player.y))
  38. pygame.display.flip()
  39. def on_cleanup(self):
  40. pygame.quit()
  41. def on_execute(self):
  42. if self.on_init() == False:
  43. self._running = False
  44. while( self._running ):
  45. pygame.event.pump()
  46. keys = pygame.key.get_pressed()
  47. if (keys[K_RIGHT]):
  48. self.player.moveRight()
  49. if (keys[K_LEFT]):
  50. self.player.moveLeft()
  51. if (keys[K_UP]):
  52. self.player.moveUp()
  53. if (keys[K_DOWN]):
  54. self.player.moveDown()
  55. if (keys[K_ESCAPE]):
  56. self._running = False
  57. self.on_loop()
  58. self.on_render()
  59. self.on_cleanup()
  60. if __name__ == "__main__" :
  61. theApp = App()
  62. theApp.on_execute()

现在,您可以使用箭头键在屏幕上移动该块。

Pygame 中的迷宫 - 图1

pygame

创建迷宫

我们定义一个 NxM 矩阵来表示迷宫块的位置。 在此矩阵中,元素 1 表示存在一个块,元素 0 表示不存在。

  1. class Maze:
  2. def __init__(self):
  3. self.M = 10
  4. self.N = 8
  5. self.maze = [ 1,1,1,1,1,1,1,1,1,1,
  6. 1,0,0,0,0,0,0,0,0,1,
  7. 1,0,0,0,0,0,0,0,0,1,
  8. 1,0,1,1,1,1,1,1,0,1,
  9. 1,0,1,0,0,0,0,0,0,1,
  10. 1,0,1,0,1,1,1,1,0,1,
  11. 1,0,0,0,0,0,0,0,0,1,
  12. 1,1,1,1,1,1,1,1,1,1,]

我们有完整的代码来绘制迷宫:

  1. from pygame.locals import *
  2. import pygame
  3. class Player:
  4. x = 44
  5. y = 44
  6. speed = 1
  7. def moveRight(self):
  8. self.x = self.x + self.speed
  9. def moveLeft(self):
  10. self.x = self.x - self.speed
  11. def moveUp(self):
  12. self.y = self.y - self.speed
  13. def moveDown(self):
  14. self.y = self.y + self.speed
  15. class Maze:
  16. def __init__(self):
  17. self.M = 10
  18. self.N = 8
  19. self.maze = [ 1,1,1,1,1,1,1,1,1,1,
  20. 1,0,0,0,0,0,0,0,0,1,
  21. 1,0,0,0,0,0,0,0,0,1,
  22. 1,0,1,1,1,1,1,1,0,1,
  23. 1,0,1,0,0,0,0,0,0,1,
  24. 1,0,1,0,1,1,1,1,0,1,
  25. 1,0,0,0,0,0,0,0,0,1,
  26. 1,1,1,1,1,1,1,1,1,1,]
  27. def draw(self,display_surf,image_surf):
  28. bx = 0
  29. by = 0
  30. for i in range(0,self.M*self.N):
  31. if self.maze[ bx + (by*self.M) ] == 1:
  32. display_surf.blit(image_surf,( bx * 44 , by * 44))
  33. bx = bx + 1
  34. if bx > self.M-1:
  35. bx = 0
  36. by = by + 1
  37. class App:
  38. windowWidth = 800
  39. windowHeight = 600
  40. player = 0
  41. def __init__(self):
  42. self._running = True
  43. self._display_surf = None
  44. self._image_surf = None
  45. self._block_surf = None
  46. self.player = Player()
  47. self.maze = Maze()
  48. def on_init(self):
  49. pygame.init()
  50. self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
  51. pygame.display.set_caption('Pygame pythonspot.com example')
  52. self._running = True
  53. self._image_surf = pygame.image.load("player.png").convert()
  54. self._block_surf = pygame.image.load("block.png").convert()
  55. def on_event(self, event):
  56. if event.type == QUIT:
  57. self._running = False
  58. def on_loop(self):
  59. pass
  60. def on_render(self):
  61. self._display_surf.fill((0,0,0))
  62. self._display_surf.blit(self._image_surf,(self.player.x,self.player.y))
  63. self.maze.draw(self._display_surf, self._block_surf)
  64. pygame.display.flip()
  65. def on_cleanup(self):
  66. pygame.quit()
  67. def on_execute(self):
  68. if self.on_init() == False:
  69. self._running = False
  70. while( self._running ):
  71. pygame.event.pump()
  72. keys = pygame.key.get_pressed()
  73. if (keys[K_RIGHT]):
  74. self.player.moveRight()
  75. if (keys[K_LEFT]):
  76. self.player.moveLeft()
  77. if (keys[K_UP]):
  78. self.player.moveUp()
  79. if (keys[K_DOWN]):
  80. self.player.moveDown()
  81. if (keys[K_ESCAPE]):
  82. self._running = False
  83. self.on_loop()
  84. self.on_render()
  85. self.on_cleanup()
  86. if __name__ == "__main__" :
  87. theApp = App()
  88. theApp.on_execute()

Pygame 中的迷宫 - 图2

迷宫

结论

您学习了如何在 Python 中创建 2d 迷宫。 现在,您可能想要添加在上一教程中显示的碰撞检测。 因为我们已经解释了这个概念,所以我们不再赘述 :-)

下一个教程:Pygame 的 AI