Python Bots
To get into the spirit of computational thinking and working with Python, in this lab we’ll run a simple game where you control a robot through a maze of obstacles. You can download the files for the game here:
The .zip
file is a compressed archive containing all of the files. Create a new directory in your course folder (esapcs/
), e.g., call it pythonbots
. Place this file in your pythonbots/
folder and explode the archive. You can do this from the standard Windows or OS X GUI. Alternatively, you can use the unzip
command (e.g., unzip pythonbots.zip
if you are in a Unix command prompt.
After doing this, you can see the contents of the directory:
kambing-mobile:files osera> ls
challenge1.arena pythonbots.py tutorial3.arena tutorial7.arena
challenge2.arena pythonbots.zip tutorial4.arena tutorial8.arena
challenge3.arena tutorial1.arena tutorial5.arena tutorial9.arena
playground.arena tutorial2.arena tutorial6.arena
The files contain a Python source file, pythonbots.py
, as well as a number of “arena” files that are maps you can play in the game.
To start the game, run the Python interactive mode (with the -i
flag), also passing in the Python source file as input:
python -i pythonbots.py
This will load the contents of the Python source file into the interpreter so that you can use the functions declared inside. At the Python interactive prompt, you can call the init_game
function which takes an arena file name as input, e.g.
init_game('tutorial1.arena')
This will load a new game with the given arena. Loading tutorial1.arena
will give you the following screen:
===== Board State =====
.........
..>..!...
.........
===== Moves =====
flip(): 0
rotate_clockwise(): 0
backwards(): 0
forwards(): 3
hold(): 0
backwards2(): 0
forwards2(): 0
rotate_counterclockwise(): 0
The top part of the output is the board. The robot you are controlling is represented by the angle bracket (>
). The bottom part of the output describes the possible moves that you have left. You can issue these moves—really more Python functions—by issuing them in the interactive prompt, e.g.,
forwards()
Will move your robot forwards. Note that all commands are performed relative to the robot’s current direction which you can derive by which way the robot is facing on the board.
Turn-in
Complete all of the tutorial and challenge arenas in order. Once you are done, create a new text file called mychallenge.arena
that is a new arena of you and your partner’s design. Make sure the arena is doable!
Once you are done, show a staff member your:
- Solutions to the three challenge arenas.
mychallenge.arena
file.
Have the staff member try to complete you challenge; it should be doable!