Dr. Duncan now appears to be teaching at Louisiana Tech
Download Java source
Reference: CSC329: Introduction to Game Programming, University of Miami, Spring 2006
Before we go into some basic details about this particular applet, here are the relevant links:
The code is somewhat long, about 500+ lines of programs. But, it is designed to be somewhat flexible so adding new features can be easier - sometimes.
Look through the code to get a broad idea of how it works. For example, each Paddle comes equipped with two rectangles: one describes the paddle itself, the other describes its "motion" range. This prevents a paddle from moving outside of its bounds. The reason? To allow more flexible movement while being able to easily define where it can move to.
There are a lot of features so don't try to understand all of it. Look at the method names, descriptions, my limited commenting (sorry), and such to get an idea what is going on. After you have a clear idea of what is happening, think about what you want to do to improve the game.
When it comes to actually implementing changes, you will most likely need features from the AWT package or similar classes. The Pong game currently does use some of these features but by no means does it use all. Refer to the APIs and browsing the documentation to find a method or class that suits your needs. There is a lot there and it is hard to predict exactly what you will need.
What I recommend for improvements here is to be creative. Try to change the
game as much as possible to do what you like. Here are just a very few
suggestions:
Oh, and sorry there is no Pong sound
available!
Currently, my game just quickly (w/o pause)
displays the loser and then terminates. It does not keep track of scoring.
Certainly a noble addition. This would also involve displaying the score on
the screen somewhere. Maybe at the top? The score can be drawn in the paint
method like everything else. Look at how the Pong game displays the "Loser"
message using the drawString method.
This
involves changing both the keyboard input and how the paddles move.
These could move in sync with each other or
separately with different key commands. For example, you could have three
different paddles per player each with commands to move up and down
individually.
One
easy way to do this is to make the "goals" Paddles that don't move. The method
hitBall determines if the ball hit the paddle or not. So, you could simply
check if the ball hit these "goals" and if so, it would be considered in.
To do this, each iteration in
the run() method, the computer adjusts the paddle either up or down (just like
entering a key stroke) based on where the ball is located relative to the
paddle. It plays a very simple chase game, but it could be written more
sophisticated than that.
Currently, the
paddle maintains velocity unless you push the stop key and up or down
increases or decreases that velocity. There are many ways to change this. For
example, no momentum at all. Pushing down moves up - then stops - continue to
hold the key down and it will continue to go up. The ball gets heavy spin
based on your speed at the time it hits the paddle. This could be removed or
made less significant, by using a fraction of the speed for example.
Each paddle can release a ball if it hits the other
paddle, they lose. To prevent bombarding the balls, each paddle may be allowed
to release one ball at any time before it hits the opponet of opposite wall.
Or maybe the ball bounces back and the player must "catch" their ball again.