Archive for the ‘PyGame’ Category

3D Lissajous in PyGame

Monday, May 7th, 2007

Okay, I would be the first to admit that I am getting carried away with these little PyGame experiments. This one renders 3D lissajous figures, which are kind of like a 3D spirograph drawings. It’s the first time I have use Game Objects in anything other than a test, and is a good example of simple Vector and Matrix math. Enjoy!

Download Spiro

lissajous3d

PyGame Runtime?

Sunday, May 6th, 2007

Pygame is a wonderful API for creating games and rich media applications, but the diffulty in distributing PyGame creations may be holding it back from the big time. When you write a game / application / demo, the first thing you want to do is have your friends and family play it, then you want to launch it on the internet, and perhaps even profit from it. To distribute a PyGame app you currently have two choices. You could ask people to install Python and Pygame, plus any other modules you may have used. Which is fine for other Python programmers, but your grandmother may find it tricky - “Are you sure I should click on the snake, dear - what if it has a virus?”. The other option is to build a package with Py2exe or similar tool, which works great but requires a little effort to get working and will generate a large bundle of files - and then you are left with the tedious task of making an installer.

I think this lack of easy distribution is preventing many amazing PyGame creations from getting ‘out there’. What I would love to have is the ability to zip up a bundle of .py files and assets, rename the zip to .pyg then have a PyGame runtime launch the game. This runtime would be available on the net somewhere and have install packages for all the major platforms. So .pyg packages would be “write once, play anywhere” games.

No-doubt there be a number of challenges in implementing something like this, but I’m sure it is doable. The runtime may have to support multiple versions of Python; at least 2.4 and 2.5. It would also have to contain a number of modules that are popular in games, and know how to get modules that aren’t installed - possibly with import hooks and cheeseshop.

I know what you are thinking; if it is such a good idea then why don’t you do it yourself? I may eventualy try it, but I’m still working on other good ideas at the moment. I guess I was hoping that some inspired individual would pick it up and go with it. Looking at Movable Python may be a good starting point.

Ultimately there may even be profit to be made from such a thing. Make the runtime a free download, but sprinkle the site with a few google ads, and perhaps even charge for some of the more professional games. Casual games are big right now, and PyGame programmers would have a big advantage over the poor schlobs working on C++!

Infinite sprites with PyGame

Wednesday, May 2nd, 2007

The one thing that has always bugged me about sprites in PyGame is that they are so damn finite. Why should I be restricted to an upper bounded number of sprites in my game? Fortunately, I figured out a way of breaking the finite sprite limit, and render an infinite number of sprites to the screen - without any reduction in the frame rate!

Now before you try to convince me this is impossible with such arguments that as “an infinite number of sprites requires an infinite amount of memory”, bare in mind that I consider the laws of mathematics to be more rules than actual laws. And rules were meant to be broken, as they say. Not convinced? Download and run this demo, and tell me when you run out of memory. Requires Python and PyGame.

Download Infinite Sprites

infinitesprites

Swim Turtle, Swim

Monday, April 30th, 2007

I recently posted a path finding demo that uses a breadth-first algorithm to route a sprite around a map. Kamilche has taken the basic idea and created a new demo with many improvements. Fugu has been replaced with an animated turtle that uses the A* algorithm to do the path finding. A* (pronounced “A star”) generally performs better than breadth-first search because it directs the search towards the target, rather than have it expand evenly.

Download Fugu’s Friend.

Kamilche is the author of Incarnation, an impressive RPG written in Python & PyGame. I highly recommend checking it out!

fugusfriend

Going sub-pixel with PyGame

Wednesday, April 25th, 2007

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.

Download subpixel.zip

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.

subpixel

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.

Swim Fugu, Swim

Friday, April 20th, 2007

I have written a demonstration of a simple path-finding algorithm (in Python), for use in grid-based computer games. It used a breadth-first search to route a sprite from one part of a map to another, avoiding obstacles in between. The code is a listing taken from my forthcoming book, Beginning Game Development with Python and Pygame. There’s no documentation I’m afraid. If you want to know how it works, you will have to buy the book. :-)

Download path finding demo

To run it, you will need PyGame, and of course Python. The code is completely public domain, use it for whatever purpose you want and you don’t owe me a penny. Here be a screenshot…

fuguswim


Close
E-mail It
Socialized through Gregarious 42