Basic Pong
<< Questions 20 | OtherProjectsTrailIndex | Basic Cannon >>
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:
- a field with edges
- a ball that has a speed and direction
- a paddle that moves up and down
- 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:
- A constant for the thickness of the outside wall
- A constructor that makes two Rectangles, one for the outside and one for the area that the ball could contain
- A
draw
method - A
contains
method that responds with a true if a rectangle is contains in the legal are for the ball - A
getLeft, getRight, getTop
andgetBottom
that returns theint
corresponding to the legal ball area
Ball Class
- A constant for the ball size (try 15)
- A constructor that makes a bounding box of type
Rectangle
, initalises adeltaX
and adeltaY
- A
move
method that translates the bounding boxRectangle
- A A
getLocation
method that returns the bounding box - A
changeX
andchangeY
method that changes the deltaX and deltaY to the negative version of the current value - A
draw
method
The Paddle class
- Needs two constants for the width and height of the paddle
- A constructor that inialises the bounding box
- A
setHeight
method that sets the y location of the paddle's bounding box - A
touches
method that compares a givenRectangle
with the paddles' bounding box (use theRectangle
class'intersects
method.