Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
PatheticPong Demo
A working demo of PatheticPong will appear below. Click inside the applet to interact with it.
PatheticPong BlueJ Project
Click here to download a BlueJ project for PatheticPong.
PatheticPong Source Code
The Java source code for PatheticPong is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * Example PatheticPong: a too-simple "Pong" game that features * a paddle and a falling ball that, sadly, do not interact. * * Jim Teresco, Siena College, CSIS 120, Spring 2011 * The College of Saint Rose, Fall 2013 * Based on similar example from CSCI 134, Williams College * * $Id: PatheticPong.java 2322 2014-02-13 02:04:20Z terescoj $ */ public class PatheticPong extends WindowController { // position and dimensions of the court private static final int COURT_LEFT = 50; private static final int COURT_TOP = 50; private static final int COURT_HEIGHT = 300; private static final int COURT_WIDTH = 300; private static final int COURT_RIGHT = COURT_LEFT + COURT_WIDTH; // dimensions of the paddle private static final int PADDLE_WIDTH = 50; private static final int PADDLE_HEIGHT = 20; private static final int PADDLE_Y = COURT_TOP + COURT_HEIGHT - PADDLE_HEIGHT - 1; private FilledRect paddle; private FramedRect boundary; // the boundary of the playing area. public void begin() { // make the playing area boundary = new FramedRect(COURT_LEFT, COURT_TOP, COURT_WIDTH, COURT_HEIGHT, canvas); // make the paddle paddle = new FilledRect(COURT_LEFT + (COURT_WIDTH-PADDLE_WIDTH)/2, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT, canvas); } // create a new falling ball public void onMousePress(Location point) { // create the ball at the top of the playing area // at the mouse point's x coordinate if (boundary.contains(point)) { new FallingBall(new Location(point.getX(), COURT_TOP), canvas); } } // move the paddle to follow the mouse's x coordinate public void onMouseMove(Location point) { if (point.getX() < COURT_LEFT) { // place paddle at left edge of the court paddle.moveTo(COURT_LEFT, PADDLE_Y); } else if (point.getX() + PADDLE_WIDTH > COURT_RIGHT) { // place paddle at right edge of the court paddle.moveTo(COURT_RIGHT - PADDLE_WIDTH, PADDLE_Y); } else { // keep the edge of the paddle lined up with the mouse paddle.moveTo(point.getX(), PADDLE_Y); } } }
import objectdraw.*; import java.awt.*; /* * * A simple active object that controls a ball that falls down the * canvas * * Jim Teresco, Siena College, CSIS 120, Spring 2011 * The College of Saint Rose, Fall 2013 * * $Id: FallingBall.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class FallingBall extends ActiveObject { // size and speed of falling balls private static final int BALLSIZE = 30; private static final double Y_SPEED = 8; private static final int DELAY_TIME = 33; // the ball controlled by this instance private FilledOval ball; // how far to fall before stopping and disappearing? private double yMax; // Draw a ball and start it falling. public FallingBall(Location start, DrawingCanvas aCanvas) { // draw the ball at its initial position ball = new FilledOval(start.getX() - BALLSIZE/2, start.getY() - BALLSIZE/2, BALLSIZE, BALLSIZE, aCanvas); // ask the canvas how big it is so we know when to stop yMax = aCanvas.getHeight(); // activate! start(); } // move the ball repeatedly until it falls off screen public void run() { while (ball.getY() <= yMax) { ball.move(0, Y_SPEED); pause(DELAY_TIME); } ball.removeFromCanvas(); } }