Swim Turtle, Swim

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!


Going sub-pixel with PyGame

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.


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

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



April 17th, 2007

The other day I noticed that the words 'kiss' and 'lips' (don't ask) could be spelled on the same buttons when texting on a phone. I idly wondered what other related words could be spelled on identical buttons. That idle wondering festered in my brain and eventually turned in to a Python script (which is often the result of my wonderings). The script I wrote, groups words together by the sequence of phone buttons needed to txt them. It also filters out sequences with just one word, which I wasn't interested in. Here is the code:

txt_words = {}

def to_number(c):
    return "22233344455566677778889999"[ord(c)-ord('a')]

for line in file("word.lst"):
    word = line.rstrip()
    txt_word = "".join(to_number(c) for c in word)
    if txt_word not in txt_words:
        txt_words[txt_word] = []

def cmp_word_lengths(w1, w2):
    return cmp(len(w1), len(w2))

out = file("txtwords.txt", "w")

for txt_word in sorted(txt_words.keys(), cmp=cmp_word_lengths):
    if len(txt_words[txt_word]) == 1:
    print >> out, "[%s] %s" % (txt_word, ", ".join(txt_words[txt_word]))

Next I scanned through the output to find related words. I guess this could be done automatically if I had access to a thesaurus file, but that seemed more trouble that it is worth. Here are a few I picked out...

[24453] AGILE CHILD - A fast moving kid?

[54779] KISSY LIPPY - Pucker up.

[74353] RIFLE SHELF - Were you keep your rifles.

[874353] TRIFLE UPHELD - A British desert, held up.

[766643] POMMIE ROOMIE - Its what Australians call their English room-mates.

In addition to the related words I found, there was also a few combinations that I found amusing, in a low-brow kind of a way.

[28789] BUSTY CURVY - Stop, I'm blushing.

[77265] SPANK PRANK - Don't do it, it's harassment.

[746633] RIMMED SINNED - Pretty sure it's a sin.

I'm tired of looking. If you find any more good ones, let me know!

Additional: You will need the word list if you want to play with the code.


Drum roll, please

April 16th, 2007

With no regard to my personal safety, and no safety net, I perform the amazing grape trick. Do not try this at home.


3,462 miles by back stroke

April 16th, 2007
bigswimMy sister sent me this (hi Jen), it is a lesson in not taking computers literally. According to Google, if you want to go from New York to Edinburgh you must swim across the Atlantic ocean. Nice going egg-heads!

Buttocks in Church!

April 10th, 2007
Cathedral nudes row It must have been a slow news day in Chester yesterday. Front page on the Chester Chronicle is an article about a painting of Adam and Even hanging in the Cathedral. Apparently some locals were grossly offended at the sight of Eve's buttocks and breast. I'm tempted to get all militant atheist here, but I know its just a handful of stuffy prudes. Personally I think Eve looks quite good, and no cellulite in sight! Judge for yourself...

Top 5 tips for working from home

April 9th, 2007

Last week I started a new job as a full time Python developer. Prior to that I worked for a computer games company called  Evolution Studios. Evos - as it is affectionately known by employees - achieved a great deal of success with its latest game, Motorstorm, and it looks like they are set to keep producing hit games. There are a lot of great things about working for a games company, and being a games programmer gets you a lot of respect amongst certain circles (i.e. game geeks). So why give it up? Well over the last few years I have fallen in love with Python and I use it at every opportunity. Consequently I have been idly keeping an eye on the Python job board for Python related work and came across a job that I was perfectly suited for. It listed requirements for skills that I thought were incidental, and the work I would be been doing is not dissimilar to the type of Python coding I do to amuse myself in my spare time. Not sure if I can tell you about it yet, I would have to clear that with the boss. Did I say 'boss'? I meant client, because now I am now self-employed!

It was still a difficult decision though, but working from home swayed me. I can avoid a perilous drive up the M56 murderway and look after myself a little better (crunch time is not good for the waist-line)!

Working form home is not without it's challenges, but I have done it before so I think I know what I have in store for me. Here are my top tips for ditching the rat race and making a living developing software at home. I'll leave out obvious ones, these are tips that were hard lessons to learn.

  1. Get some sun. It's difficult to shake that groggy feeling and start work in the morning. I found that my brain just didn't kick in till after I had gone out for lunch, so I self medicated with ever increasing quantities of caffeine. This was folly. Caffeine is poison. Rich aromatic poison that goes down easy. But poison none the less.  Far better is sunlight. All I had to do was open the damn curtains to trigger some kind of brain chemistry change that made me far more alert. Even in Scotland on a cloudy day there enough UV light to trigger wakefulness.
  2. Stick to a routine. Start work at the same time every day, but more importantly finish work at the same time. If you are working from home you will have more spare time. Use that time to look after yourself. Eat well, get some exercise and have fun. Otherwise you may as well go back to the 9-5.
  3. Tidy the frick up. Working from home is less fun when you are working in a pit of indescribable filth. Dishes should be cleaned within 24 hours (48 at the most) to avoid a health hazard. Rubbish (trash if you are American) must be taken out as soon as a bag is filled. Do not stock pile them until you can't get in to the kitchen. If you are a natural slob like me, you must train yourself to be tidy and hygienic. If you have a tidy friend go to his / her house and mimic what they do.
  4. Take long lunch breaks.Take at least an hour for lunch, 2 hours is even better. It makes the work feel like less of a stretch. It also lets you avoid working during that period after you eat where the body deprives your brain of oxygen because it has moved it to the gut. And if you can walk for a good part of your lunch break you wont get fat. Ever seen a fat postman?
  5. Blog. A new one on me, but I intend to do it more. You can delude yourself that people are remotely interested in what you are working on. It's also a good way to practice writing - an important life skill! And may help you make professional connections for future contracts.
Search for Posts
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan