Game Development:
Intro to Computer Science

CSC 105
Union College
Fall 2009

Exercises about dictionaries and to practice for the final are on Codelab.

Helpdesk in Olin 102, Sun-Thu 7-9pm.

Reading a map file

The goal of this project is to read in a file that specifies the position and dimensions of rectangles and to draw these rectangles onto the screen.

Download the map file.

Step 1: function to read and process file

Define a function read_map_file. This function should take a string parameter, which is the name of the file to be read. It should return a list of 4-tuples (you can represent the 4-tuple as a list with four elements) with each 4-tuple consisting of 4 numbers specifying the coordinates and width and height of a rectangle.

So this function needs to: open the file, read the file line by line, extract the numbers from each line, assemble them into a tuple and add the tuple to a list, close the file once everything is read, and return the list.

To extract the numbers from each line, you need some string methods. Here is a list of all built-in string methods.

Since this function does not depend on any pygame specifics, you can develop and test it in a separate file, and only then, once it works, add it to your pygame program. That makes debugging much easier.

Step 2: call the function

Download, the following program as a starting point: rects_v0.py.

Use the function you write in the previous step to read in the map file and create the list of tuples representing rectangles once before the game loop starts. Print out the list that is being created to check that your function is working right.

Step 3: draw the rectangles

Add code to your game loop that draws each rectangle in the list created by the function call in the previous step.

Step 4: make it colorful

Change the map file so that it also specifies a color for each rectangle. Then modify your program so that each rectangle gets drawn using the color specified in the map file.

Bonus: navigate through a maze with dangerous walls

Download collision_example.py and worm_left.gif and worm_right.gif. It demonstrates how to check for collisions between two rectangles (such as the player and an obstacle). In this case, the player steers a little worm. If the worm hits the poisonous green leaf (rectangle), it says "ouch" (prints "ouch" to the screen) and dies (goes back to the start).

Write a map file that specifies a maze.

Merge your map file reading and displaying code and the collision code and extend it so that it becomes a game in which the player has to navigate the little worm through the maze without touching the walls of your maze. If the little worm does touch the walls, it has to go back to the start.

If you are still bored, you can embellish your game a bit more, by playing an "ouch" sound (look on the resources list on the pygame site for something good) instead of just printing "Ouch!" to the screen. Or you could add lifes, so that the worm gets send back to the beginning the first two times it touches the walls and then dies the third time.