The problem is that there is no end. You can walk away and it will score some points. There are several ways to make this more challenging. You can increase the force of the ball so that it finds the paddle faster and faster. You can also limit it to only ten touches of the paddle.
Once you get the idea of what is going on, you can use this as a start of your own Pong Game. You can invent you own or imitate a game you have seen.
This applet demonstrates the advanced Graphics topics of Chapter 13. It employs ImageBuffering to get rrid of flicker problems, and also introduces more implementation of Interfaces (MouseMotionListener and ActionEvent). It uses the Timer Class from the swing library.
import java.awt.*; /** * class Ball has the information abou the * size and location of the Ball and * methods for changing its location and * drawing itself on a Graphics object. * * @author Chris Thiel * @version 19 Nov 2008 */ public class Ball { // instance variables - replace the example below with your own private int x,y; // location private double dx, dy; // change in x,y private double force; private int size; private Color myColor; int score; /** * Constructor for objects of class Ball */ public Ball(int xLoc, int yLoc, int diameter) { // initialize instance variables x = xLoc; y = yLoc; size = diameter; myColor=Color.RED; dx=Math.random()*4.0-2.0; dy=1.0; force=1.0; score=0; } /** * Constructor for objects of class Ball */ public Ball(int xLoc, int yLoc, int diameter, Color c) { // initialize instance variables x = xLoc; y = yLoc; size = diameter; myColor=c; dx=Math.random()*5.0+1.0; //dx=2.5; dy=1.0; force=1.0; score=0; } /** * move will update the location of the ball * * @param Rectangle the bounds * */ public void move(Rectangle bounds, Paddle p) { int newX=x+(int)(force*dx); int newY=y+(int)(force*dy); //Check for size collision if ((newX+size)>(bounds.x+bounds.width) || newX<bounds.x) { dx*=-1; newX=x; } //Check for top collision if (newY<bounds.y) { dy*=-1; newY=y; } //Check for bottom collision if ((newY+size)>(bounds.y+bounds.height)) { if (p.isHit(newX+size/2)) { dy*=-1; newY=y; }else{ score++; newY=y; } } x=newX; y=newY; } public void draw(Graphics g) { g.setColor (myColor); g.fillOval (x, y, size, size); } public int getX() { return x; } public int getScore() { return score; } }
import java.awt.*; /** * Paddle here. * * @author Chris Thiel * @version 20 Nov 2008 */ public class Paddle { // instance variables - replace the example below with your own private int x,y; //current location private int size; private Color myColor; /** * Constructor for objects of class Paddle */ public Paddle(int xLoc, int yLoc, int s, Color c) { x = xLoc; y = yLoc; size=s; myColor=c; } public void setX(int xLoc) { x=xLoc; } public void draw(Graphics g) { g.setColor(myColor); g.fillRect(x-size/2,y,size,20); } public boolean isHit(int ball) { if (x-size/2<=ball && x+size/2>=ball) return true; return false; } }
import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; /** * Class AvoidancePong - write a description of the class here * * @author Chris Thiel * @version 18 November */ public class AvoidancePong extends Applet implements MouseListener, MouseMotionListener, ActionListener { // instance variables - replace the example below with your own //Ball a=new Ball(160,150,100); Timer timer; Image virtualMem; Graphics gBuffer; int appletWidth; int appletHeight; Ball b; Paddle p; Rectangle court; String message; public void init() { addMouseListener(this); addMouseMotionListener(this); timer = new Timer(10, this); b=new Ball (100,100, 20, Color.BLUE); p=new Paddle (200, 310, 50, Color.BLACK); court=new Rectangle(10,10,400,300); appletWidth = getWidth(); appletHeight = getHeight(); virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); message="Press Mouse to Begin"; } public void paint(Graphics g) { gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); gBuffer.setColor(Color.black); gBuffer.drawString("Avoidence Pong", 20, 20); gBuffer.drawString(message, 280, 20); gBuffer.drawString("Score: "+b.getScore(),20, 340); gBuffer.drawRect(court.x, court.y, court.width, court.height); b.draw(gBuffer); p.draw(gBuffer); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } /** * MOUSE LISTENER STUFF: * * Since we are implementing the MouseListener Interface * There are some required methods we need to declare here, even * if there is nothing to do if that mouse event occurs: */ public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) { if (timer.isRunning() ) { timer.stop(); message="Stopped"; }else{ timer.start(); message="Started"; } //if (b.contains(x,y) ) b.changeColor(); repaint(); } /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a restart button being pressed * or a timer event */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) // the passage of time, so we move our ball { b.move(court, p); } repaint(); } public void update(Graphics g){ paint(g); } /** * * MOUSE MOTION LISTENER IS AN INTERFACE: * these methods are needed of all MouseMotionListeners, * even if we don't do anything: */ public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { int x=e.getX(); if (x >= court.x && x<=court.x+court.width) { p.setX(x); } repaint(); } }