Going sub-pixel with PyGame

So you are writing a game in PyGame and you come up with the most beautiful particle effect. But when you implement it, you find that the particles seem to jitter between pixels, and it spoils the effect. This is because PyGame can only render sprites at integer coordinates, even if you give it float values. OpenGL or other 3D APIs don't suffer from this because they can apply filtering and effectively render images between pixels (or sub-pixel). But you can't do that with a 2D blitter.

Until now that is! :-) I've written a small module that pre-calculates sub-pixel sprites so that you can render a PyGame surface at fractional coordinates. It's nice and easy to use, and is almost a drop in replacement. Here's an example:

ball = pygame . image . load ( "ball.png" ) ball_subpixel = SubPixelSurface ( ball ) x , y = 100.23 , 60.58323 screen . blit ( ball_subpixel . at ( x , y ), ( x , y ))

It's as fast to blit sub-pixel surfaces as it is to blit regular surfaces, but there is a downside I'm afraid (isn't there always). Because of the pre-calculation, more memory is required to store the sub-pixel surface. The default level of sub-pixel accuracy uses 9X the memory. I recommend using it for small sprites, but not for large images or images that don't move. The code is completely public domain. It requires the Numeric library.

Because of the nature of sub-pixel surfaces you really need to see it moving to appreciate it, but here is a screen-shot of the sample code in the zip.

Update. A few people have commented that a screen shot is pointless. So that you don't have to run the sample code, I have generated an animated gif to demonstrate the effect. The spheres at the the top are sub-pixel, the spheres below that are blitted in the usual manner.