[OLPC-Games] Reducing pygame cpu-load to < 4 %

stas zytkiewicz stas.zytkiewicz at gmail.com
Tue Dec 11 02:06:02 EST 2007


On Dec 10, 2007 8:25 PM, Rachid El Guerrab <rachid.el.guerrab at gmail.com> wrote:
> Simulation doesn't depend on user input every frame. So any game that uses
> any simulation code (ball falling, stars blinking, whatever...) will ne cpu
> time even when no user input is present.
>
> It can be minimized a great extent, but not zeroed out.
Indeed, I noticed some years ago that when using a framerate > 30 in
pygame CPU load goes to almost 100%.
The solution is to limit the framerate to 30, then you have almost no
CPU load when doing
nothing while keeping an eventloop running to react to users events.
It would perhaps be easier that activity developers should limit the
amount of CPU wasting animations
and try to limit CPU usage.

In addition to some of the code posted in this thread, here's what I
use in schoolsplay and childsplay,
both pygame based games, for the main eventloop to minimise the CPU load.

....
self.clock = pygame.time.Clock()
....
while self.run_event_loop :
         self.clock.tick(30)# framerate 30 per second
         pygame.event.pump()
         events = pygame.event.get()
         for event in events:
               # check for the event(s) we interested in and call stuff
               break
.....

By using the clock.tick you limit the loop but you still be able to
react on events.
This let childsplay run on even the oldest hardware. (Pentium 1)

Stas

-- 
Free-source educational programs for schools
http://www.schoolsplay.org  and http://wiki.laptop.org/go/Schoolsplay
http://gvr.sf.net and http://wiki.laptop.org/go/Guido_van_Robot


More information about the Games mailing list