cs106a – Assignment #3 – Task #5

The complete specification of assignment #3 can be found as part of the stream at iTunes.

Finishing up

If you’ve gotten to here, you’ve done all the hard parts. There are, however, a few more details you need to take into account:

  • You’ve got to take care of the case when the ball hits the bottom wall. In the prototype you’ve been building, the ball just bounces off this wall like all the others, but that makes the game pretty hard to lose. You’ve got to modify your loop structure so that it tests for hitting the bottom wall as one of its terminating conditions.
  • You’ve got to check for the other terminating condition, which is hitting the last brick. How do you know when you’ve done so? Although there are other ways to do it, one of the easiest is to have your program keep track of the number of bricks remaining. Every time you hit one, subtract one from that counter. When the count reaches zero, you must be done. In terms of the requirements of the assignment, you can simply stop at that point, but it would be nice to give the player a little feedback that at least indicates whether the game was won or lost.
  • You’ve got to experiment with the settings that control the speed of your program. How long should you pause in the loop that updates the ball? Do you need to change the velocity values to get better play action?
  • You’ve got to test your program to see that it works. Play for a while and make sure that as many parts of it as you can check are working. If you think everything is working, here is something to try: Just before the ball is going to pass the paddle level, move the paddle quickly so that the paddle collides with the ball rather than vice-versa. Does everything still work, or does your ball seem to get “glued” to the paddle? If you get this error, try to understand why it occurs and how you might fix it.

To know how many bricks are left, add a counter which is set in setupBricks() and adjusted when a brick is removed:

	private int numberOfBricks;
        ...
        private void setupBricks() {
        ...
		numberOfBricks = NBRICK_ROWS * NBRICKS_PER_ROW;
	}
        ...
	private void checkForCollision() {
        ...
		} else if (collider != null) {
			remove(collider);
			numberOfBricks--;
			vy = -vy;
		}
	}

The infinite loop is adjusted to stop when no bricks are left, or the ball has been lost 3 times:

	public void run() {
		for (int i = 0; i < NTURNS; i++) {
			setupBall();
			while ((ball.getY() < HEIGHT) && (numberOfBricks > 0)) {
				moveBall();
				checkForCollision();
				pause(DELAY);
			} 			
			if (numberOfBricks == 0) {
				gameOver("Winner!");
				return;					
			}
		}
		gameOver("Game Over");
	}

Note that the code above displays a game over message, when the game has been won or lost:

	private void gameOver(String message) {
		GLabel label = new GLabel(message);
		label.setFont("SansSerif-28");
		double x = (getWidth() - label.getWidth()) / 2;
		double y = (getHeight() + label.getAscent()) / 2;
		label.setLocation(x, y);
		add(label);
	}

The problem with the glued ball appears when the direction of the ball is changed when the ball has “nearly” passed the paddle, and the direction is changed again when after the delay because, it had not time to “leave” the area of the paddle. This can be prevented by never actually let the ball “enter” the paddle by adjusting its position:

	private void checkForCollision() {
		double x = ball.getX();
		double y = ball.getY();
		double d = 2 * BALL_RADIUS;
		if (y < 0) {
			vy = -vy;
			ball.move(0, -2 * y);
		}
		if (x > WIDTH - d) {
			vx = -vx;
			ball.move(-2 * (x - WIDTH + d), 0);
		}
		if (x < 0) {
			vx = -vx;
			ball.move(-2 * x, 0);
		}
		
		GObject collider = getCollidingObject();
		if (collider == paddle) {
			vy = -vy;
			ball.move(0, -2 * (y + d - HEIGHT + PADDLE_Y_OFFSET));
		} else if (collider != null) {
			remove(collider);
			numberOfBricks--;
			vy = -vy;
		}
	}

The code for this assignment is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *