zopiac

BouncingBall

Sep 17th, 2010
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. //BouncingBall with gravity and 10 second time limit (before stopping).
  2. import acm.program.*;
  3. import acm.graphics.*;
  4. import java.awt.*;
  5.  
  6. public class BouncingBall extends GraphicsProgram {
  7.     public void run() {
  8.        GOval ball = new GOval(25, 25, 50, 50);
  9.        ball.setFilled(true);
  10.        ball.setFillColor(new Color(255, 0, 0));
  11.        add(ball);
  12.        int frame = 0;
  13.        double dx = 3;
  14.        double dy = -4;
  15.        while(frame < 1000) {
  16.            pause(10);
  17.            if(ball.getX() + ball.getWidth() >= getWidth()
  18.                    || ball.getX() <= 0.0) {
  19.                dx = -dx;
  20.                dx = dx * .8;
  21.            }
  22.            if(ball.getY() + ball.getHeight() >= getHeight()
  23.                    || ball.getY() <= 0.0) {
  24.                dy = -dy;
  25.                dy = dy * .8;
  26.            }
  27.            dy = dy + 0.2;
  28.            ball.move(dx, dy);
  29.            frame = frame + 1;
  30.        }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment