0
\$\begingroup\$

I am coding a game for fun, and I am trying to implement collision; specifically, I want the player to be able to move to the side even when ramming into a wall diagonally (I had this problem before, but I managed to resolve it here). It works great from top to bottom, but when I run into the sides, it just glitches down to the bottom (or top).

My original code is linked above, and here is my new code for the collion checking (so far, I can either do the sides or the top/bottom... I can only do one or the other so far, but I want both).

Code

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:

                    '''
                    As you can see here, I have tried to do only this
                    diagonal direction (testing purposes), but (again) can only do 
                    one or the other, not both (in my code, AL)
                    '''

                    player.y += PLAYERDATA['speed'] * 2

                    if Rect.colliderect(player.display, w['rect']):
                        player.y -= PLAYERDATA['speed']
                    else:
                        player.x -= PLAYERDATA['speed']
                        player.y -= PLAYERDATA['speed'] * 2
                        if Rect.colliderect(player.display, w['rect']):
                            player.x += PLAYERDATA['speed']
                if playerDirection == SW:
                    player.y -= PLAYERDATA['speed']
                if playerDirection == NE:
                    player.y += PLAYERDATA['speed']
                if playerDirection == SE:
                    player.y -= PLAYERDATA['speed']

                updatePlayerDisplay()
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .