Showing posts with label pygame. Show all posts
Showing posts with label pygame. Show all posts

Thursday, October 07, 2010

Lost In Translation

I've blogged a few times about ClapTraps, the ingenious free software puzzle game written in PyGame and Python by testpilotmonkey. The last time I blogged about it, I mentioned I was planning to add i18n (internationalisation) and a bit of l10n (localisation) to the game.

Claptraps in British English

In terms of internationalisation, what I wanted to do was to make the game multi-lingual so that on my daughters' computer, where they use Hungarian rather than English, they would be able to play the game in Hungarian.

I had been under the mistaken impression that all I needed to do to prepare a Python script for i18n was to import the gettext library and then surround all translatable strings with the function _().

It turned out to be a little bit more complicated than that. The reason was that I wanted ClapTraps to stay self contained in a single folder rather than force the user to install it.

ClapTraps' self-contained folder structure

Normally when GNU/Linux programs are installed on a computer, the compiled translation scripts are copied to a central location for access by all users of the computer. On Fedora GNU/Linux, that location is /usr/share/locale. Python always expects the compiled translation files to be stored there. However, I wanted my translations to remain in the ClapTraps folder.

It would have taken me quite some time to work out what to do if Mark Mruss had not already solved all the problems for me in his excellent blog post on translating Python/PyGTK programs. There's a fantastic snippet of code there called "Translation stuff" that magically does it all!

The next step, now I had the Python script prepared, was to generate a POT (Portable Object Template) file. The POT file is the template file from which individual translations can be prepared.

The POT file for ClapTraps

It contains a header, then a list which shows the line a translatable string appears in, the id of the translatable string, and a space for the translation.

I found one problem when first creating my POT file was that it only had five strings in it! The reason was that testpilotmonkey had used single quotes for most of his strings but xgettext, the tool you use to create a POT file, only recognises double quotes.

From my POT file, I used the msginit tool to create two PO (Portable Object) files - one for en_GB (British English), the other for hu (Hungarian). Now came the really hard part - the Hungarian translation! I did this myself, but it took me three days until I was happy. The main problem was my difficulty with the imperative mood in Hungarian. Fortunately the excellent website HungarianReference.com helped enormously. My Hungarian is still a bit dodgy, but I like to think that adds to the charm.

Finished PO file with Hungarian translation

Now all I needed to do was compile my PO files in MO files and I could see if it worked. To test the game I used the LANG variable from the command language.

To force the game to run in Hungarian whilst running Fedora 13 in British English I typed LANG=hu python claptraps and....

Magyar and testpilotmonkey - a tricky combination!

Success! That was a lovely feeling. Now the next challenge was a bit of l10n. The problem I faced was that ClapTraps regularly asks the user to press Y for Yes or N for No. But in Hungarian that should be I for Igen or N for Nem. So I needed some way of changing the keys that you need to press to suit the current language.

This turned out to be quite easy - first I had to import the locale library. Next, I just needed to ask the locale class for the initial letters used for "yes" and "no" in a language like this:

# Make lists of "yes" and "no" keys for current language
locale.setlocale(locale.LC_ALL, '')
yes_keys=list(locale.nl_langinfo(locale.YESEXPR)[2:-3]);
no_keys=list(locale.nl_langinfo(locale.NOEXPR)[2:-3]);

The nl_langinfo function returns a regular expression string with the acceptable keys for Yes or No for the current locale. For English it would be:

^[yY].*
^[nN].*

For Hungarian it would be:

^[IiyY].*
^[nN].*

Note that Y is also acceptable for Hungarian. I used the Python slice operator to slice off the bits I didn't want and then bunged the result in a variable. So now, when I want the user to press yes or no I simply do this:

while(1):
    wait_event = pygame.event.wait()
    if wait_event.type == pygame.KEYDOWN:
        if pygame.key.name(wait_event.key) in yes_keys:
        return False
    elif pygame.key.name(wait_event.key) in no_keys:
        program_quit = False
        break 
    else:
        pass

And that solves it. Since I made these changes to ClapTraps my daughters have both had hours of fun out of the game. It's a tribute to the genius of testpilotmonkey - and Richard Stallman. For, without the GPL, I wouldn't have been free to make these changes which allowed my children to enjoy this fantastic piece of software.

