python newbie
python newbie

Reputation: 275

How to add a boundary on areas inside the screen in pygame

I've been trying to create a game screen but I can't seem to add a boundary around the houses on the screen so that the player doesn't walk over them. Below is my code

import pygame
import sys
from pygame import mixer

pygame.init()

playerImg = pygame.image.load('player.png')
WalkFront = [pygame.image.load('B1.png'), pygame.image.load('B2.png'),pygame.image.load('B3.png'),
         pygame.image.load('B4.png')]
WalkBack = [pygame.image.load('F1.png'), pygame.image.load('F2.png'), pygame.image.load('F3.png'),
        pygame.image.load('F4.png')]
WalkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
         pygame.image.load('R4.png')]
WalkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
        pygame.image.load('L4.png')]
walkcount = 0
clock = pygame.time.Clock()


scr = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Pokemon: Red')

logo = pygame.image.load('logo.png')
pygame.display.set_icon(logo)

background = pygame.image.load('BG.png')
pallet = pygame.image.load('pallet town.png')

mixer.music.load('Start menu.mp3')
mixer.music.play(100, 0, 0)


playerX = 200
playerY = 200
up = False
down = False
left = False
right = False


def redrawgamewindow():
    global walkcount
    scr.fill((0, 0, 0))
    scr.blit(pallet, (60, 0))
    if walkcount + 1 >= 29:
        walkcount = 0
    if up:
        scr.blit(WalkFront[walkcount // 7], (playerX, playerY))
        walkcount += 1
    elif down:
        scr.blit(WalkBack[walkcount // 7], (playerX, playerY))
        walkcount += 1
    elif left:
        scr.blit(WalkLeft[walkcount // 7], (playerX, playerY))
        walkcount += 1
    elif right:
        scr.blit(WalkRight[walkcount // 7], (playerX, playerY))
        walkcount += 1
    else:
        player(playerX, playerY)

    pygame.display.update()


def player(x, y):
    scr.blit(playerImg, (x, y))


def start_menu():
    while True:
        scr.fill((255, 255, 255))
        scr.blit(background, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                game()

                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()

            pygame.display.update()


def game():
    global playerX
    global playerY
    clock.tick(12)
    mixer.music.pause()
    mixer.music.load('pallet_music.mp3')
    mixer.music.play(100)
    playerX_change = 0
    playerY_change = 0
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
   
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    global up
                    global down
                    global left
                    global right
                    up = True
                    down = False
                    playerY_change = -0.8
                elif event.key == pygame.K_DOWN:
                    up = False
                    down = True
                    playerY_change = 0.8
                else:
                    up = False
                    down = False
                    walkcount = 0
                if event.key == pygame.K_LEFT:
                    playerX_change = -1
                    left = True
                    right = False
                elif event.key == pygame.K_RIGHT:
                    playerX_change = 1
                    right = True
                    left = False
                else:
                    left = False
                    right = False
                    walkcount = 0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    playerY_change = 0
                    up = False
                    down = False
                    left = False
                    right = False
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    playerX_change = 0
                    up = False
                    down = False
                    left = False
                    right = False

        playerX += playerX_change
        playerY += playerY_change

        if playerX <= 90:
            playerX = 90
        elif playerX >= 670:
            playerX = 670
        if playerY <= 40:
            playerY = 40
        elif playerY >= 540:
            playerY = 540
        redrawgamewindow()


start_menu()

the background image has an area on it with a house in the top left. See the background image below so you can get a better idea. what I want is for the player to not be able to walk on the house so he gets blocked at the edge.

background image

Upvotes: 2

Views: 605

Answers (2)

Rabbid76
Rabbid76

Reputation: 210909

I recommend to define a rectangular area for the house. Use pygame.Rect. You have to find the values for hx, hy, hw and hh:

house_rect = pygame.Rect(hx, hy, hw, hh)

Create a rectangle for the player after the position of the player is changed. The size of the rectangle can be get from the pygame.Surface object which represents the player by get_rect(). The position has to be set by an keyword argument (topleft = (playerX, playerY)).
Use pygame.Rect.colliderect to evaluate if the player collides with the house and restrict the position of the player to the area outside the house:

playerX += playerX_change
player_rect = playerImg.get_rect(topleft = (playerX, playerY))

if player_rect.colliderect(house_rect):
    if playerX_change > 0:
        player_rect.right = house_rect.left
    elif playerX_change < 0:
        player_rect.left = house_rect.right
    playerX = player_rect.x

playerY += playerY_change
player_rect = playerImg.get_rect(topleft = (playerX, playerY))

if player_rect.colliderect(house_rect):
    if playerY_change < 0:
        player_rect.top = house_rect.bottom
    elif playerY_change > 0:
        player_rect.bottom = house_rect.top
    playerY = player_rect.y

Upvotes: 2

Matteo Bianchi
Matteo Bianchi

Reputation: 442

I guess you have to patiently make the boundaries with x and y like this:

if x > boundaryX and y > boundaryY:
    xOfThePlayer -= moving

It is an example you have to change this according to your needs.

Upvotes: 1

Related Questions