2

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

Chris Rahmé
  • 357
  • 2
  • 6
  • 20
python newbie
  • 275
  • 1
  • 11

2 Answers2

2

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    it did work! but it seems you switched the lesser than and greater than signs in the X axis (first part) of the code, but it was a quick fix, thank you so much I spent like 3 days trying to fix this and tried so many kinds of logics! – python newbie Sep 17 '20 at 11:05
  • 1
    @pythonnewbie I see, you're right. I've fixed that in the answer. – Rabbid76 Sep 18 '20 at 14:09
1

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.

Matteo Bianchi
  • 434
  • 1
  • 4
  • 19