cs106a – Assignment #3 – Extra Task #5

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

Keep score

You could easily keep score, generating points for each brick. In the arcade game, bricks were more valuable higher up in the array, so that you got more points for red bricks than cyan bricks. You could display the score underneath the paddle, since it won’t get in the way there.

Add two new instance variables, one for accessing the label and one for storing the actual score. Setup the score label. Finally adjust the score according to the color of the hid brick:

	private GLabel scoreLabel;
	private int score;

	public void init() {
                ...
		setupScore();
	}

	private void setupScore() {
		score = 0;
		scoreLabel = new GLabel("Score: " + score + " ");
		scoreLabel.setFont("SansSerif-28");
		double x = getWidth() - scoreLabel.getWidth();
		double y = getHeight() - scoreLabel.getAscent();
		scoreLabel.setLocation(x, y);
		add(scoreLabel);
	}
	
	private void addScore(int value) {
		score += value;
		scoreLabel.setLabel("Score: " + score + " ");
		double x = getWidth() - scoreLabel.getWidth();
		double y = scoreLabel.getY();
		scoreLabel.setLocation(x, y);
	}

	private void checkForCollision() {
               ...
		} else if (collider instanceof GRect) {
			if (collider.getColor() == Color.CYAN) addScore(10);
			else if (collider.getColor() == Color.GREEN) addScore(20);
			else if (collider.getColor() == Color.YELLOW) addScore(30);
			else if (collider.getColor() == Color.ORANGE) addScore(40);
			else if (collider.getColor() == Color.RED) addScore(50);
			remove(collider);
               ...
		}
	}

The code for this assignment is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.