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

Re: [pygame] How come when running games with pygame it takes a load of cpu?



On Sun, September 9, 2007 2:50 am, Lamonte Harris wrote:
> It gets anoyying, but w/ my 384mb RAM I didn't expect it to take so much
> cpu.  99% Some times.

Generally, a Pygame program will run as fast as the OS lets it, which
means very high CPU usage. If you want to use less, there are several
methods. One of them is to "import time" and then, in the program's main
loop, call "time.sleep(n)", where n is some fraction of a second. This
method pauses the program in a way that lets other programs run.

A more Pygame-focused method is to use Pygame's Clock class. Offhand I
think it goes like this:

clock = pygame.clock.Clock()
[and then in the main loop:]
clock.tick(desired_frame_rate)

This second method is also good for maintaining a steady framerate --
useful for action games especially. There was some talk on this list
earlier about a different method of maintaining a framerate more precisely
at the cost of much more intense CPU use, but that's not what you want.

If you want to see your program's framerate, use the "time" functions to
get the time at the beginning of the program (or at some other point),
compare that to the time at another point, and divide by the number of
frames drawn.