1
\$\begingroup\$

I am making a game right now, and I am working on collision for different Rect walls (all of their data is stored in a list). It works great, except for one problem: when I try to move to the side while also trying to ram in a wall, it won't move to either side (i.e. if I were to move forward into a wall while also trying to press one of the side keys, then the player won't move at all.

I have tried to take out the while loop (shown below in the code), but it removes the collision effect.

Code

Variables

LEFT = 'left'
DOWN = 'down'
UP = 'up'
RIGHT = 'right'

NW = LEFT + UP
SW = LEFT + DOWN
NE = RIGHT + UP
SE = RIGHT + DOWN

PLAYERDATA = {
    'size': 30,
    'speed': 5
}

checkWallCollision() function

def checkWallCollision():
    global walls
    
    for w in walls:    
        w['rect'] = Rect(w['x'], w['y'], w['width'], w['height'])
        draw.rect(DISPLAY, DARKGRAY, w['rect'])
        
        if Rect.colliderect(player.display, w['rect']):
            while Rect.colliderect(player.display, w['rect']):
                if playerDirection == LEFT: 
                    player.x += PLAYERDATA['speed']
                if playerDirection == RIGHT:
                    player.x -= PLAYERDATA['speed']
                if playerDirection == UP:
                    player.y += PLAYERDATA['speed']
                if playerDirection == DOWN:
                    player.y -= PLAYERDATA['speed']
                if playerDirection == NW:
                    player.x += PLAYERDATA['speed']
                    player.y += PLAYERDATA['speed']
                if playerDirection == SW:
                    player.x += PLAYERDATA['speed']
                    player.y -= PLAYERDATA['speed']
                if playerDirection == NE:
                    player.x -= PLAYERDATA['speed']
                    player.y += PLAYERDATA['speed']
                if playerDirection == SE:
                    player.x -= PLAYERDATA['speed']
                    player.y -= PLAYERDATA['speed']

                updatePlayerDisplay()

updatePlayerDisplay() function

def updatePlayerDisplay():
    player.display = Rect(player.x, player.y, PLAYERDATA['size'], PLAYERDATA['size'])

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Does playerDirection imply that your character can move in 8 different directions while also facing them, or does it move in 8 directions but only face in cardinal directions? \$\endgroup\$
    – Steven
    Apr 3 at 7:40
  • \$\begingroup\$ Both, but it just depends on what keys the user is pressing. (i.e. if the user is just pressing [W] then it will just go North, but if they are pressing [W] and [A] at the same time, then they would be considered as going North West. \$\endgroup\$
    – Tiimzee
    Apr 3 at 12:07

1 Answer 1

0
\$\begingroup\$

From the looks of it, every movement of the players x and y values updated to the reversed direction when it's colliding with a wall. Is it possible that you can just set the player's X or Y to 0 when it's colliding with the wall in one direction?

while Rect.colliderect(player.display, w['rect']):
            if playerDirection == LEFT: 
                player.x = 0
            if playerDirection == RIGHT:
                player.x = 0
            if playerDirection == UP:
                player.y = 0
            if playerDirection == DOWN:
                player.y = 0

            updatePlayerDisplay()

It doesn't need to stop it's movement in both x and y axes, just on the one it was facing agaist a wall. (This is why I asked in how many ways the player is facing, As I think setting diagonal collision is not necessary if they're already checking each four seperately)

\$\endgroup\$
2
  • \$\begingroup\$ When I tried this, it just puts it to the corner of the screen... \$\endgroup\$
    – Tiimzee
    Apr 3 at 14:39
  • \$\begingroup\$ Oh right, this is their direct position, I had in mind that it was a variable that determines their movement speed. in that case, I think you can keep it reversed if that works for you, but keep the diagonal parts removed. \$\endgroup\$
    – Steven
    Apr 4 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .