# Introduction to Computer Programming: Lab 9

## A simple graphics game

### (an introduction to event-driven programming)

#### Instructions

• Work on this lab with your partner (or on your own if you have not been assigned one).

• Download two files: the starter file and the square-graphics module. Work within the starter file.

• You may find it helpful to examine this event-driven example based on what we have developed in recent lectures.

• Unlike most other labs this semester, this is intended to be short. However, if you do not have time to complete the exercises during the scheduled time then you should finish them on your own.

#### Goal

Your goal is to implement a very simple graphical game that uses key and timer events. The "story" for the game: the player seeks the gold treasure, guarded by the enemy. The player must reach the gold before the enemy does, before time expires and without running into the enemy. The player moves in four cardinal directions using arrow keys. The enemy moves randomly, several times per second. The player loses if it touches the enemy or if too much time elapses.

#### Exercises

1. Read over the the starter file and the square-graphics module. Ask questions about any parts of the code you do not understand. Experiment with running the starter code as is and contrast that with the runnable solution module.

Out of the box, the player and goal (the "gold treasure") are displayed and the player can move to the right ("east") by pressing the right-arrow key.

2. Write functions `north()`, `south()`, and `west()` and add three more `onkey` events to `start_game` so that the player can move north, south, and west. (Follow the model used for `east`. The relevant key names are `'Up'`, `'Down'` and `'Left'`.)

3. Winning. Modify `player_move` so that if the player moves onto the same square as the treasure (`g_gold`), the game ends and a suitable message indicating that the player won is printed to the shell. (Call `game_over`).

4. Add code to the function `countdown_timer` so that, assuming the game is still being played, a message is printed in the shell indicating how many seconds are remaining (the global variable `g_time`), the timer is decremented, and the function schedules itself to execute again one second later. (Use `ontimer`.)

5. Losing - part I. Modify `countdown_timer` so if the time reaches 0, the game ends with an appropriate message. (Call `game_over`.) You may wish to adjust the initial value of `g_time`.

6. Modify `player_move` so that the player's position only changes if it does not go outside the bounds of the graphics window. You may wish to adjust the values of `ROWS` and `COLUMNS`.

7. Complete `enemy_timer` so that, assuming the game is still being played, the enemy moves randomly up to one position in both the horizontal and vertical directions. (The chance the enemy moves north, south or stays in the same row should each be equally likely; likewise for moving west, east or staying in the same column.) The enemy should move once for every `ENEMY_TIME` milliseconds. (So, `enemy_timer`, like `countdown_timer` should schedule itself to execute again in the future using `ontimer`.) Start the enemy moving by adding an `ontimer` event to `start_game`. You may wish to adjust the value of `ENEMY_TIME`.

8. Add "wrap around" mechanics for the enemy: if it goes just past the right edge it should reappear on the left edge of the graphics window and vice versa and likewise for the top and bottom of the window. (Hint: use the `%` operator.)

9. Losing - part II. Modify `player_move` and `enemy_timer` so that if the player moves into the enemy, the enemy moves into the player, or if the enemy moves into the gold, the player loses.

10. Experiment with different values for the countdown clock, the dimensions of the graphics window and the speed of the enemy to make the game suitably difficult.

#### Time permitting

• Add a global variable for score and report the score when the game ends.

• Add the ability to "teleport" by having the player move to wherever the mouse is clicked.

• Make the enemy move faster the longer the game is played.