Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
TiltPong Demo
A working demo of TiltPong will appear below. Click inside the applet to interact with it.
TiltPong BlueJ Project
Click here to download a BlueJ project for TiltPong.
TiltPong Source Code
The Java source code for TiltPong is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
/*
* Example Pong: a still-simple but playable "Pong" game that features
* a paddle and a falling ball that can now bounce off the sides and
* interact with the paddle, now with the ability to nudge the ball.
*
* Jim Teresco, Siena College, CSIS 120, Spring 2011
* Based on similar example from CSCI 134, Williams College
*
* $Id: TiltPong.java 2322 2014-02-13 02:04:20Z terescoj $
*/
public class TiltPong 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.
private PongBall ball; // newest pong ball created
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);
ball = null;
}
// create a new "nudgeable" pong 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)) {
ball = new PongBall(paddle, boundary, canvas);
}
// if we clicked outside, we should also nudge the most-recently
// created ball
else if (ball != null) {
ball.nudge();
}
}
// 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.*;
import java.text.DecimalFormat;
import java.util.Random;
/*
*
* A simple active object that controls a ball that bounces around the
* canvas and interacts with a boundary and a pong paddle, plus a
* nudge method that allows someone to make a minor change to its
* trajectory.
*
* Jim Teresco, Siena College, CSIS 120, Spring 2011
* The College of Saint Rose, Fall 2013
*
* $Id: PongBall.java 2218 2013-10-18 14:06:39Z terescoj $
*/
public class PongBall extends ActiveObject {
// size and speed of falling balls
private static final int BALLSIZE = 30;
private static final double MIN_SPEED = 3;
private static final double MAX_SPEED = 10;
private static final int DELAY_TIME = 33;
private static final double NUDGE_MAX = 2;
// 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;
// and what about the walls?
private double xMin, xMax;
// speeds are now instance variables since
private double xSpeed, ySpeed;
// label for speeds
private Text speeds;
// random generator
Random randGen = new Random();
// for decimal formatting
DecimalFormat df = new DecimalFormat("#.###");
// Draw a ball and start it falling.
public PongBall(FilledRect paddle, FramedRect boundary,
DrawingCanvas aCanvas) {
// compute/remember our boundaries (of the upper left corner of a ball)
yMax = boundary.getY()+boundary.getHeight();
yMin = boundary.getY();
xMin = boundary.getX();
xMax = boundary.getX() + boundary.getWidth() - BALLSIZE;
// draw the ball at its initial position, which is at the
// top of the playing area, at a randomly-selected position
ball = new FilledOval(randGen.nextDouble() * (xMax - xMin) + xMin,
yMin+1, BALLSIZE, BALLSIZE, aCanvas);
// remember the paddle
this.paddle = paddle;
// A label for the speeds
speeds = new Text("", 10, 10, aCanvas);
// activate!
start();
}
// our "nudge" method -- make a minor change in the trajectory
// and temporarily make the ball red
public void nudge() {
// randomly perturb both the x and y speeds
xSpeed = xSpeed + randGen.nextDouble() * (2*NUDGE_MAX) - NUDGE_MAX;
ySpeed = ySpeed + randGen.nextDouble() * (2*NUDGE_MAX) - NUDGE_MAX;
speeds.setText("xSpeed = " + df.format(xSpeed) +
", ySpeed = " + df.format(ySpeed));
ball.setColor(Color.red);
}
// move the ball repeatedly until it falls off screen, bouncing
// off the paddle and walls along the way
public void run() {
// start by moving downward and some amount to the left or right
xSpeed = randGen.nextDouble() * (MAX_SPEED - MIN_SPEED) + MIN_SPEED;
double initYSpeed = randGen.nextDouble() * (MAX_SPEED - MIN_SPEED) + MIN_SPEED;
ySpeed = initYSpeed;
// 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 = initYSpeed;
}
// if we are in contact with the paddle, start moving up
if (ball.overlaps(paddle)) {
ySpeed = -initYSpeed;
}
// bounce off side walls
if (ball.getX() < xMin || ball.getX() > xMax) {
xSpeed = -xSpeed;
}
// move a little in the appropriate direction
speeds.setText("xSpeed = " + df.format(xSpeed) +
", ySpeed = " + df.format(ySpeed));
ball.move(xSpeed, ySpeed);
speeds.moveTo(ball.getX() + BALLSIZE, ball.getY() + BALLSIZE);
ball.setColor(Color.black);
pause(DELAY_TIME);
}
ball.removeFromCanvas();
speeds.removeFromCanvas();
}
}