There's a way to disable a method for a certain amount of time?

Viewed 39

I'm learning pygame and i have a problem with the counter of hit that the enemie gives to the player, it obiviously trigger the method hit() everytime the hitbox collide, but i need to set a sort of cooldown to those hits! So i will surely need a way to use some type of delay in some spot of my code. I already thought about the use of threads, but when the amount of enemies will be larger it would be a problem for the optimization... do you have some answer or useful advices?

1 Answers

Here is a simple and basic way to implement timeouts:

import time

LIMIT_COUNTER = 0

def limited_function():
    global LIMIT_COUNTER
    current_time = int(time.time())
    if current_time - LIMIT_COUNTER < 5: # Timeout: 5 seconds in this case
        return
    LIMIT_COUNTER = current_time
    print("Continue timeouted here")

Related