How to keep a sprite within boundaries in Pygame?

401 views Asked by At

I am looking on how to keep my sprite within the set boundaries of a window in Pygame. Could anyone here please help me keep the car sprite within the lines at all times? Thanks! (please don't come to edit my question, actually help me!)

import pygame

pygame.init()
screen = pygame.display.set_mode((300,208))
pygame.display.set_caption("TinyRacer")
car = pygame.image.load("car.png")
bg = pygame.image.load("bg.png")
run = True
y = 84

while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    key = pygame.key.get_pressed()
    if key[pygame.K_UP]:
      y -= 16
    if key[pygame.K_DOWN]:
      y += 16
    screen.fill((255,255,255))
    screen.blit(car, (0,y))
    screen.blit(bg,(0,0))   
    pygame.display.update() 
    
pygame.quit()

I have tried following Techwithtim's tutorial on this, but to no avail.

my_game

1

There are 1 answers

0
furas On

If you would keep position and size in pygame.Rect() then you could use special functions to check collisions.

Or simply check

car_rect.top < screen_rect.top and screen_rect.bottom < car_rect.bottom

Or you could use contains to check if one rect is fully inside another.


I create green background which is smaller than window, and I use Rect() to check if car in inside this green region.

        car_rect.y -= 16

        if car_rect.top < bg_rect.top:
            car_rect.top = bg_rect.top

I also use Rect() to put elements in center of screen.

car_rect.centerx = screen_rect.centerx
car_rect.centery = screen_rect.centery

The same way you can put other elements (i.e. text in center of button)

To make it simpler I use surfaces instead of images so everyone can simply copy and run it.

enter image description here

import pygame

pygame.init()

screen = pygame.display.set_mode((300,208))
screen_rect = screen.get_rect()
pygame.display.set_caption("TinyRacer")

#car = pygame.image.load("car.png")
car = pygame.Surface((20,10))
car.fill((255,0,0))
car_rect = car.get_rect()
car_rect.centerx = screen_rect.centerx
car_rect.centery = screen_rect.centery
         
#bg = pygame.image.load("bg.png")
bg = pygame.Surface((300,100))
bg.fill((0,255,0))
bg_rect = bg.get_rect()
bg_rect.centery = screen_rect.centery

clock = pygame.time.Clock()         
run = True

while run:
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
         
    key = pygame.key.get_pressed()
    
    if key[pygame.K_UP]:
        car_rect.y -= 16
        if car_rect.top < bg_rect.top:
            car_rect.top = bg_rect.top
    if key[pygame.K_DOWN]:
        car_rect.y += 16
        if car_rect.bottom > bg_rect.bottom:
            car_rect.bottom = bg_rect.bottom
         
    screen.fill((255,255,255))
    screen.blit(bg, bg_rect)   
    screen.blit(car, car_rect)
    pygame.display.update() 
    
    clock.tick(25)  # 25FPS (it gives 40ms delay)
         
pygame.quit()