Python Forum

Full Version: Trying to make boundries for the pygame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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) 
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.
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.