Football Pygame, Need Help On Timer
Solution 1:
I would suggest using pygame.time.set_timer()
and pygames event mechanics since you already likely have an event processing loop in your game. See the set_timer docs here.
You do something like this:
pygame.time.set_timer(pygame.USEREVENT, 10000)
to start the a 10 second timer that will trigger a pygame.USEREVENT when it goes off. You would watch for that event in the event loop by adding a test like this:
ifevent.type == pygame.USEREVENT:
# Countdown expired
I doubt that you require it, but if you need multiple timers and need to be able to tell them apart, you can create an event with an attribute that you can set to different values to track them. Something like this:
my_event = pygame.event.Event(pygame.USEREVENT, {"tracker": "gameover"})
pygame.time.set_timer(my_event , 2000)
Solution 2:
Get the start time from the functionpygame.time.get_ticks()
. This gives you the current time (since pygame started) in milliseconds. Store this somewhere.
MAX_EVADE_TIME = 10000# millisecondsplayer_start_time = pygame.time.get_ticks()
Then during the main loop, continually look at the time, and work out if the limit has passed.
while not exiting:
...
time_now = pygame.time.get_ticks()
# is the time limit used-up?if ( time_now > player_start_time + MAX_EVADE_TIME ):
doGameOver() # or whtever
Solution 3:
You can use the time module (click here for the docs). Here's a program that would print "I'm a replacement for whatever you do in pygame!" until 5 seconds since the start has passed
import time
start = time.time()
seconds = 5whilenot time.time() >= start + seconds:
print("I'm a replacement for whatever you do in pygame!")
print(str(seconds)," seconds is up!")
Post a Comment for "Football Pygame, Need Help On Timer"