Lecture 11:   Outline
1 How to design a game
2 Extending the World class - using a library
2.1 Ending the game.
3 Extending the Posn class - adding functionality
4 Designign graphical representation of the game

Lecture 11: Designing interactive games

Use a library to design an interactive game.
Practice working with subclasses and super classes.

     OceanGame.java      javalib.jar   

Lecture 11: Outline

1 How to design a game

We have seen how to design a super class that provides an abstraction for several related subclasses. Another use for abstract classes and super classes is in designing an infrastructure for building a collection of similarly structured applications. Often a several classes and interfaces are combined into a library. In Java such collection of classes and interfaces is combined into a package that can then be imported into the user’s program. One such library we have seen and have been using is the tester library.

In the first course in DrRacket we have designed interactive graphics-based games – by definng only the game behavior and the graphical display of the game state. The entire part of teh program that generates the window in which the game is shown, displays the graphics, and manages the user’s interactions is embedded in the library. We have a similar library available for the design of interactive grahics-based games in Java. The javalib library consists of several components. For the beginner there is the javalib.funworld that manages the dislay and behavior of the game components and the user interactions, and javalib.worldimages that handles the creation and manipulation of the graphical shapes and images.

Let use wee what goes into designing a simple game. Here is a snapshot of out game:

A shark moves up and down on the left side of the screen, hoping to eat some fish to keep from starving to death. A school of fish swims right to left – somewhat randomly. The player controls the shark’s movements using the up and down arrow keys. The game ends when the shark dies of hunger. So, to represent this game we need to represent a shark, a school of fish, their behavior, their interactions, and the display of the game scene at each point in the game.

The following class diagram represents the objects that comprise our OceanWorld game:


        | OceanWorld     |


     +--| Shark shark    |

     |  | ILoFish school |--+

     |  +----------------+  |  +----------------+

     |                      |  |                |

     v                      v  v                |

+------------+         +---------+              |

| Shark      |         | ILoFish |              |

+------------+         +---------+              |

| Posn loc   |-+           / \                  |

| int hunger | |           ---                  |

+------------+ |            |                   |

      +--------+   -------------------          |

      |            |                 |          |

      |        +----------+    +--------------+ |

      |        | MtLoFish |    | ConsLoFish   | |

      |        +----------+    +--------------+ |

      |        +----------+  +-| Fish first   | |

      |                      | | ILoFish rest |-+

      | +-----------+        | +--------------+

      | |           |        v

      v v           |  +----------+

   +-------+        |  | Fish     |

   | Posn  |        |  +----------+

   +-------+        +--| Posn loc |

   | int x |           +----------+

   | int y |


2 Extending the World class - using a library

The abstract class World in the javalib.funworld library provides a framework for the game design. The game programmer defines a class that extends the World class and defines the game behavior by implementing and overriding a few methods in the World class.

To define the graphical representation of the game scene, the programmer must implement the abstract method

public WorldImage makeImage()

We will discuss the design of the images later.

The three key methods that define the game behavior are defined as concrete methods in the World class, but their implementation is only a skeleton – each method just returns this:

// produce the World after one clock tick has passed

// in this World

World onTick() { ... }


// produce the World after user pressed the given key

// in this World

World onKeyEvent(String ke){ ... }


// produce the World after user clicked the mouse

// at the given position in this World

World onMouseClick(Posn pos){ ... }

(The Posn classthat represents a position on the canvas is defined in the javalib.worldimages package, and will be discussed in the next section.)

The game programmer overrides those methods that are relevant to her game. So, a wacamole game where the programmer has to hit randomly appearing objects by clicking the mouse at the correct locations does not need to override the onKeyEvent method. A board game where the user selects next move using the keys may not need the onTick method. Our OceanWorld game does not respond to mouse clicks and so does not use the onMouseClick method.

The design of the method onTick() and onKeyEvent(String ke) is straightforward, we just have to remember to delegate the work to the classes that are responsible for each task. So, both, the class Shark and the class Fish will have a method onTick(): the shark will get hungrier, and the fish will move left (for now, some fixed distance, say 3 pixels) and some random distance up or down (no more than 2 pixels). The method in the OcenWorld class will then just produce a new OceanWorld composed of this.shark.onTick() and this.school.onTick(). The onTick() method for the ILoFish will produce a list of Fish where each fish invoked its onTick() method.

Only a shark reacts to the key events. The Strings that represent the arrow keys are "up", "down", "left", and "right".

2.1 Ending the game.

3 Extending the Posn class - adding functionality

4 Designign graphical representation of the game

The colors for the shapes are provided in one of the following two libraries:

import javalib.colors.*;

import java.awt.Color;