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
迷宮。現在你可能想要新增我們在上一個教程中展示的碰撞檢測。因為我們已經解釋了這個概念,所以我們不會再重複它了🙂