0

When my player hits one of the boundaries with the value 0 it is perfectly fine and can move freely throughout the world. But when my player hits a boundary with the value 485, it fine until you try and move up. Then depending on the boundary you hit it will get stuck at playerX = 249 or playerY = 249
import pygame import time

pygame.init()

playerX = 250 # Player X locale
playerY = 250 # Players Y locale
playerSpeed = 2

backgroundX = 0 # Background X locale
backgroundY = 0 # Background Y locale

### LOADS PLAYER IMAGES ###
playerFrontImg = pygame.image.load("Images/Player Images/playerBackOne.png")
playerBackImg = pygame.image.load("Images/Player Images/playerFrontOne.png")
playerLeftImg = pygame.image.load("Images/Player Images/playerLeftOne.png")
playerRightImg = pygame.image.load("Images/Player Images/playerRightOne.png")
playerCurrentImg = playerFrontImg
### LOADS PLAYER IMAGES ###

### LOADS MAP IMAGES ###
testMap = pygame.image.load("Images/Map Images/TEST_MAP.png")
### LOADS MAP IMAGES ###

screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    ### KEYBOARD INPUTS AND MOVEMENTS ###
    keys = pygame.key.get_pressed() # gets pressed keys and assigns it TRUE or FALSE
    if keys[pygame.K_w]: # Checks if "w" is pressed

        playerCurrentImg = playerFrontImg # Changes current image to correct image

        if playerY == 250:
            if backgroundY < 0:
                backgroundY += playerSpeed
        if backgroundY >= 0 or playerY > 250:
            playerY -= playerSpeed
        if playerY <= 0:
            playerY = 0

    if keys[pygame.K_s]: # Checks if "s" is pressed

        playerCurrentImg = playerBackImg # Changes current image to correct image

        if playerY == 250:
            if backgroundY > -500:
                backgroundY -= playerSpeed
        if backgroundY <= -500 or playerY < 250:
            playerY += playerSpeed
        if playerY >= 485:
            playerY = 485 # THIS SEEMS TO BE WHERE THE ISSUE IS

    if keys[pygame.K_a]:

        playerCurrentImg = playerLeftImg # Changes current image to correct image

        if playerX == 250:
            if backgroundX < 0:
                backgroundX += playerSpeed
        if backgroundX >= 0 or playerX > 250:
            playerX -= playerSpeed
        if playerX <= 0:
            playerX = 0

    if keys[pygame.K_d]:

        playerCurrentImg = playerRightImg # Changes current image to correct image

        if playerX == 250:
            if backgroundX > -500:
                backgroundX -= playerSpeed
        if backgroundX <= -500 or playerX < 250:
            playerX += playerSpeed
        if playerX >= 485:
            playerX = 485

    if keys[pygame.K_f]: # TROUBLE SHOOTING KEY
        print(playerX, playerY, "\n", backgroundX, backgroundY)
        time.sleep(1)

    ### KEYBOARD INPUTS AND MOVEMENTS ###

    screen.blit(testMap, (backgroundX, backgroundY))
    screen.blit(playerCurrentImg, (playerX, playerY))

    pygame.display.update()
pygame.quit()
quit()
Elfed
  • 23
  • 4

1 Answers1

2

A case of a single symbol.

The critical code is this:

 if keys[pygame.K_w]: # Checks if "w" is pressed

    playerCurrentImg = playerFrontImg # Changes current image to correct image

    if playerY == 250:
        if backgroundY < 0:
            backgroundY += playerSpeed
    if backgroundY >= 0 or playerY > 250:
        playerY -= playerSpeed
    if playerY <= 0:
        playerY = 0

It is meant to be playerY <= 250 and it is also best pratices to use and instead of nested ifs. Your code should look like this.

    if keys[pygame.K_w]: # Checks if "w" is pressed

    playerCurrentImg = playerFrontImg # Changes current image to correct image

    if playerY <= 250 and backgroundY < 0:
        backgroundY += playerSpeed
    if backgroundY >= 0 or playerY > 250:
        playerY -= playerSpeed
    if playerY <= 0:
        playerY = 0
Steven L
  • 46
  • 6