1

I have tried many sites but still cannot come up with anything. All online tutorials are not helpful. I have just started learning python and every time I try to move it goes out of the border. I have tried basically all online guide and came across an error...........................


pygame.init()

screen_width = 800
screen_height = 600

window = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Test')

bg_color1 = (135,142,142)  #MAIN BG COLOR
bg_color2 = (255,0,0)     #red
bg_color3 = (255,255,0)   #yellow

clock = pygame.time.Clock()
crashed = False
UFO = pygame.image.load('ufo.png')
rect = UFO.get_rect()
obstacle = pygame.Rect(400, 200, 80, 80)


def car(x, y):
   window.blit(UFO, (x, y))


x = (screen_width * 0.45)
y = (screen_height * 0.8)
x_change = 0
car_speed = 0
y_change = 0

while not crashed:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           crashed = True

       ############SIDE TO SIDE################
       if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_LEFT:
               x_change = -5
           elif event.key == pygame.K_RIGHT:
               x_change = 5
       if event.type == pygame.KEYUP:
           if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
               x_change = 0
       ###########UP AND DOWN#################
       if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_UP:
               y_change = -5
           elif event.key == pygame.K_DOWN:
               y_change = 5
       if event.type == pygame.KEYUP:
           if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
               y_change = 0
   ##
   x += x_change
   y += y_change
   ##
   window.fill(bg_color1)
   car(x, y)

   pygame.display.update()
   clock.tick(100)

pygame.quit()
quit()
  • I don't understand what part of this doesn't make sense to you. I'd think you'd just have code like `if x > max_x: x = max_x` and such. What's difficult to understand about that? And what did you mean by running into errors? What have you tried so far? – Random Davis Jan 05 '21 at 21:40
  • *"I have tried basically all online guide and came across an error"* - what error? I don't see any bounds checking code here. – 0x5453 Jan 05 '21 at 21:40
  • Hey! Welcome to SO. Try to a bit more descriptive in your questions. Providing a code showing your effort is great, but try to introduce us to your problem – Voodu Jan 05 '21 at 21:42

1 Answers1

0

Check that the coordinate is less than 0 or greater than the width or height of the screen. Limit the coordinate if it is out of bounds:

while not crashed:
   # [...]
 
   x += x_change
   if x < 0:
       x = 0
   elif x > screen_width - UFO.get_width():
       x = screen_width - UFO.get_width()

   y += y_change
   if y < 0:
       y = 0
   elif y > screen_height - UFO.get_height():
       y = screen_height - UFO.get_height()

You can simplify the code using the min and max functions:

while not crashed:
   # [...]

   x = max(0, min(screen_width  - UFO.get_width(),  x + x_change))
   y = max(0, min(screen_height - UFO.get_height(), y + y_change))

Another option is to use pygame.Rect objects and clamp_ip. See clamp_ip:

moves the rectangle inside another, in place

while not crashed:
   # [...]

   x += x_change
   y += y_change

   border_rect = window.get_rect()
   ufo_rect = UFO.get_rect(topleft = (x, y))
   ufo_rect.clamp_ip(border_rect)
   x, y = ufo_rect.topleft
Rabbid76
  • 202,892
  • 27
  • 131
  • 174