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:
Continue reading “cs106a – Assignment #3 – Extra Task #5”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

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:
Continue reading “cs106a – Assignment #3 – Extra Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #3 – Extra Task #3

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

Improve the user control over bounces

The program gets rather boring if the only thing the player has to do is hit the ball. It is far more interesting, if the player can control the ball by hitting it at different parts of the paddle. The way the old arcade game worked was that the ball would bounce in both the x and y directions if you hit it on the edge of the paddle from which the ball was coming. The web version implements this feature.

First calculate the position where the ball hits the paddle so that -1 signifies a hit on the left corner and +1 a hit on the right corner. Change the horizontal speed so that a hit in the center (0) does not do anything, a hit in the corner from the direction from which the ball arrived changes the direction and a hit on the other corner increases the horizontal speed. Finally to keep the ball from speeding up, adjust the vertical speed so that the overall velocity stayes the same:
Continue reading “cs106a – Assignment #3 – Extra Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

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:
Continue reading “cs106a – Assignment #3 – Extra Task #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #3 – Extra Task #1

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

Add sounds

The version that is running as an applet on the CS 106A assignment page plays a short bounce sound every time the ball collides with a brick or the paddle. This extension turns out to be very easy. The starter project contains an audio clip file called bounce.au that contains that sound. You can load the sound by writing

AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");

and later play it by calling

bounceClip.play();

The Java libraries do make some things easy.

As mentioned above add bounceClip as instance variable:
Continue reading “cs106a – Assignment #3 – Extra Task #1”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

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. Continue reading “cs106a – Assignment #3 – Task #5”
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #3 – Task #4

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

Checking for collisions

Now comes the interesting part. In order to make Breakout into a real game, you have to be able to tell whether the ball is colliding with another object in the window. As scientists often do, it helps to begin by making a simplifying assumption and then relaxing that assumption later. Suppose the ball were a single point rather than a circle. In that case, how could you tell whether it had collided with another object?
If you look in Chapter 9 (page 299) at the methods that are defined at the GraphicsProgram level, you will discover that there is a method

public GObject getElementAt(double x, double y)

that takes a position in the window and returns the graphical object at that location, if any. If there are no graphical objects that cover that position, getElementAt returns the special constant null. If there is more than one, getElementAt always chooses the one closest to the top of the stack, which is the one that appears to be in front on the display.
Continue reading “cs106a – Assignment #3 – Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail