2

I'm new to coding (somewhat familiar with basic concepts). Tried to create a maze with Pygame but it keeps freezing and I'm not sure what's the problem.

The code essentially goes like this: the map is a 2D array (Each number is a different pictures), and put each picture in its place. "if pressed[pygame.K_w]:" is there to test if I can successfully move the player.

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

I would get a map that I've drawn but whenever I press or click anything it just freezes

qwerty_99
  • 640
  • 5
  • 20

1 Answers1

1

The application freeze, because the pygame events are not handled.
The states which are returned by pygame.key.get_pressed() are evaluated, when the pygame.events are handled by either pygame.event.get() or pygame.event.pump().
Add an event loop to the game loop and call pygame.key.get_pressed() after that:

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks! The app stopped freezing but I still have trouble making the player move. Guess I'll read more on event handling before continuing – qwerty_99 Jun 11 '19 at 22:17