title: The pygame code and the error is mentioned below:
body: <p>Here is a source code of my <strong>space-arcade pygame</strong>. It's a quite simple one and doesn't shoots bullets. Only that we can move the player with the obstacles being moved as well. However, <strong>the limit for the spaceship (player) to move in the screen has been set from x = 0 to x = 765 and from y = 200 to y = 480. But the spaceship somehow cross the limit for y-axis i.e it can move beyond y = 0 and y = 600, but only at some specific x-axis like when x is 10 and 590 something</strong>. <strong>How can i fix this gap</strong> in the following python code:</p>
  <pre><code>import random
  
  pygame.init()
  
  screen = pygame.display.set_mode((800, 600))  
  
  pygame.display.set_caption(&quot;Space Invader&quot;)    
  icon = pygame.image.load(&quot;space-invaders.png&quot;)  
  pygame.display.set_icon(icon)  
  
  background = pygame.image.load(&quot;background.png&quot;) 
  
  player_image = pygame.image.load(&quot;space-invaders.png&quot;)    
  playerX = 370  
  playerY = 480  
  playerX_change = 0
  playerY_change = 0
  
  def player(x, y):
      screen.blit(player_image, (x, y))  
  
  obs_image = pygame.image.load(&quot;cave-painting (1).png&quot;)    
  obsX = random.randint(0, 800)       
  obsY = random.randint(0, 100)        
  obsX_change = 2      
  obsY_change = 0.3      
  
  def obstacle(x, y):
      screen.blit(obs_image, (x, y))  
  
  running = True
  while running:
      screen.fill( (0, 0,0) )  
      screen.blit(background, (0, 0))  
  
      for event in pygame.event.get():  
          if event.type == pygame.QUIT:  
              running = False  
          
          if event.type == pygame.KEYDOWN: 
              if event.key == pygame.K_LEFT:  
                  playerX_change = -4  
              if event.key == pygame.K_RIGHT:  
                  playerX_change = 4 
              if event.key == pygame.K_UP:  
                  playerY_change = -4
              if event.key == pygame.K_DOWN:  
                  playerY_change = 4
  
          if event.type == pygame.KEYUP:  
              if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                  playerX_change = 0  
              if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                  playerY_change = 0
  
      playerX += playerX_change  
      playerY += playerY_change 
  
      # Stopping our player beyond screen:
      if playerX &lt;= 0:
          playerX = 0
      elif playerX &gt;= 765:  
          playerX = 765
      elif playerY &lt;= 200:
          playerY = 200
      elif playerY &gt;= 480:
          playerY = 480
  
      obsX += obsX_change
      obsY += obsY_change
  
      # Movement mechanics for obstacles:
      if obsX &lt;= 0:
          obsX_change = 3
          obsY += obsY_change
      elif obsX &gt;= 736:
          obsX_change = -3
          obsY += obsY_change
    
      player(playerX, playerY)  
      obstacle(obsX, obsY)  
  
      pygame.display.update()```
  
  Here are the source pictures used in the aforementioned code:
  
  [background][1]
  [cave-painting (1)][2]
  [space-invaders][3]
  
    [1]: https://i.stack.imgur.com/EZFLs.png
  
    [2]: https://i.stack.imgur.com/CxNs4.png
  
    [3]: https://i.stack.imgur.com/Z97n7.png  
  </code></pre>