[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] timer



On Fri, 2006-06-02 at 16:39 -0400, Chuang Wu wrote:
> I wanna timer the game in 90 seconds. If player couldn't finish the
> game in 90s, it'll game over.
> 
> Anyone knows howto do that?

The simple way would be create a timer event. When the event appears,
you know the player is out of time. But this isn't great because there
is no way to know how much time is left.

The best way is to get the current time when the level starts. Every
frame you get the current time and see how long it has been since the
level started. If it's past the timeout you stop the game. Otherwise you
can show the time to the user.

def play():
    pygame.init()
    level = loadLevel("level.data")
    timeStarted = pygame.time.get_ticks()
    while 1:
        nowTime = pygame.time.get_ticks()
        levelTime = (nowTime - timeStarted) / 1000.0
        #  Divide by 1000 to convert milliseconds to seconds
	if levelTime > 90.0:
	    level.timeup()
            break
        level.updateFrame(levelTime)