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.
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:
RectangleImage represents a filled rectangle with the pinhole in the center, and the given width and height.
FrameImage represents a rectangular frame with the pinhole in the center, and the given width and height.
OvalImage represents a filled ellipse image with the pinhole in the center, and the given width and height.
EllipseImage represents an ellipse outline image with the pinhole in the center, and the given width and height.
DiskImage represents a filled disk image with the pinhole in the center, and the given radius.
CircleImage represents a circle outline image with the pinhole in the center, and the given radius.
LineImage represents a line image with the the given start and end points. The constructor places the pinhole in the middle of the line.
TriangleImage represents a filled triangle image with the given three points. The constructor places the pinhole in the middle of the triangle.
TextImage represents a display of text as an image. The pinhole is in the middle of the displayed text. The size of the text by default is 13, the programmer can change it as desired. The style is specified as follows: regular style = 0, bold face = 1, italic = 2, bold and italic = 3. Of course the default is the regular style. Finally, as for all images, the programmer can choose the color for the text.
FromFileImage represents an image provided by a .png file. The pinhole is in the center of the image. The library reads the file only once and reuses it any time it is displayed.
OverlayXYImage represents a combination of two images, The bottom image is drawn first, the second image is then drawn after its pinhole has been moved by the given distance (dx, dy). The pinhole for the resulting image is in the middle of the pinholes of the bottom and the top image.
OverlayImage is a convenience class that overlays two images without any change in the pinhole for the top image.
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:
void onTick()
This method changes the state of the world to what it should be after one tick of the clock has passed.
This method changes the state of the world in response to the user pressing a key on the keyboard.
This method changes the state of the world in response to a mouse click anywhere on the game's canvas. The givan argument provides the location of the mouse at the time of the click. click.
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.