Skip to content
Advertisement

Creating a Asteroid type game, problem is when player waits to press play button many asteroids spawn instead of controlled amount

About 500 lines of code, so to make it easier to read I believe the bug comes from _update_asteroids(self) function. Simply put, when the user is in the paused game state and must press play, if the player decides to wait lets say a minute. A minutes worth of asteroids will spawn at the exact same time. I believe this is because the pygame.time.get_ticks() function continues to gain ticks while game is paused. Is there any way to reset the ticks or make it so that 100s of asteroids do not spawn when the user decides to wait before pressing play button?

JavaScript

Advertisement

Answer

The spawning of the asteroids depends on the next_object_time. next_object_time is initialized with 0. You need to set self.next_object_time when the play button is pressed:

JavaScript

To make the algorithm more robust you can set next_object_time depending on the current time when an asteroid spawns (this is optional):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement