Up for a game of life, anyone?

No, I’m not talking about the board game. This particular game of life, was thought up by a (respected) guy named Conway way back in the 70s. The rules are pretty simple, and a little time with our friend Google can fish out a lot of info on the subject. But for the uninitiated and the lazy, let me elaborate:

Game of life
Look’s creepy. But trust me, the black dots are supposed to be alive!

Imagine space – not necessarily where the space shuttles go, but ordinary two dimensional, sheet-like space would do just fine. Now imagine a speck in that space. Imagine earth, or imagine little round stones. Now imagine it to be alive (easier if you imagine earth). Now forget your 3rd grade science textbook and imagine little planets AROUND the earth – and no sun. Done?  Now imagine all the planets and the earth to cuddle together! Now the Earth is your ‘live’ cell (or planet) and the little planets around the earth are the ‘neighbors’ of the cell called Earth. Imagine that our Earth and little planets are confined to a 2-d space. That is, the earth and the planets are like the drawings on a piece of paper (I know that’s a little too much elaboration, but I couldn’t somehow help writing that). The ‘neighbours’ of the cell Earth can either be alive (as in Extra terrestrial life) or dead (as in a chunk of rock). Imagine the little planets around the earth to have other different planets near them as well. Now we have a pretty crowded (it depends) sheet of paper with many live and dead cells. Here are the rules:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction (imagine planets reproducing :p )

That’s it. The stage for the game of life is ready. What’s all this? It doesn’t sound very ‘interesting’?

Check out the wiki : http://en.wikipedia.org/wiki/Conway’s_Game_of_Life

Unimpressed? Boring? Drab?

Have another shot : http://conwaysgameoflife.appspot.com/

A warning though. We are now walking along the shores of that vast sea called cellular automata. Now ‘cellular automata’ was one of those words I used to specifically avoid and classify as ‘complicated un-readable stuff’. And it is complicated. But I read about the game of life on a different context and it sounded simple enough, and thus forms the basis of my next little program!

Here’s an overview of what I HOPE to implement :

1. A grid. Lines running horizontal and vertical divides the grid into cells.

2. an Initial pattern, and a target cell. The goal of the player is to make the target cell alive and maybe keep it alive for 3 turns

3. In each turn, the user can click on a limited no of cells to turn them ‘live’

I guess that’s pretty much it. No complicated Artificial intelligence. No simulation of cool patterns. Just a bunch of boxes to click on and green dots to populate them. Let’s see how far it goes. I guess it shouldn’t be much of a problem, since python and pygame always comes to the rescue 😉

But before going all graphical, let me try to get it up and running in text mode!

Ho Ho MapleBlow!

My very first Pygame project. Its not professional and it certainly isn’t the best way to implement what I was hoping to implement. But as they say, a journey of thousand miles begins with a single step.

This little program here lets the user click around the screen to make a maple leaf move. The basic idea is, the closer you click, the faster the leaf moves. The user will have to do his part by imagining that a wind blows from the point of click to the center of the maple leaf. Also, simply keeping the mouse button pressed won’t do the trick. You have to keep it pressed AND move it a little bit. The problem is, when u keep ur mouse button pressed, my program interprets it as a single mouse press as long as the location remains the same.

Everything done using python. The maple leaf is actualy a png image. Used pygame library for creating chunk of the program. The little menu u see before the program runs was created using wxpython. Oh, and I’ve converted the whole thing into an exe format using py2exe. Download the ready to execute version here :

 https://docs.google.com/file/d/0B6gQ1pCnWlqbZWx0bEdQNVNGVFU/edit

For the source code : https://docs.google.com/file/d/0B6gQ1pCnWlqbZ1pzVE54bi1CNFE/edit

Note: The source code is in a tarball. Linux users just have to extract it. But windows users will have to find some software that can open tarball files.  But I think new versions of winrar can open that file.

Here’s an overview of what I’ve done :

1.vector2.py : Created a vector class to handle all those vector additions (for adding velocity of wind to that of leaf)

2.wind.py : A Wind class which contains functions that calculate the direction and magnitude of the vector from a point to the centre of the leaf

3.World.py: Mainly contains the code for creating platforms (hurdles) that reflect the leaf. Since I found the collision detection a little buggy, I’ve programmed the game.py (main file) to skip over platforming creation

4.Leaf.py: Contains everything directly related to the protagonist of our story – the maple leaf. Contains functions to render the picture (which is really a static png image) , move the picture (update the coordinates) and collision detection in case the platforms are present

5.game.py: The main file. Calls every other function. Contains the code for creating a basic menu for the program as well

The program is not very complicated, even though it spans multiple files.