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()