Search code examples
pythonpygame

How do I create a border in Pygame so my character doesn't walk over an image?


I need a rectangular border around an image so my character stops walking through it. It would be nice if my character stopped at certain coordinates instead of walking over the image.

I've tried to create a border with my 'x' and 'y' coordinates, but the border seems to stretch across the screen.

import pygame
from pygame.locals import *
pygame.init()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT,))
pygame.display.set_caption("Zombie Hunters")

background = pygame.image.load("background.jpg").convert()
background = pygame.transform.scale(background, (SCREEN_WIDTH,SCREEN_HEIGHT))

player = pygame.image.load("character no gun.png").convert_alpha()
player = pygame.transform.scale(player, (270, 270))

# these are the coordinates to move the player
x, y = 0, 0
MOVE_RIGHT = 1
MOVE_LEFT = 2
MOVE_UP = 3
MOVE_DOWN = 4
direction = 0
speed = 1

#House barrier
barrier_xlocation = 345
barrier_ylocation = 80
barrier_width = 190
barrier_height = 260

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

        if event.type == KEYDOWN:
            if event.key == ord('q'):
                pygame.quit()
                exit()

            if event.key == K_LEFT:
                direction = MOVE_LEFT

            if event.key == K_RIGHT:
                direction = MOVE_RIGHT

            if event.key == K_UP:
                direction = MOVE_UP

            if event.key == K_DOWN:
                direction = MOVE_DOWN

        elif event.type == KEYUP:

            if event.key == K_LEFT:
                direction = 0

            if event.key == K_RIGHT:
                direction = 0

            if event.key == K_UP:
                direction = 0

            if event.key == K_DOWN:
                direction = 0

    if(direction == MOVE_LEFT):

        x-= speed

    if(direction == MOVE_RIGHT):

        x+= speed

    if(direction == MOVE_UP):

        y-= speed

    if(direction == MOVE_DOWN):

        y += speed


    #Background
    screen.blit(background, (0, 0))

    #House border
    pygame.draw.rect(screen, (255,0,0), (barrier_xlocation,barrier_ylocation,barrier_width,barrier_height), 2)

    #Player hitbox
    pygame.draw.rect(screen, (255,0,0), (x + 117,y + 105, 50,50),2)
    screen.blit(player, (x,y))
    pygame.display.update()

I don't get any error messages, but I need to create a border around the house.


Solution

  • Use pygame.Rect objects and .colliderect() to check for the collision of two rectangles.

    Store the current position of the player. After the player position has change, check for a collision with the barrier. When the player and the barrier are colliding, then reset the position of the player.

    The size of as [pygame.Surface] object can be get by .get_size():

    # store current position
    px, py = x, y
    
    # change position
    if(direction == MOVE_LEFT):
        x-= speed
    if(direction == MOVE_RIGHT):
        x+= speed
    if(direction == MOVE_UP):
        y-= speed
    if(direction == MOVE_DOWN):
        y += speed
    
    # set player and barrier rectangle
    playerRect = pygame.Rect(x, y, *player.get_size())
    barrierRect = pygame.Rect(
        barrier_xlocation, barrier_ylocation, barrier_width, barrier_height)
    
    # check for collision
    if playerRect.colliderect(barrierRect):
        # reset position
        x, y = px, py