/******************************************************************************
* Name:
* NetID:
* Precept:
*
* Description: Upgraded version of BouncingBall.java, as seen in Lecture 4D.
******************************************************************************/
public class BouncingBallDeluxe {
public static void main(String[] args) {
// initial values
double rx = 0.480, ry = 0.860; // position
double vx = 0.015, vy = 0.023; // velocity
double radius = 0.05; // radius
// set the scale of the coordinate system
StdDraw.setXscale(-1.0, 1.0);
StdDraw.setYscale(-1.0, 1.0);
// main animation loop
while (true) {
// set the background to light gray
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
StdDraw.filledSquare(0.0, 0.0, 1.0);
// bounce off wall according to law of elastic collision
if (Math.abs(rx + vx) + radius > 1.0) {
vx = -vx;
StdAudio.play("pipebang.wav");
}
if (Math.abs(ry + vy) + radius > 1.0) {
vy = -vy;
StdAudio.play("pipebang.wav");
}
// update position
rx = rx + vx;
ry = ry + vy;
// draw ball on the screen
StdDraw.picture(rx, ry, "TennisBall.png");
// display and pause for 20 ms
StdDraw.show(20);
}
}
}