Basic Pong

<< Questions 20 | OtherProjectsTrailIndex | Basic Cannon >>

See a Working demo here

I hope this is a understandable basic introduction to a game with motion. This is a simplified classic: Pong. Our version will have three sides, one ball and one paddle. Once you get the idea, you can make a two player version, or perhaps make a "brick out" version.

We'll need:

  1. a field with edges
  2. a ball that has a speed and direction
  3. a paddle that moves up and down
  4. an applet that handles user input and the passage of time

BasicPong.java

package pong;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.Timer;


public class BasicPong extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
	private Timer timer;

	/**
	 * These instance fields are for getting rid of flicker
	 * on the Windows platform-- the Applet will draw the 
	 * picture in memory before putting it on the screen
	 */
	private Image virtualMem;
	private Graphics g0;
	private Font font;
	private String message;
	public void init(){
		timer = new Timer(10,this);
		font = new Font("Helvetica", Font.BOLD,  18);  
		message="Pong: click to begin";

		timer.stop();
                addMouseListener(this);
		addMouseMotionListener(this);
	}
	public void paint (Graphics g)
	{
		//make a new buffer in case the applet size changed
		virtualMem = createImage(getWidth(),getHeight());
		g0 = virtualMem.getGraphics();
		g0.setColor(Color.BLACK);
		g0.fillRect(0, 0, this.getWidth(), this.getHeight());
		g0.setColor(Color.WHITE);
		g0.setFont(font);
		g0.drawString(message, 20, 20);


		g.drawImage(virtualMem,0,0,this);//set new display to Screen
	}
	public void update(Graphics g)
	{
		paint(g); //get rid of flicker with this method
	}
	@Override
	public void actionPerformed(ActionEvent e) {

		repaint();

	}
	@Override
	public void mouseDragged(MouseEvent e) {}
	@Override
	public void mouseMoved(MouseEvent e) {}
	@Override
	public void mouseClicked(MouseEvent e) {}
	@Override
	public void mousePressed(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}

}

Field Class

the field class should have the following methods:

  1. A constant for the thickness of the outside wall
  2. A constructor that makes two Rectangles, one for the outside and one for the area that the ball could contain
  3. A draw method
  4. A contains method that responds with a true if a rectangle is contains in the legal are for the ball
  5. A getLeft, getRight, getTop and getBottom that returns the int corresponding to the legal ball area

Ball Class

  1. A constant for the ball size (try 15)
  2. A constructor that makes a bounding box of type Rectangle , initalises a deltaX and a deltaY
  3. A move method that translates the bounding box Rectangle
  4. A A getLocation method that returns the bounding box
  5. A changeX and changeY method that changes the deltaX and deltaY to the negative version of the current value
  6. A draw method

The Paddle class

  1. Needs two constants for the width and height of the paddle
  2. A constructor that inialises the bounding box
  3. A setHeight method that sets the y location of the paddle's bounding box
  4. A touches method that compares a given Rectangle with the paddles' bounding box (use the Rectangle class' intersects method.