Road Rage – n900 game

The Lappeenranta University of Technology (LUT) organized 20th summer school on telecommunications during the last week. I was there arranging it and running a 24 hour codecamp. The theme of the summer school was “Supporting independent living with technology”. We had a number of seminaries related on the theme and at the end of the week we had a 24 hour codecamp. During the codecamp the students were introduced to the Nokia N900 smart phone that runs the newest Maemo operating system. The good side of the OS is that you can use Python to quickly develop applications and test them. This was the case on the one-day codecamp – Program something that supports independent living at home and do it quickly (and dirty :-P).

Even though I was helping people out most of the time and programming some harder parts (like the very evil how-to send an SMS), I still had time to do something own. As most of the readers know, I am a big game aficianado. Thus, I had to do a game 😛 All I had was a few dark hours of the night. I decided to use pygame as the library to program with and away I went!

An Idea?

Okay… Where do I start from? Well, I had a mobile device on my hands. The first thing that came to my mind was to create a certain limitation (in addition to the small amount of time I had). Thus, I decided that the only way to control would be with the accelerometer of the phone – no touching, no keyboard buttons. Just tilting the phone.

Fine, I had a limitation and the controls. Now, the game? I wanted to have something fast paced, quick to play, quick to fail and quick to start again. Thus, I came across the idea of a car game. Just your old, not-so-breakthrough, driving game.

So, now we have the game idea: A car driving forwards that moves according to the tilting of the phone. Nice 😀


Well, something easy and fast-paced. This was quite easy to come by. Imagine a car driving very fast on the wrong lane. There are other cars coming towards you. The player needs to tilt the phone to avoid colliding with the law-abiding citizens. As this would be too easy by itself, I added oil spills. If the car hits an oil spill, it looses maneuverability – the player cannot move the car for a few seconds, thus forcing the car to just keep going straight. This added some nice challenge to the game.

Also, the game keeps track of the player’s score. The further you get, the more points you get. In addition, if the player tilts the phone forwards the car goes faster and if tilted backwards the car brakes. Going slow is easier, but the player earns a lot less points in the exchange. So, either higher speed, higher gain and higher risk or the slow grandma way 😛 Now there is a simple gameplay logic and challenge to the game. All that is missing is…


Urmm… yeah. This is something that is needed. I guess. As I suck on drawing and making graphics I had to go with some really cheap stuff.  If you can go the whole nine yards, I decided not to even go to the start line. No fancy programs and pixel graphics. Instead, I drew the car, oil spills, the road and the oncoming cars by hand. Some really ugly and nasty pencil drawings. BUT! After editing them a little bit (just changing temperature, saturation, hue, brightness, etc.) I came up with pretty decent looking graphics.

Here are the graphics I created and edited (all in a short amount of time)

The Car:  

Enemy car:

Oil spill:  


The road:  

Ugly? Well, it still works surprisingly good.

Putting it all together

Okay, so now I had everything in place. Easy controls, cheap graphics and simple gameplay. The creation of this whole game from a scratch took me about 4 hours, which I am quite proud of. Here is a screenshot of the game (yeah, screen capture of a quick-paced game is not easy, but you get the idea):

If you are still interested and would like to try out the game (I think it works only on n900), get the source from the following page:

It’s completely free and open-source. Do whatever you want with it, but please, at least provide some credits to my work. Also, if you happen to use something I have made, please let me know. I would be more than happy to hear about it 😀

Using Emergent Behavior in Video Games

As some of my readers might know, I am currently doing my exchange year in Madrid, studying Artificial Intelligence. One of the courses I had was about Intelligent Agents and Multi-Agent Systems. As a short test project I wanted to create an emergent enemy AI for a simple game. Just making the game and keeping everything to myself might have been easier, but I decided to share some of my thought with you.  Underneath is a shorted version of everything, but you can also download the complete .pdf article >> Using Emergent Behavior to Improve AI in Video Games << [all rights reserved]:-D


As most know, video game industry has been competing for the best graphics in games for a long time. Now, as making graphics has become more and more easier all the time, there has to be a new hook. Some say it is the story (I totally agree), but another thing that has not evolved much is the non-player character intelligence. This is something rather hard to do and experimenting on new methods is something the triple A studios are not very eager to work with. I am no top-notch researcher, so I just decided to try to implement a simple game AI, using emergent behavior.

Ummm… Emergent Behavior?

Yeah, what is emergent behavior? It is a behavior that emerges from simple set of rules, yet it can be considered intelligent. The creator only specifies some simple rules, that in the end result in wanted behavior. Insects, for example, is something that is being studied by computer scientists in order to find this kind of emergent behavior. Ants and bees for example display this kind of behavior in finding the shortest route to their goal – not because they actively search one, but because of their behavior they mark the fastest route between two points.

