COSC 174 - Computer Games
Assignment #2
Animation and Interaction

Due Date: Friday, January 30, 2009

GOAL: You should produce an applet which involves both animation and user input. That is, there should be some part of the applet which is moving or changing based on an animation loop (such as the ball in Pong) and some part which depends on user input (such as the paddles in Pong). You could have the user input directly control some shape on the screen, or you could have the user input do something like change colors or speeds of shapes in the animation.

The hardest part of this assignment will be to keep it simple! We'll be talking about how to organize complicated games with lots of scenery and moving pieces - don't try to do that now. Think in terms of two or three simple shapes moving around on the screen.

For instance, here's one suggestion. Create an applet with a ball launcher in the bottom left corner and a random target somewhere on the bottom right side. You click the mouse somewhere above and to the right of the ball launcher (to show how the ball should start - see below), the ball launches, moves under the influence of gravity up and down, hoping to hit the target. The layout might look something like this:

The yellow dots would not really appear on the screen, they just show how the blue ball was moving in this still picture. Also, the small white square is just showing where the mouse was clicked. You could set the initial velocity of the ball based on the location of the mouse click - further to the right sets a larger x velocity, further up sets a larger y velocity. The player would try to click the mouse in a place that would make the ball land on the target.

METHOD: You should use an animation Thread and double-buffering as discussed in class. The input will require setting up an event listener for the mouse or keyboard. If you are using key events, you'll need to get the focus (probably by having the user click on the applet).

Placing the target in a random location (if you're doing something like the above suggestion) requires getting a random number in some specified range. The method Math.random() will return back a random number between 0 and 1 (possibly equal to 0, but less than 1). If we want an integer, say between 1 and N, then we can use


  j = (int)(N*Math.random()+1);

Multiplying by N gives us a number between 0 and N, adding 1 gives us something between 1 and N+1 (but less than N+1), and taking the integer part gives us an integer between 1 and N.

The details of how to update each step in your animation will depend on the effects you are trying to achieve. You could use a Finite State Machine to control frame changes, or movement could be based on simulated Physics. For an explanation of how to simulate falling under gravity and bouncing, see the pages on Movement and Bouncing and Gravity.

HAND-IN: You should email me a copy of your .java file and your .html file (to display the applet) before the start of class on Friday. Be sure that your program includes comments saying who wrote it. Also, either your code or a separate file should explain what the program does and how to work it (what input is expected).