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:


Tuesday, May 6, 2014

Making a game in Python

Coding a Game

How could I improve my coding in Python? that was my question a month ago - and after I made my menu library in curses for Python - I was thinking of writing a rogue like game, just for fun.
But to my surprise now I'm hooked on this fun project.

I've played rogue like game in text mode in the past - one of the greatest and most famous is called "crawl". But playing them - left me with a feeling of emptiness, killing monsters and being killed was pretty much the expected experience and after filling the morgue with all sort of failed attempts, I gave up...

And I thought, what is the main lesson in this game?! is it you have to kill or get killed?
what is it I'm unconsciously learning through this game? is it the old "Dungeon and Dragon" paradigm that when I kill monsters I get more experiences (xp) and can level up?

Because to be honest, the only incentive (and excitement) in this sort of game is to level up, so you can meet new monsters more powerful than before, but your hope is that you will level up soon enough, so you can defeat them, but the irony is that inevitably then you will meet even more powerful creatures...

This game mechanism is almost a metaphor for the Buddhist definition of "suffering" : The anxiety or stress of trying to hold onto things that are constantly changing...

So my idea was to write a different kind of game... where the avatar's desires and attachments are seen as a way to achieve enlightenment.

A different kind of game

A game where it's not about killing, but about playing with concepts and kōan , and learning philosophy and spirituality along the way ...

That's the idea in a nutshell, I'll give you an example of the kind of strategy to use in the game.
Let say my character is entering a cave and there is inside a dragon protecting his hoard, and through the use of meditation and persuasion the avatar is capable of teaching the dragon how to let go of attachment, after a while the dragon just leaves the cave and fly away ... there was no fight, no kill - the dragon is now a friend and the whole situation is positive.

There will be of course many challenges in this game, but no kill - no experience will be gained through slaughtering monsters. This idea of killing to gain experience and level up is completely wrong.