title: How can I use an incrementing variable to spawn sprites at random locatons after an interval?
body: <p>I am working on a space defender game where the player has to shoot incoming spaceships. This game does not have levels and should run as long as the player is alive and no enemy ships have made it to the end of the screen. I have created a player sprite and have also made it to where the player can shoot lasers when the spacebar is pressed. I am at the point where I am trying to have the enemy spaceships spawn onto the screen at appropriate intervals.</p>
  <p>Here is the ¨main¨ file in my space defender program. There are other files but they do not pertain to this question. I have attempted to use a modulus method and a counter variable to draw an enemy sprite approximately every five seconds at a random x position. When I ran this program the screen was empty but no errors were detected. Please let me know how to correct this program if possible, and if not please suggest alternative solutions.</p>
  <p>The problem is most likely located in the spawn function or the calling function at the end.</p>
  <pre><code>import pygame, sys
  from player import Player
  from aliens import Alien1
  import random as rand
  
  
  class Game:
  
    def __init__(self):
      
      player_sprite = Player((screen_width/2,screen_height-10),screen_width,screen_height,3)
      self.player = pygame.sprite.GroupSingle(player_sprite)
      self.count = 0
      
      
  
    def run(self):
      self.player.sprite.lasers.draw(screen)
      self.player.draw(screen)
      self.player.update()
  
    def spawn(self):
      alien1_sprite = Alien1((rand.randrange(38,462),50))
      while True:
        self.count += 1
        rem = self.count % 5
        if rem == 0:
          self.alien1 = pygame.sprite.Group()
          self.alien1.add(alien1_sprite)
          self.alien1.draw(screen) 
      
      
  if __name__ == '__main__':
      pygame.init()
      screen_width = 500
      screen_height = 500
      screen = pygame.display.set_mode([screen_width,screen_height])
      pygame.display.set_caption(&quot;Space Defender&quot;)
      
      
      clock = pygame.time.Clock()
      game = Game()
  
      while True:
        for event in pygame.event.get():  
          if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        pygame.display.flip()
        screen.fill((30,30,30))
        clock.tick(60)
        
        game.run()
        game.spawn()
  </code></pre>
  <p>Thank You and Happy Holidays</p>