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

[pygame] Optimizing a noise-maker



At the end of each level for my game, the screen is supposed to become
"noisy" (like a TV with no show on). Currently, I'm doing this:

   # plot a surface
   from random import random as r
   sa = surface.set_at
   for x in xrange(_x):
       for y in xrange(_y):
           if int(r() * 10) > 5:
               # plot white pixels
               sa( (x, y), (255, 255, 255))
           else:
               # plot black pixels
               sa( (x, y), (0, 0, 0))

Then, I'm flipping the surface three times to new surfaces, and
finally I animate the four by blitting them one at a time.

This works.

However, the game-screen is pretty big and the plotter-loop is awfully
slow. What I'm doing to speed it up is to plot the pixels on a surface
that's half the size of the full screen (i.e. screen = 640x480,
plot-screen = 320x240), then scaling it up just before using
transform.flip().

This works, too, but the pixels are due to the scale too big so it
doesn't look too god.

What I _really_ want to do instead of faking like this is to plot four
(preferably six-eight) independant screens and animate them all. Using
a set_at()-loop as above is unfortunately too slow for this.

I guess this can be solved using surfarray, but I'm not sure how. I
haven't learned how to use surfarrays yet.

Anyone got a solution?


-- - Rikard.