PyGame border collision detection not working

Viewed 233

I am new to pygame and I made a screen border detection but it doesn't work

screen = pygame.display.set_mode([854, 480])

and to check if its touching the edge

pressed = pygame.key.get_pressed()

if playerY > 0:
    if pressed[pygame.K_w]: playerY -= 1
if playerY > 854:
    if pressed[pygame.K_s]: playerY += 1
if playerX > 0:
    if pressed[pygame.K_a]: playerX -= 1
if playerX > 480:
    if pressed[pygame.K_d]: playerX += 1

but if we use it breaks / the player gets stuck

and the entire code is

import pygame

pygame.init()

screen = pygame.display.set_mode([854, 480])

running = True

playerX = 70
playerY = 400

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

    pressed = pygame.key.get_pressed()

    if playerY > 0:
        if pressed[pygame.K_w]: playerY -= 1
    if playerY > 854:
        if pressed[pygame.K_s]: playerY += 1
    if playerX > 0:
        if pressed[pygame.K_a]: playerX -= 1
    if playerX > 480:
        if pressed[pygame.K_d]: playerX += 1
    
    screen.fill((255, 255, 255))

    player = pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(playerX, playerY, 10, 25))

    pygame.display.flip()
2 Answers

The width of the window is 854 and the height is 480.You have to test if playerY < 480 and playerX < 854. Additionally you should consider the size of the player:

import pygame
pygame.init()

screen = pygame.display.set_mode([854, 480])
running = True
playerX = 70
playerY = 400

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

    pressed = pygame.key.get_pressed()
    if playerY > 0:
        if pressed[pygame.K_w]: playerY -= 1
    if playerY < 480 - 25:
        if pressed[pygame.K_s]: playerY += 1
    if playerX > 0:
        if pressed[pygame.K_a]: playerX -= 1
    if playerX < 854 - 10:
        if pressed[pygame.K_d]: playerX += 1
    
    screen.fill((255, 255, 255))
    player = pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(playerX, playerY, 10, 25))
    pygame.display.flip()

I suggest simplifying the code with move_ip and clamp_ip:

import pygame
pygame.init()

screen = pygame.display.set_mode([854, 480])
player = pygame.Rect(70, 40, 10, 25)

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

    pressed = pygame.key.get_pressed()
    player.move_ip(
        pressed[pygame.K_d] - pressed[pygame.K_a],
        pressed[pygame.K_s] - pressed[pygame.K_w]
    )
    player.clamp_ip(screen.get_rect())    
    
    screen.fill((255, 255, 255))
    player = pygame.draw.rect(screen, (0, 0, 0), player)
    pygame.display.flip()

It should be

pressed = pygame.key.get_pressed()

if playerY > 0:
    if pressed[pygame.K_w]: playerY -= 1
if playerY < 480:
    if pressed[pygame.K_s]: playerY += 1
if playerX > 0:
    if pressed[pygame.K_a]: playerX -= 1
if playerX < 854:
    if pressed[pygame.K_d]: playerX += 1

Edit: @rabbid76 was right! You have swapped your x and y!

Related