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

Re: [pygame] 100% CPU FAQ



By default, Python/Pygame will run as fast as the OS lets it, which means
100% CPU usage. If you want to avoid that, there are several ways. One is
to create a Pygame Clock object ("clock = pygame.clock.Clock()", I think)
and then, in your main loop, call the clock's tick() function with the
desired framerate, eg. "clock.tick(20)". Another is to call time.sleep(),
which simply makes the program pause without using CPU cycles.

Either of these methods is usable, though if you're using Pygame already,
the Clock object is the better choice because it lets you directly control
the framerate. Either method will allow other programs to run faster,
reduce needless wear on the computer, and (especially useful for laptops)
use less energy.

There's also a method I don't recall offhand for controlling the program's
speed more precisely at the expense of still using 100% CPU, but you
probably don't need it.

Incidentally, if you say "start_time = time.time()" when starting your
program, then get the time when ending, subtract start_time, and divide by
the number of frames drawn, you get an easy FPS counter.