<-- Back to Andy Balaam Home

Troncode

Introduction

Troncode is a simple game that gives you a chance to play around with writing programs that play games.

The game is the classic "light cycle" game inspired by the film Tron. Each player controls an ever-growing line and must trap her opponents, causing them to crash before she does.

It looks like this:

The title screen looks like this:

Getting Troncode

(Browse the code here: http://repo.or.cz/w/troncode.git.)

To run pygame, cd into the directory where it lives and run:

python troncode.py

Alternatively, on Windows, double-click on troncode.py.

How to play

In the current version, the default setup is to watch every player fight every other one simultaneously (including a human player you can control with the arrow keys). To see this, just run:

python troncode.py

The other option is to play an automatic tournament where you can't see all the matches happen. Each player plays each other one 100 times, and there are also 100 x (number_of_players - 1) all-together "Melee" battles. The winner is chosen by the number of battles they won. To see this, run:

python troncode.py --tournament

How to write players

You need to create a new file inside the players directory and name it SomethingPlayer.py (it must end in 'Player.py'). Inside that file you should create a class with exactly the same name as the file (without the .py).

The file players/old/TurnRightPlayer.py in the source code is a simple example of what you need to do:

from troncode_values import *

class TurnRightPlayer( object ):

   def __init__( self ):
           pass

      def GetColour( self ):
              return ( 160, 160, 255 )

      def GetName():
              return "Turn Right Player"

      GetName = staticmethod( GetName )
      def GetDir( self, position, direction, gameboard ):
              """Turn right if there is a pixel directly in front."""

              if gameboard.GetRelativePixel( position, direction, 1, 0 ) > 0:
                      return gameboard.TurnRight( direction )
              else:
                      return direction

The key thing is to implement the GetDir() method, which is called every time step.

The return value of GetDir() must be one of the directions, stating which way you want to go in the next time step (one of DIR_UP, DIR_RIGHT, DIR_DOWN or DIR_LEFT).

Your implementation of GetDir() can use the input you have - your position in the arena (which is 200x200 in size), your direction which is one of DIR_UP, DIR_RIGHT, DIR_DOWN, DIR_LEFT, and the gameboard which provides 1 method to find out about which squares on the board are occupied GetRelativePixel(), 1 method to find out where the other players are GetPlayerPositions(), and 2 convenience methods TurnRight() and TurnLeft() which handle rotating directions for you.

The interface for the gameboard object is defined in the file AbstractGameBoard.py.

Apart from that, you can use any Python stuff you need (e.g. import math, random), and the only rules are that you can't modify the gameboard variable (it is passed by reference for efficiency) and you can't use an unreasonable amount of CPU. More rules will be added as people find clever ways of cheating.

Test-driven development

If you like to do thing test-driven, just create a function in your MyPlayer.py module like this:

def run_tests():
    # Do tests here
    print "All MyPlayer tests passed."

Then you can run them (with the correct imports etc. in place) like this:

python troncode.py --test MyPlayer

If you want to create a fake/mock gameboard, it may well be useful to inherit from BasicGameBoard, and just implement the missing methods to return what you want. An example of this may be found in SelfishGitPlayer.

Competitions

The latest winner is Selfish Git by Andy Balaam.

Troncode Competitions

Edit | History | Print | Recent Changes | Search | Admin Page last modified on June 21, 2009, at 11:23 AM