Python Posts

166 posts tagged with python

I don't often say this, but I miss something from C++ that I can't do in Python. What I miss is unions. I know they can be dangerous and aren't supported consistently across compilers, but damn it, they can occasionally be useful.

I'm working on a 4x4 matrix class (for 3D maths) as part of my Game Objects library. It's something I have done several times in C++, where I create a union of a two dimensional array and four Vector4 classes. That way the matrix is conceptually both a grid of numbers and four vectors, which allows for quite elegant code. For example if I have a CMatrix class that stores the transform for a spaceship and I want to move it forward 2 units, I might use code like this. continue reading…

Many moons ago I implemented Langton Ants on a ZX Spectrum 48K, and I have been fascinated by it ever since. I thought it would be fun to implement it in PyGame.

Langton ants are simple creatures. They live in a grid of squares that can be one of two colors, and follow these two simple rules.

That is all they do. They don't ponder the meaning of life or current affairs, they just check the color of the square they are on and then turn and move. You would think that something this simple would quickly get in to a cyclical pattern, but it turns out that Langton ants like to make crazy complex patterns and don't repeat themselves. Humor me, while I write a Python script to test it.

First thing we need to do is define a few constants for use in the script, as follows. continue reading…

A recent post on reddit.com sparked a debate on the following snippet of C code.

The denizens of reddit have been musing over the the output of the code; is it 13 or 14? It can be either depending on the compiler used - or something else entirely - as the result is undefined, which another denizen noted. Sometimes I wonder if the C language was designed purely to provide challenging job interview questions.

Even though I was a C/C++ programmer for a lot longer than I have been a Python programmer, the entire concept of a valid language construct being undefined is offensive to me. Seriously. It's like month old milk; makes me screw my face up and turn away in disgust. In contrast, Python smells like a mixture of freshly cut grass and new car smell. continue reading…

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!

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. continue reading…

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.

If anyone wants the aforementioned rubber serpent, then let me know. I want to get rid of it before it starts leaving little rubber droppings behind the television. I'll even post it to you for free (UK only). On the condition that you have kids and you're not just a geek with an inner-child.

Now if you will excuse me, I have to get back to day-job, which is something that we adults have to do.

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.

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

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: continue reading…

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

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...