[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Using Numeric & Surfarray with alpha values
We are writing a 2D particle engine and we would like to render each
particle with gradually decreasing alpha values. However, our original
image for the particles has per-pixel alpha, so we can't use set-alpha.
So, we are going to have to modify each pixel's alpha value ith Numeric,
it seems.
We have a value for the time left for the particle to live, named
lifetime, and a value for how long it will live, named decay. The value
(lifetime/decay) is the percent of the original alpha value we want to
render each pixel with. So, if a pixel in the original image has alpha
200, and a particular particle has lifetime 50 and decay 100, we would
want to render that pixel in that particle with an alpha of 200 *
(50/100) = 100.
We are trying to do this with numeric & surfarray, but are having
trouble with array types. The best we've been able to do is to create a
list comprehension which does the job:
[[pixel and (pixel * particle.life / particle.decay) for pixl in row]
for row in array]
This uses short circuit evaluation and the fact that booleans return the
last object they looked at to return either the pixel's alpha value if
it is 0, or the pixel's alpha value times our percentage if it is not 0.
array is pygame.surfarray.array_alpha(original_image)
SO, my question is: What is the best way to translate the above list
comprehension into Numeric, so as to speed it up?
Thanks,
Mike Rotondo