1

my Wall Collision code doesnt work. I dont get any error message, the player just goes through the walls. This is my code:

def drawRect(x, y):
    pygame.draw.rect(screen, BLUE, [pxl(x), pxl(y), pxl(1), pxl(1)])
def drawLevel(level):
    global gameDrawn, wallsCoord
    x = y = 0

    wallsCoord = []

    walls = []

    ends = []

    
    
    if gameDrawn == False:
        screen.fill(WOODY)
        
        drawRect(1, 8)
        showTimer()
        
        for row in levels[level]:
            for col in row:
                if col == "W":
                    wall = Wall((x, y))
                    walls.append(wall)
                    wallsCoord.append((x, y))
                if col == "E":
                    end = End((x, y))
                    ends.append(end)
                if col == "P":
                    drawRect(1, 9)
                x += 80
            y += 80
            x = 0
        for wall in walls:
            pygame.draw.rect(screen, BLACK, wall.rect)
        for end in ends:
            pygame.draw.rect(screen, RED, end.rect)
    gameDrawn = True  
    return walls, ends

def drawPlayerPath(players):
    global wallsCoord
    for x, y in players:
        if (x, y) in wallsCoord:
            state = "gameover"
            init_state = True
        else:
            drawRect(x, y)

def getPlayerPath(players):
    global move_list, player_x, player_y
    player_x, player_y = players[0]
    for i in move_list:
        if i == 1:
            player_y -= 1
        elif i == 2:
            player_y += 1
        elif i == 3:
            player_x += 1
        elif i == 4:
            player_x -= 1
        players.append((player_x, player_y))

I dont really get, why it doesnt work since I already iterated the x and y values of the players list over the coordinates list of the walls.

2 Answers2

0

I suggest you add your walls in a sprite group and then you can iterate using the built-in collide method pygame.Rect.colliderect

Shawky Ahmed
  • 35
  • 10
  • Could you help me and tell me how to do that? I never created a sprite group – Luca Glaentzer Aug 14 '21 at 19:57
  • Check that previously answered question: [how-to-use-sprite-groups-in-pygame](https://stackoverflow.com/questions/13851051/how-to-use-sprite-groups-in-pygame) – Shawky Ahmed Aug 14 '21 at 20:09
  • 3
    No! When using Sprites, `colliderect` ist not the correct function. Sprites and Groups have there own collision functions. – Rabbid76 Aug 14 '21 at 20:17
  • @Rabbid76 Oh, you are right I got confused offering solution using rect collision & mention sprites & spritegroup – Shawky Ahmed Aug 14 '21 at 20:19
  • But then the player rectangles must be sprites as well, am I right? – Luca Glaentzer Aug 14 '21 at 20:24
  • @ShawkyAhmed but can I use the sprite collision function to check if the sprite collides with a normal non-sprite rectangle? – Luca Glaentzer Aug 14 '21 at 20:35
  • I don't think you can, I suggest you just use the "walls" list to iterate over and check collision using pygame.Rect.colliderect(rect2), also you can check specific point for if collides with wall rect using pygame.Rect.collidepoint(x,y) – Shawky Ahmed Aug 15 '21 at 05:23
0

You have to use the global statement, when you want to change a variable in global namespace within a function:

def drawPlayerPath(players):
    global state, init_state    # <---
    for x, y in players:
        if (x, y) in wallsCoord:
            state = "gameover"
            init_state = True
        else:
            drawRect(x, y)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174