Search code examples
pythonpygamecollision-detection

Create a border in PyGame


I would like to know how to create a border around my screen to stop my player from getting off screen. Here's what I've done until now.

# Importing Libraries
import pygame
from pygame.locals import *
pygame.init()

# Variable Stockage
color = (0, 0, 0)
x = 385
y = 470
velocity = 12
background_color = (255, 255, 255)
clock = pygame.time.Clock()

# Screen
screen = pygame.display.set_mode((800, 500))
pygame.display.set_caption('Shooter')


# Game Loop
running = True
while running:

    # Setting to 60fps
    clock.tick(60)
    screen.fill(background_color)
    px, py = x, y

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

    # Player
    player = pygame.draw.rect(screen, color, pygame.Rect(x, y, 30, 30))

    # Player Movement
    key_pressed_is = pygame.key.get_pressed()

    if key_pressed_is[K_LEFT]:
        x -= 8
    if key_pressed_is[K_RIGHT]:
        x += 8
    if key_pressed_is[K_UP]:
        y -= 8
    if key_pressed_is[K_DOWN]:
        y += 8

    pygame.display.update()

I tried this line of code but it didn't work

    # Barrier Around The Screen
barrierRect = pygame.Rect(0, 0, 800, 500)
if player.colliderect(barrierRect):
    x, y = px, py

I'm a bit knew so if you don't mind adding some tips to make my programm better, I would be thankful.


Solution

  • Option 1: Test that the barrierRect does not contains() the player rectangle. You must set the player rectangle directly before using it with the current x and y coordinate:

    player = pygame.Rect(x, y, 30, 30)
    barrierRect = pygame.Rect(0, 0, 800, 500)
    if not barrierRect.contains(player):
        x, y = px, py
    

    Option 2: Use clamp_ip to move the player rectangle inside the barrier rectangle:

    player = pygame.Rect(x, y, 30, 30)
    barrierRect = pygame.Rect(0, 0, 800, 500)
    player.clamp_ip(barrierRect)
    x, y = player.topleft
    

    Complete example:

    import pygame
    from pygame.locals import *
    pygame.init()
    
    # Variable Stockage
    color = (0, 0, 0)
    x = 385
    y = 470
    velocity = 12
    background_color = (255, 255, 255)
    clock = pygame.time.Clock()
    
    # Screen
    screen = pygame.display.set_mode((800, 500))
    pygame.display.set_caption('Shooter')
    
    # Game Loop
    running = True
    while running:
    
        # Setting to 60fps
        clock.tick(60)
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        # Player Movement
        key_pressed_is = pygame.key.get_pressed()
        x += (key_pressed_is[K_RIGHT] - key_pressed_is[K_LEFT]) * 8
        y += (key_pressed_is[K_DOWN] - key_pressed_is[K_UP]) * 8
    
        # Player
        player = pygame.Rect(x, y, 30, 30)
    
        # Barrier Around The Screen
        barrierRect = pygame.Rect(0, 0, 800, 500)
    
        # clamp player in barrier
        player.clamp_ip(barrierRect)
        x, y = player.topleft
    
        screen.fill(background_color)
        player = pygame.draw.rect(screen, color, player)    
        pygame.display.update()