0

I tried creating a variable called level and deppending on their value draw the next level but it's not working, literally nothing happens I also tried to call the function "createMaze" when the player collide the goal but it was drawed at the same time with the other level. What other way to change the level I could use?

from pygame.locals import *


pygame.init()


#Variables del juego
windowWidth = 400
windowHeight = 400
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
mainClock = pygame.time.Clock()
FPS = 30


pygame.display.set_caption('Maze')


#Colores
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 255, 255)
red = (255, 0, 0)


#Tamaño del bloque

blockSize = 20


#Datos del jugador
playerSize = 15
player = pygame.Rect(0, 0, playerSize, playerSize)
speed = 5


#Variables de movimiento
moveLeft = moveRight = moveDown = moveUp = False


#Laberinto
maze1 = [
    "oboooooooooooooooooo"
    ]
maze2 = [
    "oxxxxxxxxxxxxxxxxxxx",
    "oxoooooooooooooooooo",
    "oxoxxxxxxxxxxxxxxxxo",
    "oxoxooooooooooooooxo",
    "oxoxoxxxxxxxxxxxxoxo",
    "oxoxoxooooooooooxoxo",
    "oxoxoxoxxxxxxxxoxoxo",
    "oxoxoxoxooooooxoxoxo",
    "oxoxoxoxoxxxxoxoxoxo",
    "oxoxoxoxoxboxoxoxoxo",
    "oxoxoxoxoxxoxoxoxoxo",
    "oxoxoxoxooooxoxoxoxo",
    "oxoxoxoxxxxxxoxoxoxo",
    "oxoxoxooooooooxoxoxo",
    "oxoxoxxxxxxxxxxoxoxo",
    "oxoxooooooooooooxoxo",
    "oxoxxxxxxxxxxxxxxoxo",
    "oxooooooooooooooooxo",
    "oxxxxxxxxxxxxxxxxxxo",
    "oooooooooooooooooooo"
]
blocks = []
level = 1
def createMaze(level, map):
    pointX = 0
    pointY = 0
    
    for i in map:
        for j in i:
            if j == "x":
                block = pygame.Rect(pointX, pointY, blockSize, blockSize)
                pygame.draw.rect(windowSurface, white, block)
                blocks.append(block)
                if block.colliderect(player):
                    if player.bottom - block.top ==  5:
                        player.y = block.top - playerSize
                    if block.bottom - player.top == 5:             
                        player.y = block.bottom
                    if block.right - player.left == 5:
                        player.x = block.right
                    if  player.right - block.left == 5:
                        player.x = block.left - playerSize
            if j == "b":
                block = pygame.Rect(pointX, pointY, blockSize, blockSize)
                pygame.draw.rect(windowSurface, red, block)
                blocks.append(block)
                if player.colliderect(block):
                    level += 1
            if j == "o":
                pass
            pointX += 20
        pointX = 0
        pointY += 20 

#Función para cerrar juego
def close():
    pygame.quit()
    sys.exit()


#Ciclo principal del juego
while True:
    #Buscar evento QUIT
    for event in pygame.event.get():
        if event.type == QUIT:
            close()

        if event.type == KEYDOWN:
            if event.key == K_w:
                moveUp = True
                moveDown = False
            if event.key == K_s:                
                moveUp = False
                moveDown = True
            if event.key == K_a:
                moveLeft = True
                moveRight = False
            if event.key == K_d:
                moveLeft = False
                moveRight = True

        if event.type == KEYUP:
            if event.key == K_w:
                moveUp = False
            if event.key == K_s:                
                moveDown = False
            if event.key == K_a:
                moveLeft = False
            if event.key == K_d:
                moveRight = False


    #Mover jugador
    if moveLeft and player.left > 0:
        player.move_ip(-1 * speed, 0)
    if moveRight and player.right < windowHeight:
        player.move_ip(speed, 0)
    if moveUp and player.top > 0:
        player.move_ip(0, -1 * speed)
    if moveDown and player.bottom < windowHeight:
        player.move_ip(0, speed)


    windowSurface.fill(black)
    if level == 1:
        createMaze(level, maze1)
    if level == 2:
        createMaze(level, maze2)
    pygame.draw.rect(windowSurface, blue, player)
    

    pygame.display.update()

    mainClock.tick(FPS)```
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

You need global level at the start of createMaze. Any time you're going to modify a global variable in a function, you have to use the global statement. It should have diagnosed that.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30