pygame迷宫不断冻结

时间:2019-06-10 18:10:29

标签: python pygame

我是编码的新手(有点熟悉基本概念)。试图用Pygame创建迷宫,但它会一直冻结,我不确定是什么问题。

代码本质上是这样的:地图是一个2D数组(每个数字是不同的图片),并将每个图片放在其位置。 “如果按下[pygame.K_w]:”可以测试我是否可以成功移动玩家。

import sys
sys.path.append('C:/Users/User_Name/AppData/Local/Programs/Python/Python37-32/Lib/site-packages')
import pygame
pygame.init()

print(pygame.ver)

xPlayer = 50
yPlayer = 50

class maze:
    def __init__(self):
        self.width = 10
        self.lenght = 10
        self.map = [
            [1, 1, 1, 1, 1, 1, 1, 3, 1, 1],
            [1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
            [1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
            [1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
            [1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
            [1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
            [1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
            [2, 0, 0, 1, 1, 0, 0, 1, 1, 1],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        ]
    def drawMap(self,display_surf,image_wall, image_start, image_finish, image_target, image_player):
        mapX = 0
        mapY = 0
        for i in range(0,self.width):
            for j in range(0,self.lenght):
                if self.map[i][j] == 1:
                    display_surf.blit(image_wall, (i*xPlayer, j*yPlayer))
                elif self.map[i][j] == 2:
                    display_surf.blit(image_start, (i*xPlayer, j*yPlayer))
                elif self.map[i][j] == 3:
                    display_surf.blit(image_finish, (i*xPlayer, j*yPlayer))
                #elif self.map[i][j] == 15:
                    #display_surf.blit(image_target, (i*xPlayer, j*yPlayer))
                #elif self.map[i][j] == 30:
                    #display_surf.blit(image_player, (i*xPlayer, j*yPlayer))
    def movePlayer(self, display_surf, image_player):
        #elif self.map[i][j] == 15:
            #display_surf.blit(image_target, (i*xPlayer, j*yPlayer))
        for i in range(0,self.width):
            for j in range(0,self.lenght):
                if self.map[i][j] == 30:
                    display_surf.blit(image_player, (i*xPlayer, j*yPlayer))
                if pressed[pygame.K_w]:
                    i += 50



windowWidth = 500
windowLenght = 500
player = 0
gameOver = False
mazeDisplayed = maze()
display_surf = pygame.display.set_mode((windowWidth, windowLenght))
image_player = pygame.image.load("image_player.png")
image_wall = pygame.image.load("image_wall.png")
image_start = pygame.image.load("image_start.png")
image_finish = pygame.image.load("image_finish.png")
image_targer = pygame.image.load("image_target.png")
count = 0
print(mazeDisplayed.map[5][5])
PlayerX = 5
PlayerY = 5
playerNumber = 30
pressed = pygame.key.get_pressed()


while not gameOver:
    mazeDisplayed.map[PlayerX][PlayerY] = playerNumber;
    mazeDisplayed.drawMap(display_surf,image_wall,image_start,image_finish,image_targer,image_player)
    mazeDisplayed.movePlayer(display_surf,image_player)
    mazeDisplayed.drawMap(display_surf, image_wall, image_start, image_finish, image_targer, image_player)
    pygame.display.flip()
    #gameOver = True

我会得到一张绘制的地图,但是只要我按下或单击任何东西,它就会冻结

1 个答案:

答案 0 :(得分:1)

由于未处理pygame事件,应用程序冻结。
pygame.key.get_pressed()pygame.events处理pygame.event.get()时,将评估pygame.event.pump()返回的状态。
将事件循环添加到游戏循环中,然后调用pygame.key.get_pressed()

tun = True
while run and not gameOver:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    pressed = pygame.key.get_pressed()

    mazeDisplayed.map[PlayerX][PlayerY] = playerNumber;
    # [...]
    pygame.display.flip()