The Game

The game was implemented using python programming language and pygame library. Those not familiar, can read an introduction from my previous post. The game was programmed completely from the scratch, just to practice a little bit more of my programming skills. The game itself features a player controlled character, that can move around the screen and shoot with a gun. The enemies come in form of green aliens, that try to nd the player and attack him by biting. The way they communicate, is by leaving pheromones (blue dots) to the game area, that contains information about the locationof the player character. The rest following underneath is straight taken from my paper.

The AI

The idea of the Arti cial Intelligence is to create a simple emergent behavior for thealiens. They are considered to be quite simple and the method used is somewhat basedon ideas presented by Steels (1990) and Lewis and Bekey (1992). One important thing to notice here is that the aliens cannot use direct communication to talk with each other. Because they are simple critter-like creatures, they cannot have telepathic abilities, as that could be felt cheating by the player. Thus the aliens should use pheromones to leave information to the environment and use this indirect way of communicating instead of telepathy. This approach also makes the AI easier to implement and probably less resource intensive.

The Aliens have three modes: Search, Flee and Attack. In search mode the alien is trying to find the player in the game area. The alien has no knowledge of the environment around it and it does not gather any information about the level. Because of this, the alien is implemented to walk randomly until it sees anything of interest. If the alien in search state sees pheromones on the ground, it will start to follow the trail. In case the enemy sees the player, it will attack or flee, depending on the situation. There is a threshold related to the attack/flee behavior (as seen in the figure 1, points 3 and 4). If the alien sees other aliens nearby it will attack the player, but if it is alone, it will flee. The behind this kind of behavior is to make the aliens attack in pairs or groups. This way the player will have to fight a bigger bunch of aliens and has the sensation of challenge.

In flee mode the alien is running away from the player. This might be triggered for example because of being outnumbered or heavy loss of health (in the implementation here, the fleeing depends solely on the number of aliens nearby). In flee mode the alien leaves pheromones behind, so that other aliens might find the player (or that the alien itself might find the way back to the player). The pheromones contain a direction, which points towards the player in order for the aliens to know which way to follow the trail. As the alien is running away from the player, the pheromones left point always to the opposite direction the alien is facing.

In the attack mode the alien is charging towards the player, trying to attack it. This is done by getting to a close distance to player so the alien will be able to bite the player and deal damage in that way. The algorithm shown in figure 1 portrays the behavior of the aliens.

The Initial Algorithm For the AI

The initial level is shown in the figure 2. It is just a plain level, with only the player and the aliens. The red circle around the aliens portrays the field of view of the enemies. However, as can be seen from the figure 2, the player has actually too much room to move around as well as the search space is too great a challenge for the aliens. It takes a significant amount of time for the aliens to find the player (but because of the randomness, this is not always true). Also, the pheromone trail stayed alive for a very long time and once the aliens found the trails, they did not point to the current location of the player anymore and tended to be quite long.

The Initial Version of the Game

As a solution to these problems, there had to be a time-out on the pheromones. A timer was added, that would make the pheromones to disappear after certain amount of seconds had passed. This way the pheromones no longer led to empty places nor were dozens of pheromones long. Another addition that can be seen from the figure 3 was the addition of the walls. Because this kind of shooter games usually happen in tight, closed spaces, the playing area was divided into smaller rooms. The rooms reduce the search space for the aliens significantly, so they were able to find the player within the room quite quickly. The running movement of the player is also restricted within the rooms and there are only few doorways leading out from each of the rooms.

The Game After Adding the Walls

The changes led to faster search times for the aliens and better challenge for the player. However, as the alien fled from one room to another leaving a trail of pheromones behind the player was still able to run away. Once the aliens came back, the trail was a dead-end. Also in cases when the aliens attacked the player, other aliens nearby still just kept searching and did not react to the attacking aliens nearby.

To fix the problem, another type of pheromone was added to the game. These red pheromones are left when the alien switches to attack behavior. In attack mode it leaves pheromones that point towards the way it is moving (as opposite to the fleeing pheromones). Now when the alien goes into attack mode, it gather other aliens on it’s route to also attack the player. In the case where the player decides to run away, the enemies keep following and leave a trail that gathers even more aliens to join the attack. This means, that if the player runs around the whole game level with only one alien pursuing in the beginning, the player will end up with all the aliens in the current level following him. The final version of the implementation can be seen from the figure 4.

The Final Version of the Game


