Reducing pygame cpu-load to < 4 %

Roberto Fagá robertofaga at gmail.com
Sun Dec 9 18:35:02 EST 2007


Hello Chris

Nice work! Anyway, you are only locking in the event manager, this is
great to games which doesn't need to render each frame.
A good option to use, when you need to process something in the
background, is to use the pygame.time.Clock(), like:

MAXFPS = 20 #for example
clock = pygame.time.Clock()

while loop:
    # get events without wait method to don't prejudice the other process
    # render the current frame, process other stuffes like animations
    clock.tick(MAXFPS)


Another good option to optimize games in PyGame on XO is to reduce the
depth of the colors on:
window = pygame.display.set_mode((400, 225), 16)

16 bits is good to my use, but don't use more than 24 bits, only if
you really need to use the alpha channel graduated (for <=24bits, you
can use color_key to transparency).

Roberto

On Dec 9, 2007 9:26 PM, Chris Hager <chris at linuxuser.at> wrote:
> Hi!
>
> Mcfletch and I have been talking about the heavy cpu-load of the wiki's
> pygame demo: 99%; and how to reduce it.
>
> We figured out, a combination of pygame.event.wait() and
> pygame.event.get() very good. That's the demo code; it takes less than
> 4% cpu (0.3% - 1.7% here):
>
> import sys
> import pygame
> from pygame.locals import *
>
> def main():
>         window = pygame.display.set_mode((400, 225))
>         pygame.event.set_blocked(MOUSEMOTION)
>         pygame.init()
>
>         while True:
>                 for event in [ pygame.event.wait() ] + pygame.event.get( ):
>                         print event
>                         if event.type == KEYUP:
>                                 # Quit on 'q'
>                                 if event.key == 113: sys.exit(0)
>
> if __name__=="__main__":
>     main()
>
>
> # set_blocked(MOUSEMOTION) has very little influence on the cpu-load,
> # but the output is more focused on the other events.
>
> - chris
>
>
> _______________________________________________
> Devel mailing list
> Devel at lists.laptop.org
> http://lists.laptop.org/listinfo/devel
>



More information about the Devel mailing list