1

I have a game where I need an enemy to spawn after every 1350 milliseconds in a path. And the enemies only start coming after I click on the start button.

Time interval code:

sendEnemy = 0

def enemy_object_creation(sprite_list, health):
    global sendEnemy

    if pygame.time.get_ticks() >= sendEnemy:
        foe = Enemy(sprite_list, health)
        enemies.add(foe)

        sendEnemy += 1350

Now here, when I click on the start button after around 5 secs, a bulk of of enemies come flooding at the beginning and then afterwards they again start coming smoothly. The more time I take to click on the start button, the more enemies spawn together at start. I'm not sure what to do about it.

Help will be appreciated.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
PradMaster
  • 83
  • 10

1 Answers1

2

In pygame the system time can be obtained by calling pygame.time.get_ticks(), which returns the number of milliseconds since pygame.init() was called. See pygame.time module.

You need to calculate the time when the first enemy will have to spawn, when the start button is clicked.

Specify the sendEnemy variable in global namespace:

sendEnemy = 0

Set the initial time value, when the button is clicked:

if clicked:
    global sendEnemy
    current_time = pygame.time.get_ticks()
    sendEnemy = current_time 

Spawn new enemies if sendEnemy is greater than 0, but less than the current time:

def enemy_object_creation(sprite_list, health):
    global sendEnemy

    if 0 < sendEnemy <= pygame.time.get_ticks():
        foe = Enemy(sprite_list, health)
        enemies.add(foe)

        sendEnemy += 1350

If you want to stop enemies from spawning, set sendEnemy = 0.


Alternatively you can compute the time when the next enemy will have to spawn, depending on the current time. However, this leads to a slight inaccuracy as the code does not run on time, but a few milliseconds later:

def enemy_object_creation(sprite_list, health):
    global sendEnemy

    current_time = pygame.time.get_ticks()
    if sendEnemy <= current_time:
        foe = Enemy(sprite_list, health)
        enemies.add(foe)

        sendEnemy = current_time + 1350
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    technically, the very first enemy needs to come just after the button has been clicked – PradMaster Jan 01 '21 at 12:20
  • when I did that the game crashed because all the enemies had spawned together – PradMaster Jan 01 '21 at 12:22
  • sendEnemy = current_time + 1350 seems to be working fine – PradMaster Jan 01 '21 at 12:22
  • actually adding 1350 milliseconds is not effecting the first enemy. it is surprisingly coming at the exact moment when the button has been clicked – PradMaster Jan 01 '21 at 12:31
  • @PradMaster I think you misunderstood my answer. You need to set the first time in the event when the button is clicked, but not in the code provided in your question. The initial value of `sendEnemy` has to be changed. After that `sendEnemy += 1350` is fine. – Rabbid76 Jan 01 '21 at 12:32