0

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

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Editing your question to improve it is actually something that may help get an answer. It would also help it you had made at least some attempt on your own (and posted it in your question). – martineau Mar 26 '22 at 01:34
  • You didn't provide the images, but I assume that the screenshot shows that 'the lines' are two horizontal lines running parallel along the top and bottom of the screen? What have you tried to limit your car's movement? What coordinate needs to be restricted? How could you check if that coordinate is going over some limit? – Grismar Mar 26 '22 at 01:36
  • if you would keep position and size in [pygame.Rect()](https://www.pygame.org/docs/ref/rect.html) then you could use special functions to check collisions. or simply `car_rect.top < screen_rect.top and screen_rect.bottom < car_rect.bottom`. OR you could use [contains](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.contains) to check if one rect is fully inside another. – furas Mar 26 '22 at 05:09

1 Answers1

0

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()
furas
  • 134,197
  • 12
  • 106
  • 148