C212 Assignment 6

Moving Sprite

Due: Wednesday, October 16th, 11:00 AM

Pair programming, with a partner of your choice, so long as your are both in the same lab.

Scenerio

A central characteristic of many video games is that the playing area is divided into a rather course grid of cells and the movable game elements, called sprites, move smoothly in a horizontal or vertical direction between the centers of the grid cells. Frequently the sprites change appearance when they are moving so they appear to be facing in the direction of motion. In many cases when they are in motion there is also some crude form of animation suggesting a sprite activity in addition to their gliding motion, such as walking, eating, blinking, or waving a sword. These characteristics provide enough realism and versatility for satisfying play in many games, while providing enough constraints (such as allowing only horizontal or vertical motion and stopping only at fixed grid positions) to make the game more efficient and much easier to program than it would be without the constraints.

In this assignment you will be programming all of these game elements. The resulting code will be usable with minor modification to build complete games. This assignment's sprite appearance and animation is similar to that of the famous PackMan game. The assignment provides experience with threads (objectdraw ActiveObjects), for and while loops, and optionally, simple use of an array.

In lab

Choose your partner and begin work on the following assignment.

Assignment


Use Netscape to view this applet and others in this course, since Internet Explorer on STD machines, and many home machines, by default does not use Java 1.4. (Or in IE select Tools>Internet Options>Advanced>Java (Sun)>Use Java 2..., exit IE, and start it again. Unfortunately on an STC machine you have to do this every time you log in.) 

This assignment is to implement a FrameWindowController class with behavior similar to the above applet. It should have the following properties:

  1. Most of the white canvas area is divided into a course grid of roughly square cells divided by lines.
  2. When not moving, the sprite appears in its "closed mouth" form as a red circle centered in a grid cell.
  3. The applet responds only to mouse press events, and then only when the sprite is not moving and it is pressed in a cell that is in the same cell row or column as the sprite. The sprite then moves smoothly to the indicated cell and stops.
  4. When in motion the sprite alternates between its closed mouth form and an arc that suggests an open mouth pointing in the direction of motion. (This alternation may not be as smooth as the sprite motion.)

A number of details are left unspecified, such as the number of rows and columns, the color of the sprite, how wide the open mouth is, and so on. You may choose any values for these details as long is it is easy to see that your program has the above functionality.  

Design

Here is the CRC design to be used in this assignment:

Class MoveSprite
Responsibilities Draw the grid, creates the sprite, and handles mouse press events.
Collaborators Sprite

Class Sprite
Responsibilities  Display the sprite (with mouth closed or open in the direction of motion), moves the sprite, animates the sprite motion, and reports whether the sprite is moving.
Collaborators Timer

Class Timer
Responsibilities Call the sprite when a given time interval has expired.
CollaboratorsSprite

For this assignment, use the following contracts (public members):

MoveSprite   Sprite

 

Timer

Submission

Submit moveSprite.jar in the same way as the last assignment.

Hints

  1. Implement your solution in several stages, testing (and saving) each one before coding the next: The following stages are recommended:
    1. Draw the grid and place a closed-mouth sprite on it. (For this the only method you need to implement in MoveSprite is begin and the Sprite class needs no methods at all, just a constructor.)
    2. Make the sprite move in one jump to a new location on the same row or column. 
    3. Make the sprite move smoothly by implementing and using the Timer class. Create a new instance of the timer class for each move. (If you make good design choices, you won't have to modify MoveSprite after this stage.)
    4. When the sprite is moving, make it appear in open-mouth forms facing the direction of motion. With passable style, you can get a C without implementing this stage.
    5. Animate the sprite so when it is moving its mouth opens and closes. With reasonable style, you can get a B without implementing this stage.
  2. The four open-mouth arcs are similar enough that a loop can be used to create them all, placing them in an array. This is somewhat better style than creating them separately and storing them in four separate variables, but you do not have to use arrays in this assignment.
  3. Create a new instance of the timer class for each move to a new cell indicated by a mouse click. These instances are active objects, but unlike the active objects you have seen so far, they do not directly represent graphic elements. Each simply makes a series of method calls that make the sprite take the little steps that make up a move between two cells.

Note: As always, you do not have to follow suggestions presented as hints, though if you do something in a significantly more difficult way, that will be considered bad style. On the other hand, if you can figure out a better way, that would be excellent style. Failure to follow design guidelines that are not presented as hints may result in substantial loss of credit. If you think there is a better way of doing those things and want to do it your way, obtain permission first.