PyGame.Time

Why time is so important? I want video!!

You can be curious why I'm not talking about video, sound, motion first. Answer is very simple: they relay on time!
To start developing complex programs you have to know basics. Yes PyGame allow you to start programing without hours of studying but still you must to know time to do something more then simple showing few pictures on the screen.

Power of Time

You need time to create motion, play sound, react to events in other words to make games. Motion is just illusion put some fancy image on the desk and move it a little every second. Yes it is a motion. Of course in game we will be moving a bit faster but idea is the same.

Another reason…

This is one of the easiest modules in PyGame (just after pygame module).

pygame.time

In computers generally we don't count seconds we counts ticks. One tick can be 0.1 0.01 or 0.001 of second but those ticks are not for you! Computer counts them in special hardware variable. You can relay only on mechanisms that provide Your operating system. In PyGame things are a bit easier. First you can see how much ticks have already have past, so by comparing to of them You know how much time have past. Second You can waits for given period of time. Next You can ask pygame.time to invoke given event after every time you wont. And you can create clocks that can manage FPS, frame per seconds. FPS counts how many times You have updated screen. So pygame.time provide You with every thing You will ever need.
Time in pygame is measured in milliseconds (1/1000 of a second) but on some platforms it can be soundet to 10 milliseconds.

pygame.time.get_ticks

This function returns time that have past since pygame.init, but before invocation pygame.init it will always return 0.

pygame.time.wait

Sometimes we will need to wait certain amount of time. pygame.time.wait will do it. You only must be aware that during that time processor will be scheduled to other programs so You may awake after given time (but never before), if You want more precision following function is for you.

pygame.time.delay

pygame.time.delay() What's the difference? This function will use your CPU heavily, so other programs will suffer a bit. But sometimes precision is more important than that so you can choose.
P.S. try to use pygame.time.wait for normal game or programs, you can always change to pygame.time.delay if you will need optimizations.

pygame.time.set_timer

Another common situation in games (and multimedia apps) is when you must repeat one thing every given period of time e.g. in tetris blocks fall down every second, your hero heal 1HP every second etc.
In pygame you will do it by calling pygame.time.set_timer(eventid, milliseconds). It will invoke event every milliseconds you specified. This event will be available in event queue. What is important you can call this function multiple times with different eventid to have multiple events. When you don't need a particular event to ocure any more just call pygame.time.set_time(eventid_you_want_to_stop, 0).

pygame.time.Clock

This is class that will help us to track amount of time or to manage framerate.

pygame.time.Clock.tick

To track how many frames we have rendered we must call pygame.time.Clock.tick() after computing and rendering graphic on the screen, also calling pygame.time.Clock.tick( number) will order pygame to render only number times per second, so if your rendering takes 10 milliseconds it would normally be rendered 100 times per second but calling pygame.time.Clock.tick(60) will cause rendering only 60 times and then pygame will automaticly call pygame.time.wait(). If you remember it is not so good if you need great precision then following function is better:

pygame.time.Clock.tick_busy_loop

pygame.time.Clock.tick_busy_loop do the same as pygame.time.Clock.tick but uses pygame.time.delay so use heavily CPU but is very precise.

pygame.time.Clock.get_time

When you need time that have passed from last pygame.time.Clock.tick just call pygame.time.Clock.get_time(). You will get time in milliseconds.

pygame.time.Clock.get_rawtime

pygame.time.Clock.get_rawtime is very similar but it doesn't count time spended by pygame.time.Clock.tick() on pygame.time.deley.

pygame.time.Clock.get_fps

Last function in pygame.time is pygame.time.Clock.get_fps that returns current FPS number.