Pong was the first video game to achieve commercial success and it was the application that brought computers into peoples homes for the first time. It was invented in 1972 at Atari. Nolan Bushnell, the head of Atari, is usually given credit for inventing Pong, but the person who actually wrote the code for the game is Alan Alcorn, a Lowell graduate.
In this assignment you will make a recreation of the original two player Pong game. Your completed assignment will have at least three classes: the “Applet” class, a Ball class and a Paddle class.
Since this is a large assignment, it probably best to complete it in steps. First create a new applet project. Then add a Ball class, and and finally add the paddle class.
public class Pong extends Applet implements Runnable
{
Thread animation; //declares a thread called animation
static final int REFRESH_RATE = 50; //Constant for “delay”
//declare other class data members
public void init()
{
//initialize class data members
}
public void paint(Graphics g)
{
//painting
}
public void start()
{
animation = new Thread(this);
if(animation != null)
{
animation.start();
}
}
public void run()
{
while(true)
{
//put code that runs in loop here
repaint();
try
{
Thread.sleep(REFRESH_RATE);
}catch(Exception exc){};
}
}
public void stop()
{
animation = null;
}
}
boolean
variables for the Up/Down direction (bUp) and the Left/Right direction (bRight).
Ball()
the class constructor. You may want to add arguments for the frame height and width of the frame and the x coordinate of the paddles (you can add these later).
void move ()
checks bUp and bRight to move ball in the correct direction.
void bounce()
Checks to see if the ball is moving off the screen. If it is it "bounces" the ball by changing myUpDown and/or myLeftRight.
void draw (Graphics g)
draws the ball at its current position.
- Paddle(int nXpos, int nYpos, Color aColor) the class constructor
- void moveUp()
- void moveDown()
- void draw (Graphics g)
- int yPos() an accessor method that returns the y position of the paddle.
implements KeyListener
to the "Pong" class. (Remember, if your applet implements more than one interface, separate the interfaces with commas).
import java.awt.event.*;
- public void keyPressed (KeyEvent ke)
- public void keyReleased (KeyEvent ke)
- public void keyTyped (KeyEvent ke)
addKeyListener(this);
and requestFocus();
to the init method.
boolean bLeftUp=false, bLeftDown=false, bRightUp=false, bRightDown=false;
keyPressed
, set up a switch
statement to set bLeftUp
(and the other booleans) to true if the correct key is pressed. In
keyReleased
, setup an similar switch statement to set the booleans to false. Then in the loop you can move the paddles with code like:
if(bLeftUp)
Paddle1.moveUp();
public class Pong . . . .
{
//1. two added declarations
Graphics offscreen;
Image image;
public void init()
{
. . . .
//2. two added initializations
image = createImage(nSCREEN_WIDTH, nSCREEN_HEIGHT);
offscreen = image.getGraphics();
}
public void paint(Graphics g)
{
//3. clear offscreen
offscreen.setColor(Color.white);
offscreen.fillRect(0,0,nSCREEN_WIDTH, nSCREEN_HEIGHT);
//4. modify code so that drawing is offscreen
theBall.draw(offscreen);
. . . .
//5. copy offscreen to visible screen
g.drawImage(image,0,0,this);
}
//6. override update()
public void update(Graphics g)
{
paint(g);
}
}
You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.
Submit the URL of your applet along with your Java code (the .java
file(s)) in an email message
to mrsimon@lycos.com.