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 app that handles user input and the passage of time
BasicPong.java
import java.awt.*; import java.awt.event.*; import javax.swing.Timer; /** * @author Chris Thiel, ofmcap * @version Sept 30, 2024 */ public class BasicPong extends Frame implements ActionListener, MouseListener, MouseMotionListener { private Timer timer; /** * These instance fields are for getting rid of flicker * on the Windows platform-- the App 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 BasicPong(Font f){ font =f; } public BasicPong(){ super("Basic Pong 1.0"); setSize(500,500); timer = new Timer(10,this); font = new Font("Helvetica", Font.BOLD, 18); message="Pong: click to begin"; timer.stop(); addMouseListener(this); addMouseMotionListener(this); this.addWindowListener(new WindowListener() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} }); } 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, 40, 50); 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) {} public static void main(String[] args) { BasicPong app= new BasicPong(); app.setVisible(true); } }
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
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
import java.awt.*; /** * Ball is the moving object that will * bounce against walls and the user's paddle * * @author (your name) * @version (a version number or a date) * * It should have: * 1. A constant for the ball size (try 15) * 2. A constructor that makes a bounding box of type Rectangle, * and initalises a deltaX and a deltaY * 3. A move() method that translates the bounding box Rectangle * 4. A getLocation() method that returns the bounding box * 5. The changeX() and changeY() methods that changes the deltaX * and deltaY to the negative version of the current value * 6. A draw(Graphics g) method to make a round white ball in * the correct place on the screen. */ public class Ball { private static final int SIZE = 15; private int deltaX, deltaY; private Rectangle box; /** * Constructor for objects of class Ball */ public Ball() { } }
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.