cs106a – Assignment #3 – Extra Task #2

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

Add messages

The web version waits for the user to click the mouse before serving each ball and announces whether the player has won or lost at the end of the game. These are just GLabel objects that you can add and remove at the appropriate time.

The “game over” message have already been added before. To use the same label for the “press mouse to start” functionally adjust the previous function, make the label a instance variable and add another function to remove the label:

	private GLabel label;

	private void displayMessage(String message) {
		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);
	}

	private void removeMessage() {
		remove(label);
	}

Add another instance variable which indicates if the game has been started by the user. Adjust the main method to display a start message and wait till the user has pressed the mouse button. If so remove the message and start the actual game:

        private boolean startGame;

	public void run() {
		for (int i = 0; i < NTURNS; i++) {
			displayMessage("press mouse button");
			startGame = false;
			while (!startGame) {
				pause(DELAY * 10);
			}
			removeMessage();
			setupBall();
                        ...
		}
		...
	}

Finally set this new instance variable once the mouse has been clicked:

	public void mouseClicked(MouseEvent e) {
		startGame = true;
	}

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 *