HtDP Exercise: A Fire-Plane Game

© Kathi Fisler, 2007-8

Your job is to write a simple interactive game in which the player drops water from a plane onto fires on the ground; the goal of the game is to put out all of the fires by dropping enough water on them. A sample appears to the right.

The plane is always moving across the screen (independent of user input). The player can change the direction of the plane by pressing the left and right arrow keys. The player can also drop water from the plane by pressing the down arrow key. Each fire has an intensity: when a drop of water hits a fire, the intensity decreases. If a fire burns without water hitting it, its intensity increases. A fire is extinguished (and disappears) when its intensity reaches zero. The game ends when all of the fires have been extinguished.

You will build this game using the same world.ss teachpack. Work in the "Beginner with List Abbreviations" language level.

To write this game, you will need to provide at least the following:

Once you have these functions, you will run your game using the commands:

(big-bang WIDTH HEIGHT (/ 1.0 28) INITWORLD)
(on-redraw draw-world)
(on-key-event process-keys)
(on-tick-event update-world)
(stop-when game-over?)
where WIDTH, HEIGHT, and INITWORLD are constants you define for the size of the game window and the initial configuration of the game. You may omit any of these lines if you haven't yet written the corresponding functions (like game-over?)

The exercises in the next section break down the above steps to guide you through the implementation. For more of a challenge, figure these steps out for yourself (see the hints and tips section following the broken-down steps).

We do not expect you will finish the whole game in this lab setting. The goal here is for you to get experience working with lists nested within structures, and more experience with world.ss. Hopefully, everyone will get about halfway through this during lab (question 5 on the broken-down part).

Building the game, step by step

  1. Data definitions, examples and templates: Define the world to be a structure containing a plane, a list of fires, and a list of water drops. The plane should have a location (posn) and direction of travel. Fires have a location (posn) and an intensity level (number). Water drops have a location. Write out examples of data and templates for your data definitions.

  2. Draw the plane: Write a draw-world function that draws the plane, but ignores the water drops and fires.

  3. Animate the plane: Write the process-keys functions describe above to move the plane by user key presses. Write an update-world function to move the plane in its current direction when no key is pressed. Your test cases may assume that the lists of fires and water drops are empty.

  4. Draw and animate the water drops: Extend draw-world, process-keys, and update-world to handle the water drops. A down-arrow key should create a new water drop at the same location that the plane had when the key was pressed. The drops fall a few pixels (3 or 4, depending on the speed you want) each time the world is updated.

  5. Eliminate fallen drops: Edit your update-world function to remove any water drop that has fallen past the bottom of the screen.

  6. Draw the fires: Edit your draw-world function to also draw the fires. You may use different size fire icons based on the range of each fire's intensity.

  7. Detect collisions between drops and fires: Edit your update-world to remove any water drop that hits a fire. Also reduce the intensity of the fire each time a drop hits it (don't worry about increasing the fire intensity yet).

  8. Eliminate zero-intensity fires: Edit your update-world function to remove any fire whose intensity has decreased to zero.

  9. End the game when no fires remain: Develop the game-over? function to return true if there are no fires remaining.

  10. Increase fire intensity: Increase the fire intensity a small amount on each update when no drop has hit that fire.

Hints and Tips

Our Images

Use the "insert image" option under the "special" menu to load an image into your DrScheme file; you can use define to give an image a name. We used the circle operator (defined in world.ss) to get images for the water drops.

small fire medium fire large fire plane

Icons are from school-clip-art.com (fire) and clipartguide.com (plane).