Monday, May 19, 2014

Squadron4 game now in graphic mode

Squadron4G is born


Like I said earlier in this blog, my goal in programming with Python was to reach some kind of knowledge of the "graphical mode" - something to be honest, I was kind of excited and anxious about.

Excited because it means I will have  access to colors, loading images, changing palettes, using "sprites" and later adding sounds effects, maybe music etc ...

Anxious because during my past experiences in programming that was pretty much the end of the road (except maybe that Sprite Editor I was doing in Turbo Basic back in the days (on 320x200 screen resolution and 4 colors))

Pygcurse to the rescue



Now enter pygcurse, this was the missing link for my Python's adventure - something that could be a bridge between the curses library and pygame (which for the moment still seems a little too complex).

After reading the manual, and some sources from the pygcurse package (especially demo-maze.py), I made the "port" of the code into something more graphical "friendly" and Windows compatible.

My strategy for the "port" was to override all the curses standard function (addch(), addstr(), inch() and so on) at the start of the program - to minimize the changes in the rest of the source. And to my amazement it worked quite well, almost immediately ... because I couldn't figure how to replace the inch() function since they was no equivalent, that was until I figure that the PygcurseSurface object had a table variable : _screenchar[x][y] which  holds the character at cell x, y.

Problem was the geometry : 80x24 in text mode doesn't play well in graphics - it was ridiculously too wide. The code was working, but the design had to be redefined.

Now the game is running in 40x40 area - there are colors, and even a new feature for the player I just added this morning : "Teleportation"by pressing 't' the player will teleport himself back in the center of the area. A very handy feature, when he will get surrounded by the chasers. But Teleportation costs 100 out of the countdown score, so again a feature to use wisely.

 

Where is squadron4G?


It is hosted at code.google.com - and here are :

 NB :
To run the game pygcurse is required (just copy pygcurse.py in the same folder)

 

Installation 


One last thing concerning my first game (the one in text-mode) the curses library I was using - isn't compatible with Windows machine, they need to use another separate library - and changing a bit of the source code too (maybe?!).

To be fair, Python is pre-installed on OS X and Linux - so I never thought that windows user should have to select which version they needed to download on python.org.

In conclusion, the installation for Window user might appear as a little heavy :
The first time they need to download python from python.org, then pygame from pygame.org, then pygcurse from github, and then finally my source code hosted at code.google.com...

Friday, May 16, 2014

Squadron4 a simple Python game using curses

How Squadron4 got made

This week I was lucky, and got some spare time, so I wrote a little program - from scratch, over 2 days - (but less than 24h ; so basically it's like the 24h comics challenge but for game programming).

I was also lucky because my development machine is an old (from 2004-2005) iBook g4 powerpc based, running 10.4.11 and the default installed python running on it was a version 2.3 ... But, very supremely cool python.org website is keeping an archive of their latest release (python 2.7.6) for powerpc (ppc) platform (running 10.3.9 and higher)! This was really totally awesome of them to do so.

So now I'm running python 2.7.6 and I also installed pygame 1.9.1 (but haven't had time to test it yet).

What is "Squadron4"?


A very basic platform game written in Python 2.7 using the standard curses library - with only one level (but since the program use random location, technically this could be considered as an infinite number of levels).

Work best with a terminal window of 80x24 - during the test phase, I had the best game experience with this relatively small area, and it's also incidentally the default size of the terminal in OS X.

The goal of this game is for the player ( the little '@' symbol) to collect the four flags represented by symbol '1','2','3' and '4' that are disseminated over the 4 corners of the game area.

But the challenge for the player in the flag collection is to avoid (or trap) the chasers (symbols : 'c', 'd', 'e', 'f') that are after him ; to render the flag collection even more challenging in the middle area 4 balls are bouncing endlessly and can't be destroyed (but they can be trapped).

The task has to be done before the countdown (which start at 1000) reach 0.

When the player decides to use a "magnet block" (by pressing 'f' or space bar) the countdown is reduced by 20, so part of the tactic is to use them wisely.

When the four flags are collected - they are seen in the lower status bar between brackets - the game ends, and the high score is presented.

The idea of the game is to see the interaction between 4 different moving objects :

  •     one controlled by the player (symbol : '@')
  •     4 bouncing balls (symbol : 'o')
  •     4 chasers that will track the player (symbols : 'c', 'd', 'e', 'f')
  •     and magnet blocks fired by the player to trap the chasers (symbol : 'B')

Keys for the player '@' :

  •     Arrow keys to move
  •     'f' or space bar to launch a magnet Block
  •     'q' to Quit the program

How to win?


A tactic to win, is to use the obstacle blocks which are part of the decor, or to launch some magnet blocks to trap the chasers. If the chasers are getting too close, they will prevent the player's movements, or may actually completely prevents all movements ; then the only option is to quit the game (by pressing 'q'), or to wait for the countdown to reach 0.


Where is squadron4?


It is hosted at code.google.com - and here are :
  • the link of the whole project.
  • the link to the Python source : squadron4.py
  • and some screenshots of the game:

Sometime player wins :


Sometime player loses: