0

This is a really simple question, and I don't know why I haven't got the answer to it, but does anyone know how to correctly add a timer in pygame for Python 3.4.1?

Here's what I have so far:

texta = font.render("Time:"+str(time), True, black)
screen.blit(texta, [500,100])

I have read solutions using loops and many others, but none have worked so far. I want a timer to be displayed on the screen and count the seconds it takes for the user to perform a certain task.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
Lewis Tough
  • 11
  • 1
  • 1
  • The Two lines wont do the job - you need to write a full attempt, if you can. It really does depend on how your code is structured. – Tony Suffolk 66 Aug 01 '14 at 06:44
  • With what you just wrote, the current time will be displayed to the screen once, and then never change. Here's a question that will get you on the right track. How do you update the variable 'time' repeatedly and render time repeatedly so that it shows the correct time? – DJMcMayhem Aug 02 '14 at 00:04
  • See this: http://stackoverflow.com/questions/10596988/making-a-countdown-timer-with-python-and-tkinter it might help you. – W1ll1amvl Aug 02 '14 at 05:14

2 Answers2

0

Here is a timer you can use

    import pygame, sys
    from pygame.locals import *

    clock = pygame.time.Clock()

    time = 0  #In Seconds

    #GameLoop

    while True:

        milli = clock.tick()  #clock.tick() returns how many milliseconds passed since the last time it was called


        #So it tells you how long the while loop took

        seconds = milli/1000.

        time += seconds

        print time #So you can see that this works
0

You can use jues velarde's code above and just print the time variable onto to your pygame surface using

font = pygame.font.Font(YourFont, YourSizeOfText)
text = font.render(str(time), 1, (YourColour)) # The time variable being in jues's code
DISPLAYSURF.blit(text, (YourXValue, YourYValue)

and you would just call this code in a loop when you want too, in your case, when you want to begin timing the user's task, But remember to run jues's code in your program as well so this will work.

Hope i helped!

Colourfit
  • 375
  • 3
  • 19