ImpWorld: An Overview

copyright 2012 Viera K. Proulx

Introduction

The impworld library supports the design of graphics-based interactive games and applications by novice programmers, familiar only with the basics of Java programming language. The design of the library is based on the design of similar libraries in DrRacket, adopted to the class based programming and object-oriented design. It is very similar to the funworld library and shares with it the classes that support the design of graphical images for the game. A simple variant, appletworld allows the programmer to convert any of the games into an applet that can be shown and played on the web.

The programmer designs the image that comprises the game scene by combining the background image with the images that represent various objects in the game. The game actions happen in response to key events, mouse clicks, and tick events triggered by a timer that runs at the user-defined speed. Programmer defines a class that extends the World. This class consists of the game objects and any additional information needed to play the game. It then implements the methods that define the game actions (delegating to the game objects the responsibility for their behavior. The tester library makes it possible to test every method defined for the World, except the method bigBang that starts the actual game.

The images are composed from basic geometric shapes (rectangles, circles, disks, ovals, lines, and triangles) as well as images provided by .png formatted files. Additionally, the programmer can display text anywhere in the game scene.

1  Designing images

The images are drawn on Canvas. The programmer specifies the size of the Canvas when the game starts. Every image that the programmer defines has a pinhole the location of the center of the image, or some similar anchor point, using the actual coordinates of the target Canvas. So, for example, two rectangles of the same width, height, and color may differ in the location of their pinholes, and would be drawn at two different locations.

The color of the image can be specified in one of two ways. For the beginner, we supply the interface IColor and six classes that implement this interface: Black, White, Red, Blue, Green, and Yellow. To define the red color, the programmer just writes: new Red(). For others, the library uses the java.awt.Color class with the predefined constants for some of the color shades (e.g. Color.red, Color.cyan, Color.orange, Color.magenta, Color.gray) as well as any constructors for the instances of the Color class (e.g. new Color(255, 0, 0, 155) that defines a semi-transparent red color).

All images extend the abstract class WorldImage. The following diagram describes the classes that represent the basic geometric shapes, as well as additional ways of creating images:

          +-------------+
          | WorldImage  |
          +-------------+
          | int pinhole |
          | Color color |
          +-------------+
               / \
               ---
                |
 ------------------------------------------------------------------
 |        |            | |        |       | |         |         | |
 | +----------------+  | | +------------+ | | +--------------+  | |
 | | RectangleImage |  | | | DiskImage  | | | | EllipseImage |  | |
 | +----------------+  | | +------------+ | | +--------------+  | |
 | | int width      |  | | | int radius | | | | int width    |  | |
 | | int height     |  | | +------------+ | | | int height   |  | |
 | +----------------+  | |                | | +--------------+  | |
 |                     | |                | |                   | |
 |         +-----------+ |       +--------+ |       +-----------+ |
 |         |             |       |          |       |             |
 |   +------------+      | +--------------+ |  +------------+   +-+
 |   | FrameImage |      | | CircleImage  | |  | OvalImage  |   |
 |   +------------+      | +--------------+ |  +------------+   |
 |   | int width  |      | | int radius   | |  | int width  |   |
 |   |int height  |      | +--------------+ |  | int height |   |
 |   +------------+      |                  |  +------------+   |
 |                   +-----------------+    |          +---------------+
 +-----+             | LineImage       |    |          | TriangleImage |
       |             +-----------------+    |          +---------------+
 +----------------+  | Posn startPoint |    |          | Posn p1       |
 | OverlayXY      |  | Posn endPoint   |    |          | Posn p2       |
 +----------------+  +-----------------+    |          | Posn p3       |
 | WorldImage bot |                         |          +---------------+
 | WorldImage top |               +-----------------+
 | int dx         |               | FromFileImage   |
 | int dy         |               +-----------------+
 +----------------+               | String filename |
      / \                         +-----------------+
      ---           
       |             
 +----------------+
 | Overlay        |
 +----------------+
 | WorldImage bot |
 | WorldImage top |
 +----------------+
Here is a simple description of each class that represents a basic geometric shape: Additional images are built by reading the data from a file (formatted as .png), and by overlaying two or more images. The following three classes handle these tasks: Additionally, the WordlImage class includes a method WorldImage overlayImages(WorldImage ...) that overlays an arbitrary number of images on top of the bottom image that invokes the method.

1  Designing worlds

The programmer designs a class that extends the abstract class World. This class consists of the objects of the game and any information the programmer needs to report on the game progress (e.g. the current score for the game). The game actions are implemented by overriding the following methods:

Notice, that if the programmer does not need to respond to key events, or does not wish to run the timer, no actione needs to be taken - the stub mathods that do nothing are already defined in the World class.


Producing the image that represents the world

To display the world image the programmer must implement the method

WorldImage makeImage() that produces the image of the world to be displayed.


Signaling the end of the game

Ending the game after a tick:

If the game should end after a tick, the programmer can override the method

WorldEnd stopWhen()

This method produces an instance of a class WorldEnd that consists of a boolean value indicating whether the world is ending (false if the world goes on) and the WorldImage that represents the last image to be displayed.

The world invokes this method automatically after each tick and checks whether the worlds should end.

Ending the game after a key event or a mouse event:

If the game ends in response to a specific key event or a mouse click, the programmer returns the result of invoking the method:

void endOfWorld(String s)

The String argument is the message that is passed to the method:

public WorldImage lastImage(String s) that the programmer can override. (By default, the method just invokes the makeImage method for the current world.) Typically one would place the given message onto the world image (at some desired location and in the specified color).

The World detects the end and displays the final image by invoking the lastImage method with the given String.


Finally, to run the world, the program invokes the method

boolean bigBang(int width, int height, double speed)

within one of the test methods in the Examples class.

If the programmer does not wish to run the timer and respond to the ticks, he can invoke the bigBang method with the time 0.0, or omit the last argument entirely.


Last modified: Wed Feb 29 19:12:10 EST 2012