0

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.

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.

The problem is most likely located in the spawn function or the calling function at the end.

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("Space Defender")
    
    
    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()

Thank You and Happy Holidays

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ayoon Butt
  • 21
  • 2
  • 1
    You have infinite loop in your spawn function. –  Dec 29 '21 at 07:21
  • 1
    `while True:` in `spawn` will never end. What are you trying to do there? – Rabbid76 Dec 29 '21 at 07:44
  • Related: [Spawning multiple instances of the same object concurrently in python](https://stackoverflow.com/questions/62112754/spawning-multiple-instances-of-the-same-object-concurrently-in-python/62112894#62112894) and [Trying to delay a specific function for spawning enemy after a certain amount of time](https://stackoverflow.com/questions/61409702/trying-to-delay-a-specific-function-for-spawning-enemy-after-a-certain-amount-of/61410788#61410788) – Rabbid76 Dec 29 '21 at 07:44
  • You might also be interested in creating a custom event: https://coderslegacy.com/python/pygame-userevents/. – The_spider Dec 29 '21 at 11:13
  • so I have removed the while loop why don´t my sprites stay on the screen when I call the draw function? – Ayoon Butt Dec 29 '21 at 21:07

0 Answers0