partyguy11
partyguy11

Reputation: 44

Platformer Getting Stuck in Wall

I seem to be having trouble with the platformer I am building in Pygame. I can glitch into the walls when approaching them from the left side. I set the x velocity to zero when approaching a wall, but I can still get in. Can someone please help me on this?

I am trying to make a game where the player has to parkour to the end of the track. This platformer is going to be a 2d, non-scrolling game when it is finished.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
BG_COLOR = pg.Color(173, 216, 230)

def main():
    clock = pg.time.Clock()
    image = pg.Surface((50, 50))
    image.fill(pg.Color('red'))
    player_rect = image.get_rect(topleft=(200, 200))
    image_ground = pg.Surface((1000, 50))
    image_ground.fill(pg.Color('black'))
    ground_rect = image_ground.get_rect(topleft=(0, 430))
    image_ground2 = pg.Surface((300, 50))
    image_ground2.fill(pg.Color('black'))
    ground_rect2 = image_ground2.get_rect(topleft=(0, 330))
    image_ground3 = pg.Surface((50, 50))
    image_ground3.fill(pg.Color('black'))
    ground_rect3 = image_ground3.get_rect(topleft=(400, 230))
    screen_rect = screen.get_rect()
    image_ground3.fill(pg.Color('black'))
    ground_rect3 = image_ground3.get_rect(topleft=(400, 230))
    screen_rect = screen.get_rect()
    xv = 0
    yv = 0
    level = 1

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return
        pressed = pg.key.get_pressed()
        if pressed[pg.K_RIGHT]:
            xv = xv + 0.85
        if pressed[pg.K_LEFT]:
            xv = xv - 0.85
        xv = xv * 0.9
        player_rect.x += xv
        if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
          player_rect.y -= 1
          if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
            player_rect.y -= 1
            if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
              player_rect.y -= 1
              if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
                player_rect.y -= 1
                if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
                  player_rect.y -= 1
                  if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
                    player_rect.y += 5
                    player_rect.x -= xv
                    if pressed[pg.K_UP]:
                      if xv > 0:
                        xv = -8
                      else:
                        xv = 8
                      yv = 13
                    else:
                      xv = 0
        yv = yv - 1
        player_rect.y -= yv
        if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
          player_rect.y += yv
          yv = 0
        player_rect.y += 1
        if player_rect.colliderect(ground_rect) or player_rect.colliderect(ground_rect2) or player_rect.colliderect(ground_rect3):
          if pressed[pg.K_UP]:
            yv = 13
        player_rect.y -= 1
        player_rect.clamp_ip(screen_rect)

        screen.fill(BG_COLOR)
        screen.blit(image, player_rect)
        screen.blit(image_ground, ground_rect)
        screen.blit(image_ground2, ground_rect2)
        screen.blit(image_ground3, ground_rect3)

        pg.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    main()
    pg.quit()

Upvotes: 0

Views: 79

Answers (2)

johnny
johnny

Reputation: 13

When the key up is pressed and you move into a wall, you bounce off (wall jump). But if you let go the up key and touch a wall, you get stuck. I'm not sure how to fix this, but the related posts may help you. :)

Upvotes: 0

partyguy11
partyguy11

Reputation: 44

I figured out the answer to my own question. The game was buggy because of rounding, the game was confused by moving 0.000000000000001 and bugged out glitching me into walls. I fixed it by rounding xv before moving the player.

Upvotes: 1

Related Questions