Tuesday, September 07, 2010

Distractions...

My children have been nagging me to add internationalisation (i18n) to testpilotmonkey's brilliant game ClapTraps.

Well, the road to good intentions - I got distracted and ended up designing a level.

Not bad for a first attempt

It was supposed to be very cerebral and contain Repton style apple and grass puzzles but ended up as something to rival Giant Clam in Repton 1. Now, better get back to adding some Magyar to ClapTraps before my daughters get back from school today or they won't be amused!

Wednesday, August 04, 2010

ClapTraps v1.1

You might remember that in April I blogged about the computer game ClapTraps, which was written by my friend testpilotmonkey. ClapTraps is a fiendishly addictive puzzle game written in Python using the PyGame SDL library.

ClapTraps v1.1 -  reworked title screen

Back in May I also blogged about my Inkscape reworking of the graphics for the game.

ClapTraps v1.1 - reworked graphics

Since then testpilotmonkey has released a new, expanded version of the game - ClapTraps v1.1. It includes my reworked graphics and a number of new features.

For instance, you can now redefine the control keys. This is very useful if you, like me, have a Hungarian keyboard!

Even better, people who can code in Python can easily change the behaviour the game objects, just as you can in Repton Infinity. To demonstrate this, ClapTraps comes with a XOR-style came called Arrows which has the potential to be very interesting indeed.

Arrows - a real head scratcher

Despite my best efforts to recommend the game to everyone I know, ClapTraps has been largely ignored, even by fans of Repton. I think this is inexplicable and a crying shame. It's an absolutely fantastic puzzle game and has given me and my daughters a lot of fun. It's free as in beer and free as in freedom so there is no reason not to download it and have a go - and let testpilotmonkey know what you think too!

It is available to download for Windows, GNU/Linux and OS X here.


NB for GNU/Linux users: I found I couldn't run the ClapTraps code by double clicking on the main ClapTraps.py icon from Nautilus, the GNOME file browsing program. After much experimentation I found the problem was simply that ClapTraps.py was corrupted in some way. If you copy and paste its contents into a new file it will work correctly. The copy and pasted file will be 2K shorter than the original, but work perfectly.

Sunday, May 16, 2010

Dave Dunce Goes Gloss

At the end of April, I blogged about Claptraps - my friend testpilotmonkey's new computer game. Claptraps is a fiendishly addictive puzzle game written in Python using the PyGame SDL library.

I also noted at the time that Claptraps was a piece of free software - testpilotmonkey released it using Free Software Foundation's General Public Licence or GPL. This means that anyone is free to study or modify the source code and send copies of the game to their friends.

I felt that releasing something as fantastic as Claptraps under the GPL was an act of generosity that should not go unrewarded. As I don't have any money, I decided to donate some of my time instead.

The place I felt best able to help out was the graphics. Claptraps is a tile based game, with 40 x 40 pixel tiles which are stored as 32-bit Portable Network Graphics (PNG) files. testpilotmonkey had decided on a cartoon look for his graphics, with flat fills bounded by black outlines. Here is a screenshot of the game with the testpilotmonkey's original graphics:

Original graphics - Click to enlarge

I love these graphics, as they have bags of character and the animation is very pleasantly squishy and organic. But I thought it might be nice to take these graphics and see if I could make them a bit three-dimensional. To do this I decided to use a piece of free software called Inkscape. The reason I decided to use Inkscape, as opposed to a raster graphics package like The GIMP, was I felt cartoon-ish nature of the graphics lent themselves to be created in vector format. It also meant I could create one set of graphics that could be used at any size.

I made my canvas 40 x 40 pixels in Inkscape, for convenience. As far as guides were concerned, I just used two, one half way vertically and one half way horizontally which marked out the centre of the canvas for me. I imported the original graphics into Inkscape and used the Pen tool to trace over them. For each graphic, I created a separate Inkscape SVG (Scalable Vector Graphics) file.

As I was creating vectors to export to (rather small) bitmaps, there were a couple things I needed to bear in mind when tracing the original graphics to ensure good results. The first thing was I wasn't free to put black outlines anywhere I chose. The end result would be rather grey and muddy due to too much anti-aliasing being used on the exported bitmaps. To keep anti-aliasing to a minimum (and hence keep the graphics as clear and colourful as possible) I needed to ensure that the nodes for my lines and Bezier curves were on half pixel boundaries. The second was to ensure that all my outlines had a one pixel stroke width, and were completely black - sounds obvious, but easy to mess up.

