Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void moveBall(GOval ball, int height, int width, GRect paddle) {
- // Update ball position
- ball.setLocation(ball.getX() + vx, ball.getY() + vy);
- // Make sure ball Y is within the area
- if (ball.getY() <= 0) {
- ball.setLocation(ball.getX(), 0);
- vx += getVelOff();
- vy = Math.abs(vy);
- } else if (ball.getY() + BALL_RADIUS * 2 >= height) {
- // The ball has reached the bottom of the screen
- turn ++;
- ball.setLocation(width / 2, height / 2);
- // TODO: Wait for the player to be ready
- }
- // Make sure ball X is within the area
- if (ball.getX() <= 0) {
- ball.setLocation(0, ball.getY());
- vx = Math.abs(vx);
- vy += getVelOff();
- } else if (ball.getX() + BALL_RADIUS * 2 >= width) {
- ball.setLocation(width - BALL_RADIUS * 2, ball.getY());
- vx = -Math.abs(vx);
- vy += getVelOff();
- }
- // Get nearby objects
- GObject yn = getElementAt(ball.getX() + BALL_RADIUS, ball.getY());
- GObject yp = getElementAt(ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS * 2);
- GObject xn = getElementAt(ball.getX(), ball.getY() + BALL_RADIUS);
- GObject xp = getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS);
- GObject center = getElementAt(ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS);
- // yn collision
- if (
- // check if yn exists
- yn != null &&
- // check if the ball's bottom surface is below or at yn's top surface
- ball.getY() <= yn.getY() + yn.getHeight() &&
- // check if the ball's x location is within yn's bounds
- ball.getX() + BALL_RADIUS * 2 >= yn.getX() && ball.getX() <= yn.getX() + yn.getWidth()
- ) {
- // The ball has hit the top of the yn brick
- ball.setLocation(ball.getX(), yn.getY() + yn.getHeight());
- vx += getVelOff();
- vy = Math.abs(vy);
- remove(yn);
- }
- // yp collision
- if (
- // check if yp exists
- yp != null &&
- // check if the ball's top surface is above or at yp's bottom surface
- ball.getY() + BALL_RADIUS * 2 >= yp.getY() &&
- // check if the ball's x location is within yp's bounds
- ball.getX() + BALL_RADIUS * 2 >= yp.getX() && ball.getX() <= yp.getX() + yp.getWidth()
- ) {
- // The ball has hit the bottom of the yp brick
- ball.setLocation(ball.getX(), yp.getY() - BALL_RADIUS * 2);
- vx += getVelOff();
- vy = -Math.abs(vy);
- remove(yp);
- }
- // xn collision
- if (
- // check if xn exists
- xn != null &&
- // check if the ball's left surface is left of or at xn's right surface
- ball.getX() <= xn.getX() + xn.getWidth() &&
- // check if the ball's y location is within xn's bounds
- ball.getY() + BALL_RADIUS * 2 >= xn.getY() && ball.getY() <= xn.getY() + xn.getHeight()
- ) {
- // The ball has hit the right side of the xn brick
- ball.setLocation(xn.getX() + xn.getWidth(), ball.getY());
- vx = Math.abs(vx);
- vy += getVelOff();
- remove(xn);
- }
- // xp collision
- if (
- //check if xp exists
- xp != null &&
- // check if the ball's right surface is right of or at xp's left surface
- ball.getX() + BALL_RADIUS * 2 >= xp.getX() &&
- // check if the ball's y location is within xn's bounds
- ball.getY() + BALL_RADIUS * 2 >= xp.getY() && ball.getY() <= xp.getY() + xp.getHeight()
- ) {
- // The ball has hit the left side of the xp brick
- ball.setLocation(xn.getX() - BALL_RADIUS * 2, ball.getY());
- vx = -Math.abs(vx);
- vy += getVelOff();
- remove(xp);
- }
- // center collisions
- if (center != null) {
- // top collision
- if (
- // check if the ball's bottom surface is below or at center's top surface
- ball.getY() <= center.getY() + center.getHeight() &&
- // check if the ball's x location is within center's bounds
- ball.getX() + BALL_RADIUS * 2 >= center.getX() && ball.getX() <= center.getX() + center.getWidth()
- ) {
- // The ball has hit the top of the center brick
- ball.setLocation(ball.getX(), center.getY() + center.getHeight());
- vx += getVelOff();
- vy = Math.abs(vy);
- remove(center);
- }
- // bottom collision
- if (
- // check if the ball's top surface is above or at center's bottom surface
- ball.getY() + BALL_RADIUS * 2 >= center.getY() &&
- // check if the ball's x location is within center's bounds
- ball.getX() + BALL_RADIUS * 2 >= center.getX() && ball.getX() <= center.getX() + center.getWidth()
- ) {
- // The ball has hit the bottom of the center brick
- ball.setLocation(ball.getX(), center.getY() - BALL_RADIUS * 2);
- vx += getVelOff();
- vy = -Math.abs(vy);
- remove(center);
- }
- // right collision
- if (
- // check if the ball's left surface is left of or at center's right surface
- ball.getX() <= center.getX() + center.getWidth() &&
- // check if the ball's y location is within center's bounds
- ball.getY() + BALL_RADIUS * 2 >= center.getY() && ball.getY() <= center.getY() + center.getHeight()
- ) {
- // The ball has hit the right side of the center brick
- ball.setLocation(center.getX() + center.getWidth(), ball.getY());
- vx = Math.abs(vx);
- vy += getVelOff();
- remove(center);
- }
- // left collision
- if (
- // check if the ball's right surface is right of or at center's left surface
- ball.getX() + BALL_RADIUS * 2 >= center.getX() &&
- // check if the ball's y location is within center's bounds
- ball.getY() + BALL_RADIUS * 2 >= center.getY() && ball.getY() <= center.getY() + center.getHeight()
- ) {
- // The ball has hit the left side of the center brick
- ball.setLocation(center.getX() - BALL_RADIUS * 2, ball.getY());
- vx = -Math.abs(vx);
- vy += getVelOff();
- remove(center);
- }
- }
- }
- // generates a random velocity offset
- public static double getVelOff() {
- return RandomGenerator.getInstance().nextDouble(-1.5, 1.5);
- }
Add Comment
Please, Sign In to add comment