cs106a – Assignment #3 – Extra Task #4

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

Add in the “kicker”

The arcade version of Breakout lured you in by starting off slowly. But, as soon as you thought you were getting the hang of things, the program sped up, making life just a bit more exciting. The applet version implements this feature by doubling the horizontal velocity of the ball the seventh time it hits the paddle, figuring that’s the time the player is growing complacent.

Add a constant to hold the given number of paddle hits for which the velocity should double, and an instance variable to count the hits. Reset this variable when the ball is setup. Finally adjust the collision checker to count the hits and increase the horizontal velocity after the given count of hits:

	private static final int SUCCESSFULL_PADDLEHITS_BEFORE_KICKER = 7;

	private int numberOfPaddleHits;

	private void setupBall() {
		...
		numberOfPaddleHits = 0;
	}

	private void checkForCollision() {
		...
			double hitPosition = (2 * (x - paddle.getX()) + d - PADDLE_WIDTH) / (d + PADDLE_WIDTH);
			if (hitPosition < 0) {
				vx = -Math.abs(vx);
			} else {
				vx = Math.abs(vx);
			}
			numberOfPaddleHits++;
			if (numberOfPaddleHits % SUCCESSFULL_PADDLEHITS_BEFORE_KICKER == 0) {
				vx *= 2;
			}
			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 *