One traced frog - click to enlarge

Once I had an outline for a graphic it was time to colour it in, and again it was Nancy Kominsky to the rescue. I used her technique of using three tones - light, medium and dark to fill each shape. Then, if necessary I'd add any highlights with light yellow and any lowlights with dark purple. The nice thing about Painting Along with Nancy in Inkscape is I don't have to put my spare colours into the fridge and cover them with cling film.

Incidentally, in case you think I'm joking about following Nancy Kominsky's advice, here's the Claptraps apple filled using a radial fill made up of my three tones - light, medium and dark red:

Where's the light coming from Alan?

And here are my three tones I used in the gradient fill editor :

Cadmium orange, vermilion and alizarin crimson...

Here is my purple lowlight and my lemon yellow and white highlight:

That's one teaspoon of purple, the colour...

And here is the finished apple - Nancy Style.

And no need to apply any varnish

And, just for you to compare, here are Nancy's apples:

Picture courtesy Rory Clark

So, after much colouring in but, sadly, no Alan Taylor to chat to while I was working, I had a finished set of graphics:

As seen in Fedora 13 Nautilus

And here are the finished graphics in place in the game:

Click to Enlarge

I was determined when working on the graphics that my input be as invisible as possible - Claptraps has bags of testpilotmonkey's character and I wanted to keep that. The graphics remain very much testpilotmonkey's, just coloured in slightly differently.

After showing testpilotmonkey my graphics, he said he'd like to use them in future releases of the games - which was very kind of him and made all the effort worthwhile. I'm hoping to spend more time on Claptraps and to return to it on this blog soon. In the meantime, why don't you download the game and have a play? You can get hold of it here.

Saturday, April 24, 2010

From Claptrap to Claptraps

Normally I use this blog to drone on about my own claptrap. However, today I'd like to use it to have a look at a friend's.

Level 1 - It's all uphill from here...

To be more precise, a computer puzzle game called Claptraps, written by "testpilotmonkey". It's one of the most joyfully bonkers and imaginative games I've ever played - the sort of thing Oliver Postgate would have come up with if he'd have been given a book about scripting languages.

The game itself was written in the language Python with the help of the wonderful PyGame SDL library.  If you've ever want to write a computer game for GNU/Linux, Windows, or Mac OS this combination is the place to start and indeed Claptraps will work beautifully on all three of these platforms and doubtless several more besides.

I think this game is really rather special for lots of reasons:
  • it is released under the Free Software Foundation's General Public Licence or GPL. That in itself is a marvellous and selfless thing to do, particularly when you consider how much hard work has gone into the game.
  • the quality of the music testpilotmonkey has composed to include in the game - I really enjoy listening to it.
  • the graphics, which have a wonderful cut-out animation feel
  • and, most of all, the standard of the puzzles. The game design is incredibly clever and the puzzles are fiendish but solvable with thought. That makes it one of the most rewarding games I've ever played.
I could talk about the game at length here, but why not download it yourself from testpilotmonkey's blog?

The game has come on in leaps and bounds since this video was made of an early demo version, but it's worth a look:


The gameplay is loosely inspired by the games in the Repton Infinity suite with dashes of Bonecruncher and XOR. However it has a personality all of its own, something which you don't get in modern games written by huge teams.

I'm hoping to do some work of my own on Claptraps if I get the chance over the coming weeks - in true GPL spirit I've been studying and modifying bits and bobs already which I hope to start sharing soon. The code is very, very clearly written and I'm looking forward to having a proper dabble with it enormously.

NB: As the game is still a work in progress, GNU/Linux users may find they need to append two lines to ClapTraps.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Also, don't forget to right click on the Claptraps.py in the file browser, and go to PropertiesPermissions and check the Execute check box to Allow Executing File as Program. You'll also need to make sure you have the pygame package installed on your computer - but if you've installed GNU/Linux in the first place I'm sure you can manage this.

Windows users can just click on the .exe. I know nothing at all about Apples apart from the fact that Granny Smiths are my favourite and my mother's bramley crumble with custard is delicious.