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'])
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\$