Computer Science 120
Introduction to Programming

Fall 2011, Siena College

LessPatheticPong Demo

A working demo of LessPatheticPong will appear below. Click inside the applet to interact with it.



LessPatheticPong BlueJ Project

Click here to download a BlueJ project for LessPatheticPong.


LessPatheticPong Source Code

The Java source code for LessPatheticPong is below. Click on a file name to download it.


LessPatheticPong.java

import objectdraw.*;
import java.awt.*;

/*
 * Example LessPatheticPong: a too-simple "Pong" game that features
 * a paddle and a falling ball that can now interact in a simple way.
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 * Based on similar example from CSCI 134, Williams College
 *
 * $Id: LessPatheticPong.java 1549 2011-02-22 04:01:53Z terescoj $
 */

public class LessPatheticPong 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,
            COURT_TOP + COURT_HEIGHT - PADDLE_HEIGHT -1,
            PADDLE_WIDTH, PADDLE_HEIGHT,
            canvas);
    }

    // create a new falling ball
    public void onMouseClick(Location point) {

        // create the ball at the top of the playing area
        // at the mouse point's x coordinate
        if (boundary.contains(point)) {
            new SimplePongBall(new Location(point.getX(), COURT_TOP), 
                    paddle, 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);
        }

    }
}

SimplePongBall.java

import objectdraw.*;
import java.awt.*;

/*
 * 
 * A simple active object that controls a ball that falls down the
 * canvas and interacts with a pong paddle
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 *
 * $Id: SimplePongBall.java 1549 2011-02-22 04:01:53Z terescoj $
 */

public class SimplePongBall 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;

    // the paddle with which we will interact
    private FilledRect paddle;

    // how far to fall before stopping and disappearing?
    private double yMax;
    // how far up to go before bouncing off the ceiling?
    private double yMin;

    // Draw a ball and start it falling.
    public SimplePongBall(Location start, FilledRect paddle, 
                          double courtTop, DrawingCanvas aCanvas) {

        // draw the ball at its initial position
        ball = new FilledOval(start.getX() - BALLSIZE/2,
            start.getY(),
            BALLSIZE, BALLSIZE, aCanvas);

        // ask the canvas how big it is so we know when to stop
        yMax = aCanvas.getHeight();

        // remember the top of the court as the minimum y
        yMin = courtTop;

        // remember the paddle
        this.paddle = paddle;

        // activate!
        start();
    }

    // move the ball repeatedly until it falls off screen, bouncing
    // off the paddle and the ceiling along the way
    public void run() {
        double ySpeed;
        
        // start by moving downward
        ySpeed = Y_SPEED;
        
        // keep moving as long as we haven't fallen off the bottom of
        // the screen
        while (ball.getY() <= yMax) {
            // if we are above the top line, start moving down
            if (ball.getY() < yMin) {
                ySpeed = Y_SPEED;
            }
            
            // if we are in contact with the paddle, start moving up
            if (ball.overlaps(paddle)) {
                ySpeed = -Y_SPEED;
            }
            
            // move a little in the appropriate direction
            ball.move(0, ySpeed);
            pause(DELAY_TIME);
        }
        ball.removeFromCanvas();
    }
}