0

I need a wallCollision function so the blue player doesn't move through the walls anymore. The player is not a class. It's just a blue rectangle that gets drawn by going through a list of the next positions. The walls are classes. I tried to change that, but my code stopped working. Is there any way to make it happen, that the rectangle can't go through the walls?

My function to draw the Level:

def drawLevel(level):
    global gameDrawn
    x = y = 0

    

    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)
                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

My wall class:

class Wall(object):
    def __init__(self, pos):
        self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))

My methods to get the player coordinates and to draw the player path:

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))
def drawPlayerPath(players):
    for x, y in players:
        drawRect(x, y)
def drawRect(x, y):
    pygame.draw.rect(screen, BLUE, [pxl(x), pxl(y), pxl(1), pxl(1)])

0 Answers0