Python Forum
Trying to make boundries for the pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to make boundries for the pygame
#1
Guys I have a problem is doing the boundaries of my pygame
I am trying to get the character not go above the pygame window

The code in the bottom is my game and all the way at the bottom is where I am trying to do the boundaries I got the sides but I can't manage to get the top and bottom.

import pygame, sys
from pygame.locals import *

import random 

pygame.init()
width, height = 640,480
hbox = 20
vbox = 20
controls =[False, False, False, False]
playerpos =[100,100]
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)


screen=pygame.display.set_mode((width, height))
rect = pygame.Rect(300, 220, hbox, vbox)
player = pygame.image.load("resources/images/pac.png")
grass = pygame.image.load("resources/images/grass.png")
coin = pygame.image.load("resources/images/coin.png")
player = pygame.transform.scale(player, (30,30))
coin = pygame.transform.scale(coin, (20,20))    
l = random.randint(0,480)
k = random.randint(0,640)
n = random.randint(480,640)
walls = (
    pygame.Rect(0, 0, 640, 10),
    pygame.Rect(0, 0, 10, 480),
    pygame.Rect(630, 0, 10, 480),
    pygame.Rect(0, 470, 640, 10),
    )
bar = (
    pygame.Rect(230, 170, 160, 10),
    pygame.Rect(230, 170, 10, 140),
    pygame.Rect(230, 310, 160, 10),
    pygame.Rect(390, 170, 10, 150),
    )

while 1:
    
    screen.fill(0)
    moved = None
    for x in range(width//grass.get_width()+1):
        for y in range(height//grass.get_height()+1):
            screen.blit(grass,(x*100,y*100))
    screen.blit(player, playerpos)

    screen.blit(coin, (l,k))
    
    
    
    for wall in walls:
        pygame.draw.rect(screen, pygame.Color("red"), wall)
    for bars in bar:
        pygame.draw.rect(screen, pygame.Color("red"), bars)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key==K_w:
                controls[0]=True
            elif event.key==K_a:
                controls[1]=True
            elif event.key==K_s:
                controls[2]=True
            elif event.key==K_d:
                controls[3]=True
        if event.type == pygame.KEYUP:
            if event.key==pygame.K_w:
                controls[0]=False
            elif event.key==pygame.K_a:
                controls[1]=False
            elif event.key==pygame.K_s:
                controls[2]=False
            elif event.key==pygame.K_d:
                controls[3]=False
    if controls[0]:
        playerpos[1]-=2
        print(playerpos[0],",",playerpos[1])
        #sleep(1)
    elif controls[2]:
        playerpos[1]+=2
        print(playerpos[0],",",playerpos[1])
    if controls[1]:
        playerpos[0]-=2
        print(playerpos[0],",",playerpos[1])
    elif controls[3]:
        playerpos[0]+=2
        print(playerpos[0],",",playerpos[1])

    if playerpos>=[600,0]:
        controls[3] = False
    elif playerpos<=[10,0]:
        controls[1] = False
    elif playerpos<=[0,-440]:
        controls[2] = False

    
    
        


        if event.type==pygame.QUIT:
            pygame.quit() 
            exit(0) 
Reply
#2
I'd rather not read through all of this code so I'll tell you this.
1. This code is really sloppy, there are lots of tips on making a game with pygame in the pygame tutorials here on this forum, I'd recommend you to go check those out.
2. To keep a player from going outside the screen, it depends on if they're left, right, up, or down, but for up, just check if the second value of they're rect, y-value, is less than 0, if so then set it back to 0. This creates the effect that they can't move beyond the walls of the pygame window.
Reply
#3
Using pygame rect will make this simple. Create area rect and player rect. Move player then clamp_ip player rect.
player.rect.clamp_ip(area) Now the player can't move beyond the window.
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make enemy chase me in pygame? Jan_97 3 4,273 Nov-23-2021, 05:08 PM
Last Post: Jan_97
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,280 Nov-10-2021, 03:38 PM
Last Post: metulburr
  cant make a door automatically close a few seconds after i open it in pygame cooImanreebro 2 2,215 Dec-15-2020, 08:40 PM
Last Post: michael1789
  How can I make music with python/pygame? xBlackHeartx 12 7,068 Oct-15-2019, 08:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020