title: I am trying to make the obstacles appear on the screen a random distance apart in my game but can't figure out how?
body: <h1>Getting images from the file</h1>
  <pre><code>start = pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoStart.png&quot;))
  dead = pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoDead.png&quot;))
  running = [pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoRun1.png&quot;)),
          pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoRun2.png&quot;))]
  jumping = pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoJump.png&quot;))
  ducking = [pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoDuck1.png&quot;)),
          pygame.image.load(os.path.join(&quot;Images/Dino&quot;, &quot;DinoDuck2.png&quot;))]
  
  small_cactus = [pygame.image.load(os.path.join(&quot;Images/Cactus&quot;, &quot;SmallCactus1.png&quot;)),
                  pygame.image.load(os.path.join(&quot;Images/Cactus&quot;, &quot;SmallCactus2.png&quot;)),
                  pygame.image.load(os.path.join(&quot;Images/Cactus&quot;, &quot;SmallCactus3.png&quot;))]
  large_cactus = [pygame.image.load(os.path.join(&quot;Images/Cactus&quot;, &quot;LargeCactus1.png&quot;)),
                  pygame.image.load(os.path.join(&quot;Images/Cactus&quot;, &quot;LargeCactus2.png&quot;)),
                  pygame.image.load(os.path.join(&quot;Images/Cactus&quot;, &quot;LargeCactus3.png&quot;))]
  
  bird = [pygame.image.load(os.path.join(&quot;Images/Bird&quot;, &quot;Bird1.png&quot;)),
          pygame.image.load(os.path.join(&quot;Images/Bird&quot;, &quot;Bird2.png&quot;))]
  
  cloud = pygame.image.load(os.path.join(&quot;Images/Other&quot;, &quot;Cloud.png&quot;))
  
  bg = pygame.image.load(os.path.join(&quot;Images/Other&quot;, &quot;Track.png&quot;))
  </code></pre>
  <h1>Reading the all time high score</h1>
  <pre><code>def get_saved_high_score():
      try:
          with open('high_score.json') as s:
              return json.load(s)
      except FileNotFoundError:
          return 0
  </code></pre>
  <h1>Making the Pterodactyls and Cactie</h1>
  <pre><code>class Obstacle:
      def __init__(self, image, type):
          self.image = image
          self.type = type
          self.rect = self.image[self.type].get_rect()
          self.rect.x = screen_width
  
      def update(self):
          self.rect.x -= game_speed
          if self.rect.x &lt; - self.rect.width:
              obstacles.pop()
  
      def draw(self, screen):
          screen.blit(self.image[self.type], self.rect)
  </code></pre>
  <h1>Making the small cactie</h1>
  <pre><code>class SmallCactie(Obstacle):
      def __init__(self, image):
          self.type = random.randint(0, 2)
          super().__init__(image, self.type)
          self.rect.y = 325
  </code></pre>
  <h1>Making the big cactie</h1>
  <pre><code>class LargeCactie(Obstacle):
      def __init__(self, image):
          self.type = random.randint(0, 2)
          super().__init__(image, self.type)
          self.rect.y = 300
  </code></pre>
  <h1>Making the pterodactyls</h1>
  <pre><code>class Bird(Obstacle):
      def __init__(self, image):
          self.type = 0
          super().__init__(image, self.type)
          self.rect.y = 260
          self. index = 0
  
      def draw(self, screen):
          if self.index &gt;= 9:
              self.index = 0
          screen.blit(self.image[self.index//5], self.rect)
          self.index += 1
  </code></pre>
  <h1>Displaying the score</h1>
  <pre><code>def display_score():
      global points
  
      text = font.render(&quot;Score: &quot; + str(points), True, (0, 0, 0))
      textRect = text.get_rect()
      textRect.center = (1000, 40)
      screen.blit(text, textRect)
  </code></pre>
  <h1>Displaying the high score</h1>
  <pre><code>def display_high_score(hs):
      text = font.render(&quot;HI: &quot; + str(hs), True, (0, 0, 0))
      textRect = text.get_rect()
      textRect.center = (900, 40)
      screen.blit(text, textRect)
  
  def background():
      global x_pos_bg, y_pos_bg
      image_width = bg.get_width()
      screen.blit(bg, (x_pos_bg, y_pos_bg))
      screen.blit(bg, (image_width + x_pos_bg, y_pos_bg))
      if x_pos_bg &lt;= - image_width:
          screen.blit(bg, (image_width + x_pos_bg, y_pos_bg))
          x_pos_bg = 0
      x_pos_bg -= game_speed
  </code></pre>
  <h1>The main fuction to run the game</h1>
  <pre><code>def main():
      global game_speed, x_pos_bg, y_pos_bg, points, obstacles
      run = True
      death_count = 0
      obstacles = []
      high_score = get_saved_high_score()
  
      while run:
  
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  run = False
  
          screen.fill((245, 130, 100))
          userInput = pygame.key.get_pressed()
          
          background()
          
          player.draw(screen)
          player.update(userInput)
  </code></pre>
  <pre><code>    # Making the odstacles appear on the screen
  </code></pre>
  <pre><code>        if len(obstacles) == 0:
              if random.randint(0, 2) == 0:
                  obstacles.append(SmallCactie(small_cactus))
              elif random.randint(0, 2) == 1:
                  obstacles.append(LargeCactie(large_cactus))
              elif random.randint(0, 2) == 2:
                  obstacles.append(Bird(bird))
  </code></pre>
  <pre><code>    # What happens when the dino hits an obstacle
  </code></pre>
  <pre><code>        for obstacle in obstacles:
              obstacle.draw(screen)
              obstacle.update()
              if player.dino_rect.colliderect(obstacle.rect):
                  with open('high_score.json', 'w') as s:
                      json.dump(high_score, s)
                  pygame.time.delay(1000)
                  death_count += 1
                  points = 0    #Resets the score
                  game_speed = 13
                  menu(death_count)
  
          cloud.draw(screen)
          cloud.update()
  </code></pre>
  <pre><code>    # Incresing the points and the speed of the game
  </code></pre>
  <pre><code>        points += 1
          if points % 100 == 0:
              game_speed += 1
  </code></pre>
  <pre><code>    # Displaying the score
      display_score()
      
      # Checking to see if there is a new high score
      if points &gt; high_score:
          high_score = points
  
      # Displaying the high score
      display_high_score(high_score)
  
      clock.tick(30)
      pygame.display.update()
  </code></pre>
  <h1>The different menu screens</h1>
  <pre><code>def menu(death_count):
      global points, high_score
      run = True
      high_score = get_saved_high_score()
      while run:
          screen.fill((255, 130, 100))
          font = pygame.font.Font('freesansbold.ttf', 30)
  
          # The start screen
          if death_count == 0:
              text = font.render(&quot;Press any Key to Start&quot;, True, (0, 0, 0))
              screen.blit(start, (screen_width // 2 - 20, screen_hight // 2 - 140))
          # The restart/your high score screen
          else:
              text = font.render(&quot;Press any Key to Restart&quot;, True, (0, 0, 0))
              score = font.render(&quot;High Score: &quot; + str(high_score), True, (0, 0, 0))
              scoreRect = score.get_rect()
              scoreRect.center = (screen_width // 2, screen_hight // 2 + 50)
              screen.blit(score, scoreRect)
              screen.blit(dead, (screen_width // 2 - 20, screen_hight // 2 - 140))
          textRect = text.get_rect()
          textRect.center = (screen_width // 2, screen_hight // 2)
          screen.blit(text, textRect)
  
          # Saving the high score when the game is closed
          pygame.display.update()
          for event in pygame.event.get():
              if event.type == pygame.KEYDOWN:
                  main()
              if event.type == pygame.QUIT:
                  sys.exit()
              
  menu(0)
  </code></pre>
  <p>#Code</p>
  <pre><code># Making the Pterodactyls and Cactie
  class Obstacle:
      def __init__(self, image, type):
          self.image = image
          self.type = type
          self.rect = self.image[self.type].get_rect()
          self.rect.x = screen_width
  
      def update(self):
          self.rect.x -= game_speed
          if self.rect.x &lt; - self.rect.width:
              obstacles.pop()
  
      def draw(self, screen):
          screen.blit(self.image[self.type], self.rect)
  
  # Making the small cactie
  class SmallCactie(Obstacle):
      def __init__(self, image):
          self.type = random.randint(0, 2)
          super().__init__(image, self.type)
          self.rect.y = 325
  
  # Making the big cactie
  class LargeCactie(Obstacle):
      def __init__(self, image):
          self.type = random.randint(0, 2)
          super().__init__(image, self.type)
          self.rect.y = 300
  
  # Making the pterodactyls
  class Bird(Obstacle):
      def __init__(self, image):
          self.type = 0
          super().__init__(image, self.type)
          self.rect.y = 260
          self. index = 0
  
      def draw(self, screen):
          if self.index &gt;= 9:
              self.index = 0
          screen.blit(self.image[self.index//5], self.rect)
          self.index += 1
  
  
  # The main fuction to run the game
  def main():
      global game_speed, x_pos_bg, y_pos_bg, points, obstacles
      run = True
      death_count = 0
      obstacles = []
      high_score = get_saved_high_score()
  
      while run:
  
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  run = False
  
          screen.fill((245, 130, 100))
          userInput = pygame.key.get_pressed()
          
          background()
          
          player.draw(screen)
          player.update(userInput)
  
          # Making the odstacles appear on the screen
          if len(obstacles) == 0:
              if random.randint(0, 2) == 0:
                  obstacles.append(SmallCactie(small_cactus))
              elif random.randint(0, 2) == 1:
                  obstacles.append(LargeCactie(large_cactus))
              elif random.randint(0, 2) == 2:
                  obstacles.append(Bird(bird))
  </code></pre>