Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BouncingBall with gravity and 10 second time limit (before stopping).
- import acm.program.*;
- import acm.graphics.*;
- import java.awt.*;
- public class BouncingBall extends GraphicsProgram {
- public void run() {
- GOval ball = new GOval(25, 25, 50, 50);
- ball.setFilled(true);
- ball.setFillColor(new Color(255, 0, 0));
- add(ball);
- int frame = 0;
- double dx = 3;
- double dy = -4;
- while(frame < 1000) {
- pause(10);
- if(ball.getX() + ball.getWidth() >= getWidth()
- || ball.getX() <= 0.0) {
- dx = -dx;
- dx = dx * .8;
- }
- if(ball.getY() + ball.getHeight() >= getHeight()
- || ball.getY() <= 0.0) {
- dy = -dy;
- dy = dy * .8;
- }
- dy = dy + 0.2;
- ball.move(dx, dy);
- frame = frame + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment