Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Major method which moves the ball, causes constraints on the ball's movements(within the screen) and bounces the ball
- private void playTurn() {
- double vx = rgen.nextDouble(1.0, 3.0);
- if(rgen.nextBoolean(.5)) vx=-vx;
- double vy = Y_VELOCITY;
- while (true) {
- if(numBricksLeft == 0)
- break;
- if(ball.getY() > APPLICATION_HEIGHT)
- break;
- ball.move(vx,vy);
- pause(ANIMATION_PAUSE);
- vy = reflectOffColliders(vy);
- vx = reflectOffSideWalls(vx);
- vy = reflectOffTopWall(vy);
- }
- }
- /**
- * Change the y velocity if it hits a collider
- * @param vy
- * @return
- */
- private double reflectOffColliders(double vy) {
- GObject collider = getCollidingObject();
- if (collider == paddle) {
- bounceClip.play();
- vy = -Math.abs(vy);
- }
- else if (collider != null) {
- remove(collider);
- vy *= -1;
- }
- return vy;
- }
- /**
- * Get top screen bound and reverse velocity
- * @param vy
- * @return
- */
- private double reflectOffTopWall(double vy) {
- if (ball.getY() < 0) {
- vy *= -1;
- }
- return vy;
- }
- /**
- * Get side bounds and reverse x velocity
- * @param vx
- * @return
- */
- private double reflectOffSideWalls(double vx) {
- if (ball.getX() < 0 || ball.getX() > APPLICATION_WIDTH - ball.getWidth()) {
- vx *= -1;
- }
- return vx;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement