Trying to delay a specific function for spawning enemy after a certain amount of time

In pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue.. The time has to be set in milliseconds:

pygame.time.set_timer(pygame.USEREVENT, 1000) # 1 second

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case the value of pygame.USEREVENT is the event id for the timer event.

Receive the event in the event loop:

running = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

         elif event.type == pygame.USEREVENT:
             # [...]

The timer event can be stopped by passing 0 to the time argument of pygame.time.set_timer.

See also Spawning multiple instances of the same object concurrently in python.


Create a list of moles and add a random position to the list in mole_spawn_easy:

moles = []

def mole_spawn_easy():
    molex = random.randint(50, 950)
    moley = random.randint(450, 682)
    moles.append((molex, moley))

Draw the moles in the main application loop:

while run:
    # [...]

    for pos in moles:
        screen.blit(mole, pos)

See the example:

moles = []

def mole_spawn_easy():
    molex = random.randint(50, 950)
    moley = random.randint(450, 682)
    moles.append((molex, moley))

pygame.time.set_timer(pygame.USEREVENT, 1000)

while run:
    
    ax, ay = pygame.mouse.get_pos()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.USEREVENT:
            counter -= 1
            text = ("Time Left: " + str(counter)).rjust(3)
            if counter > 0:
                mole_spawn_easy()
            else:
                print("game over")

    screen.blit(background, [0,0])
    
    for pos in moles:
        screen.blit(mole, pos)
    screen.blit(aim, ((ax - 32 ),(ay - 32)))
    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    
    pygame.display.flip()
    clock.tick(FPS)

Leave a Comment