Pygame 迷宫游戏
在本教程中,你将学习如何构建迷宫游戏,用箭头键在迷宫中移动。
入门:基本结构和事件处理
我们定义了一个类 Player
,它保持玩家在屏幕上的位置以及它移动的速度。此外,我们定义了 Player
实例可以执行的操作(移动):
class Player:
x = 10
y = 10
speed = 1
def moveRight(self):
self.x = self.x + self.speed
def moveLeft(self):
self.x = self.x - self.speed
def moveUp(self):
self.y = self.y - self.speed
def moveDown(self):
self.y = self.y + self.speed
可以创建 Player
对象,并可以使用移动方法修改变量。我们将这些方法与事件联系起来。在 Pygame 中,我们可以使用以下代码获得非阻塞键盘输入:
pygame.event.pump()
keys = pygame.key.get_pressed()
if (keys[K_RIGHT]):
print "Right arrow pressed."
下面的完整代码让我们能够在屏幕内移动 Player
。
from pygame.locals import *
import pygame
class Player:
x = 10
y = 10
speed = 1
def moveRight(self):
self.x = self.x + self.speed
def moveLeft(self):
self.x = self.x - self.speed
def moveUp(self):
self.y = self.y - self.speed
def moveDown(self):
self.y = self.y + self.speed
class App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self.player = Player()
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
pygame.display.set_caption('Pygame pythonspot.com example')
self._running = True
self._image_surf = pygame.image.load("pygame.png").convert()
def on_event(self, event):
if event.type == QUIT:
self._running = False
def on_loop(self):
pass
def on_render(self):
self._display_surf.fill((0,0,0))
self._display_surf.blit(self._image_surf,(self.player.x,self.player.y))
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if self.on_init() == False:
self._running = False
while( self._running ):
pygame.event.pump()
keys = pygame.key.get_pressed()
if (keys[K_RIGHT]):
self.player.moveRight()
if (keys[K_LEFT]):
self.player.moveLeft()
if (keys[K_UP]):
self.player.moveUp()
if (keys[K_DOWN]):
self.player.moveDown()
if (keys[K_ESCAPE]):
self._running = False
self.on_loop()
self.on_render()
self.on_cleanup()
if __name__ == "__main__" :
theApp = App()
theApp.on_execute()
你可以用箭头键在屏幕内移动方块。
![pygame 的](/img/Tutorial/Pygame/pygame maze.webp)
创造迷宫
我们定义 NxM
矩阵来表示迷宫块的位置。在该矩阵中,元素 1
表示块存在,元素 0
表示不存在。
class Maze:
def __init__(self):
self.M = 10
self.N = 8
self.maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,]
我们有完整的代码来绘制迷宫:
from pygame.locals import *
import pygame
class Player:
x = 44
y = 44
speed = 1
def moveRight(self):
self.x = self.x + self.speed
def moveLeft(self):
self.x = self.x - self.speed
def moveUp(self):
self.y = self.y - self.speed
def moveDown(self):
self.y = self.y + self.speed
class Maze:
def __init__(self):
self.M = 10
self.N = 8
self.maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,]
def draw(self,display_surf,image_surf):
bx = 0
by = 0
for i in range(0,self.M*self.N):
if self.maze[ bx + (by*self.M) ] == 1:
display_surf.blit(image_surf,( bx * 44 , by * 44))
bx = bx + 1
if bx > self.M-1:
bx = 0
by = by + 1
class App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self._block_surf = None
self.player = Player()
self.maze = Maze()
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
pygame.display.set_caption('Pygame pythonspot.com example')
self._running = True
self._image_surf = pygame.image.load("player.png").convert()
self._block_surf = pygame.image.load("block.png").convert()
def on_event(self, event):
if event.type == QUIT:
self._running = False
def on_loop(self):
pass
def on_render(self):
self._display_surf.fill((0,0,0))
self._display_surf.blit(self._image_surf,(self.player.x,self.player.y))
self.maze.draw(self._display_surf, self._block_surf)
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if self.on_init() == False:
self._running = False
while( self._running ):
pygame.event.pump()
keys = pygame.key.get_pressed()
if (keys[K_RIGHT]):
self.player.moveRight()
if (keys[K_LEFT]):
self.player.moveLeft()
if (keys[K_UP]):
self.player.moveUp()
if (keys[K_DOWN]):
self.player.moveDown()
if (keys[K_ESCAPE]):
self._running = False
self.on_loop()
self.on_render()
self.on_cleanup()
if __name__ == "__main__" :
theApp = App()
theApp.on_execute()
![迷宫](/img/Tutorial/Pygame/pygame maze complete.webp)
总结
你学会了如何在 Python 中创建 2d
迷宫。现在你可能想要添加我们在上一个教程中展示的碰撞检测。因为我们已经解释了这个概念,所以我们不会再重复它了🙂