0
class Sideways:
"""Overall class to manage game assets"""

    def __init__(self):
    """Initialize the game and create game resources"""
    pygame.init()
    self.screen = pygame.display.set_mode((1200, 600))
    self.screen_rect = self.screen.get_rect()
    pygame.display.set_caption("Pew Pew")
    self.bg_color = (204, 255, 255)
    self.ship = Ship(self)
    self.moving_up = False
    self.moving_down = False
    self.moving_left = False
    self.moving_right = False
    self.bullets = pygame.sprite.Group()
    self.aliens = pygame.sprite.Group()


    FIRE_EVENT  = pygame.USEREVENT + 1  # This is just a integer.
    pygame.time.set_timer(FIRE_EVENT, 1000)  # 1000 milliseconds is 1 seconds.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == FIRE_EVENT:  # Will appear once every second.
            self._create_fleet()

I'm trying to spawn another ship in pygame after a certain interval of time.

I just learned about the clock/ticks function from my previous question being directed to another thread that showed me it. Thank you. I tried to incorporate that into my code during this section.

However, when I run it, the game freezes up and crashes as if it's spawning too many things at the same time to handle and overloads. How would I manage to use this function correctly?

  • You can't have any infinite loops in a game. You **must** only have one game loop that handles events, updates the logic and draws to the image. – Ted Klein Bergman Feb 23 '22 at 11:15
  • 1
    Also, you can do this manually (as you're trying to do) or just set a custom event to appear every x millisecond (in which you spawn a ship) with this function: https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer – Ted Klein Bergman Feb 23 '22 at 11:18
  • 1
    Here's an example on using `pygame.time.set_timer`: https://stackoverflow.com/a/63318893/6486738 – Ted Klein Bergman Feb 23 '22 at 11:43
  • Thank you very much. This set_timer function is much easier for me to understand than the manual clock. – codingnewtoworking3 Feb 23 '22 at 12:53
  • I've tried to incorporate it into my program, and the game no longer crashes. However, it does not seem to register the event. – codingnewtoworking3 Feb 23 '22 at 12:54
  • I have edited the original post to reflect my changes. Would you be able to help point out any errors with my thinking here? – codingnewtoworking3 Feb 23 '22 at 12:56
  • Your code is really weird. Why are you checking the events in the initializer? The event loop should be in the game loop. Why are you creating a window and set its title in a class managing assets? And where is your game loop then? Can you show the other question that suggested this because this is plain wrong. – Ted Klein Bergman Feb 23 '22 at 13:54
  • Also, it's generally frowned upon to change the question, especially after you've gotten an answer. In this case, the answer is still applicable, but edits might make the answers incomprehensible. – Ted Klein Bergman Feb 23 '22 at 13:57
  • Adding the window and title in this class was taught by the Python Crash Course book I'm reading. It works well in creating the game so far. In regards to editing the post: It didn't let me add more code in the comments to reflect my changes due to the character limit so I put it up there to ask for more help. The alternative to editing, should should I just create a separate thread to reflect my changes and ask for further assistance? – codingnewtoworking3 Feb 24 '22 at 04:44
  • Thank you again, I have learned a lot by the way. – codingnewtoworking3 Feb 24 '22 at 05:22
  • Okay, I don't know why they did it this way, but generally, you shouldn't initialize pygame, the window and the game in a single class. These are very distinct things that should happen separately, and it breaks the [Single-responsibility principle](https://en.wikipedia.org/wiki/Single-responsibility_principle) of SOLID. And you should only have a single place where you handle events, which should be in the main game loop, not in the `__init__` method. – Ted Klein Bergman Feb 24 '22 at 13:02
  • Regarding your edit. If you ask a question, then all edits should be to clarify or fix typos without changing the underlying meaning of the question. If you get another problem, then that's another question. Otherwise, you could just keep changing the question until your whole game is written for you. And all answers to your previous questions will be invalid and confusing, thus making people just waste their time and effort. In this case, Rabbid's answer is still applicable so it didn't cause problems, but your information in the question is now wrong as _"game freezes up"_ is no longer true. – Ted Klein Bergman Feb 24 '22 at 13:09

1 Answers1

0

You must create the objects in the application loop, but not in a separate loop. This extra loop freezes your system. In general, if you want to control something over time in pygame, you have two options:

  1. Use pygame.time.get_ticks() to measure time and and implement logic that controls the object depending on the time.

  2. Use the timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. Change object states when the event occurs.

For example see Spawning multiple instances of the same object concurrently in python or create clock and do action at intervals and many more similar answers.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174