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

Re: [pygame] Using Numeric & Surfarray with alpha values



On Sat, 2005-11-26 at 23:09 -0500, mr001m wrote:
> Sorry about the lack of explicitness in the last few emails,
> this is all I've been doing for the past couple days and I'm
> clearly leaving things unsaid which are not obvious. Here is
> what's going on, laid out for ya:
> http://pastebin.com/439375


Ah, I've got it now. First, let's get your problem fixed. The problem is
that Numeric is very particular about data types. It does not allow
assigning integer arrays into byte arrays. That is what is trying to
happen in your code, the integer factor can't be assigned into the byte
alpha array. You can make a single item array and then use any Numeric
type on it, this will allow you to divide by a "byte". 

factor = int(round(particle.decay / particle.life))
factor = Numeric.array(factor, Numeric.UInt8)
pygame.surfarray.pixels_alpha(newimg)[:] /= factor

Note that factor looks kind of wrong, it will always be "0" after
calling int() ?


That said, this code is going to be a performance problem for you. You
are copying images and doing a lot of work for each particle. Also,
using per-pixel particle sprites may be a bit overkill, but if it is
better, here's what I would do. I would pre-render an array of maybe 16
sprite images. Each image gets a fainter alpha. Then in your particle
loop, you pick one of the pregenerated images.


particleImages = []
for i in range(16):
    newImage = originalImage.convert_alpha()
    factor = 1.0 - (i / 16.0)
    factor = Numeric.array(factor * 255, Numeric.UInt8)
    pygame.surfarray.pixels_alpha(newImage)[:]
    particleImages.append(newImage)
    int(round(particle.decay / particle.life))


Then in your rendering loop...


for particle in particleEmitter.particles:
    frame = int(particle.decay / particle.life * 15.0)
    image = particleImages[frame]
    pos = image.get_rect(center=(particle.x, particle.y)
    self.fieldSurface.blit(image, pos)


I still say you can do some tricks to convert these into non-per-pixel
sprite images. Can you put your sprite image up somewhere to see? If you
want the code for that let me know. It should speed things up a bit on
the rendering.