A good aspect of emergent behavior is it’s simplicity to implement. With just a couple of simple rules it is possible to create “intelligently” performing agents. However, as can be seen well in this paper, the design stage is somewhat complicated. Even though the end goal was clear in the beginning, the behavior rules had to be adjusted a little. The aliens behaved less smartly than they were supposed to. The positive thing was that adjusting the behavior was quiet easy, because of the simplicity of the behavior rules.

One good side of the emergent behavior and the simplicity is the low amount of processing power required. Dozens of aliens can be running at the same time on the screen without the loss of frame rate (one thing to remember though is that the graphics are not that complicated either). With some optimization it would probably be possible to gain even better performance than there currently is.

However there still remains more tweaking possibilities to be done for the aliens. They don’t seem to recognize the pheromones really well so there could be a bigger area effect on the pheromones. Other possible implementation could be to make the aliens scream when they attack the player. This way the scream might attract other aliens nearby (for example within the same room) to join the attackers.


If you are interested in reading more, check for example the following scientific papers for more on Artificial Intelligence.

S. Cass, Mind games [computer game ai]. Spectrum, IEEE 39(12), 40{44 (2002).doi:10.1109/MSPEC.2002.1088444F.
Dignum, J. Westra, W.a. van Doesburg, M. Harbers, Games and Agents: Designing Intelli-gent Gameplay. International Journal of Computer Games Technology 2009, 1{19 (2009).doi:10.1155/2009/837095.
M.A. Lewis, G.A. Bekey, The Behavioral Self-organization Of Nanorobots Using Local Rules,in Intelligent Robots and Systems, 1992., Proceedings of the 1992 lEEE/RSJ InternationalConference on, vol. 2, 1992, pp. 1333{1338. doi:10.1109/IROS.1992.594558
J. Orkin, Three states and a plan: the AI of FEAR. Game Developers Conference, 1{18 (2006)L. Steels, Cooperation Between Distributed Agents Through Self-organisation, in IntelligentRobots and Systems ’90. ‘Towards a New Frontier of Applications’, Proceedings. IROS’90. IEEE International Workshop on, 1990, pp. 8{14. doi:10.1109/IROS.1990.262534

The Source Code

The source for all I have written above is available for download. Get it from here >> Swarm Game Source << You are free to use the code for your own use, just please, give the credit to the one whom it belongs to. It is a .rar archive. All you need is Python and PyGame libraries to get it working, the images and everything are included.

Hope this helped someone or gave new ideas. Until next time!

StarSystem Lander

Today I am going to share my design of the game I am going to make during the coming days. As I already mentioned before in my previous post, I am learning some more Python and game programming at the same time. My plan is to make few small games before summer really starts and to learn at least a little bit of actual game programming. It is actually a sad thing, that they don’t teach game programming at the university, because it is fun. 🙂

The Idea

I am not planning to do anything very complicated and I didn’t come up with truly awesome idea for a small game. So, I will be programming a lunar lander clone, which I decided to call StarSystem lander. The idea is to land safely on a planet without destroying the spacecraft or running out of fuel. Those who don’t know the game can check out for example this youtube link:

My plan is to make a little nice graphics, although I actually think that the wireframe looks nice and nostalgic. I just want to learn to make nicer graphics, because that is the way games nowdays are like. I will not probably reach a cool looking level of drawing, but at least I am giving it a shot.


Okay, so the features for the game are supposed to be something like this.

  1. Movable spacecraft, that rotates 360 degrees
  2. Fuel meter showing how much fuel is left
  3. A High-score list according to with how little fuel and how fast the user can land the ship
  4. Different planets with different gravities (Moon, Earth, Mars, etc.)
  5. Graphics
  6. Some sounds (space is an empty place, so no music)

This is propable all the features that the game should have. If someone comes up with other good features, please let me know, so I could also try to add them. But these are something I will try to accomplish in the coming week or two.


When designing for the game, it is important to make some kind of sketches, graphs, etc., to shorten the actual development time. I draw a sketch of the game on pen and paper (that is really THE FASTEST way to prototype) while sitting on a morning train from Lappeenranta to Joensuu today. Because I don’t have a scanner at hand, I will make a paint sketch here to give an idea of how I see the game looking like.

This paint sketch looks very horrible, but hey, this is something I made up in a few minutes. The one I had drawn in my sketchpad looks a lot more nicer. (maybe I should have taken a picture of with a camera…. well, next time). All in all, I really have to admit it, I am no graphics artist, but hopefully I will manage with all these ”oh-so-hard” graphics 😛

The game objects

Now, to the actual part I am a little better – designing architecture of the software. Because I will be doing this in a prototyping way and not giving way too much thought on all the parts (the game is not that big, I can easily change things when needed, as long as I program it properly). So, the main components are SpaceShip, Level, LandingPad, Timer and FuelMeter. These are the pieces the game is made of (drawn on the screen). The game engine beneath is of course it’s own case, but I will now just document some ideas of the game parts.


This is propably the most complicated of the classes. The spaceship has location (where it is on the screen), speed (= how fast it is moving), direction (which way it is moving) and fuel (how much fuel is left).

The methods that the spaceship has are Rotate(direction), which turns the spaceship either left or right. Throttle(), which is used to gain more speed to the direction the spaceship is facing. Move() is used to actually move the spaceship on the screen (this is for drawing reasons, which I will explain about a little bit later). The three get functions are used to get the specified information of the spaceship (like the current location). Also there is a checkLanded() function inside the spaceship class, but this I might probably change to somewhere else later.


Level object will propably contain the picture of the ground (how it is formed) and also contains the gravity value of the planet in question.


LandingPad is an object that will be placed on top of the actual level. This way there is no need to program to the level object the location of the landing pad, but to have it as an individual object, that can be called to check if the ship has landed at correct place.


Timer is used to get the time how long the game has been running. It just displays the current time on the top-right corner of the screen.


FuelMeter displays the amount of fuel left. Every time the user Throttles with the spaceship a small portion of the fuel is used up. The fuel meter draws the current situation of the fuel on the right side of the screen, under (or next to) the Timer.

Next Time

I have now demonstrated a very short and brutal design of the game. As I will be programming more and more, I will probably change classes a little bit when needed, but my goal is to get something done quickly. My next post will be about programming the StarSystem Lander game. The coming weekend will be pretty busy day for me, because I will be participating in one seminary. Expect for the next update around next week. And please, feel free to leave comments.


Programming Python

Hello everyone!

…and sorry for not posting anything in the last few weeks. Instead, I am going to make few blog posts this week.

So, straight to the topic. I have received a nice summer trainee position from my university. I will be working on something called Cyber Foraging (you can check more about this by googling). I will get back to this later, once I have started the work and studied this area more. My job is to program something using Python. I have done few projects with python, but not for almost a year, so I decided it is time to recap some of my skills (and learn more, of course!). Those of you who are not familiar with Python I recommend you reading the website and getting to know the language. It is fast, efficient and powerful (although the whitespace system sometimes gives some headaches).

As I have probably mentioned before I am very much interested in game programming, but haven’t had much luck with it (I have tried to start with a WAY TOO HARD approach). I have had some hard time getting Ogre3D to work with c++ ( I really don’t know why it doesn’t work correctly plus I haven’t had much time). Well, Now with my new Python interest, I found out that ogre has also python version of it. I was able to get this Python-Ogre running. YAY! However, making 3D games takes a lot of time, which I don’t have too much, I decided that I need something a little bit less complicated.  What I found out was this: PyGame.

Huh? PyGame? What’s that?

PyGame is a Python extension library that wraps SDL library and it’s helpers” SDL (short for Simple DirectMedia Layer) is cross-platform multimedia library that provides a low level access to audio, keyboard, mouse, joystic and 3D hardware.  SDL is written in C, but with the PyGame it will work with Python language as well. PyGame is actually really easy to program and even a programmer with not much programming experience can pretty quickly start coding nice 2D games. Why 2D? Simply put, 3D games require a lot more time, even more experience and more graphical skills than a 2D game (at least usually).  A nice introduction to PyGame can be read from here.

How to get started?

1. Download Python. I am using version 2.6.5 which is currently the newest (of the 2. series). A note here. Python has also 3.version, but the syntax is different from the more widely used 2. version. It also has more third party software available than 3. version.

2. Download PyGame. Here it is important that you download the same version as your Python is.

3. If you don’t want to use IDLE (the editor that comes with the Python). The problem with IDLE is, that it has no code-completion or other “little-more-sophisticated” functions. However, you can finely debug and program with IDLE. I recommed installing Eclipse (the classic version is good one) and the Python add-on for Eclipse.

The PyGame website actually has pretty good collection of Tutorials. I really recommend checking them out! I myself started going through them last week and I have a
lready programmed some nice and simple games (for example the classic Worm game). From my experience I recommend at least the following tutorials by Lorenzo E Danielsson. Also another one worth checking out can be found from Eli Bendersky’s website.

Here is a screenshot of a simple worm game I created accoring to Lorenzo’s tutorials

The links I have supplied in this blog entry:

Lappeenranta University of Technology –
Cyber Foraging by Google search –
Python –
Ogre3D –
Python-Ogre –
PyGame –
PyGame Introduction –
Download Python –
Download PyGame –
Download Eclipse –
Python add-on for eclipse –
PyGame Tutorials –
Lorenzo’s Worm game tutorial –
Eli Bendersky’s PyGame tutorials –