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

cs106a – Assignment #3 – Task #3

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

Create a ball and get it to bounce off the walls

At one level, creating the ball is easy, given that it’s just a filled GOval. The interesting part lies in getting it to move and bounce appropriately. You are now past the “setup” phase and into the “play” phase of the game. To start, create a ball and put it in the center of the window. As you do so, keep in mind that the coordinates of the GOval do not specify the location of the center of the ball but rather its upper left corner. The mathematics is not any more difficult, but may be a bit less intuitive.
The program needs to keep track of the velocity of the ball, which consists of two separate components, which you will presumably declare as instance variables like this:

private double vx, vy;

Continue reading “cs106a – Assignment #3 – Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #3 – Task #2

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

Create the paddle

The next step is to create the paddle. At one level, this is considerably easier than the bricks. There is only one paddle, which is a filled GRect. You even know its position relative to the bottom of the window.
The challenge in creating the paddle is to make it track the mouse. The technique is similar to that discussed in Chapter 9 for dragging an object around in the window. Here, however, you only have to pay attention to the x coordinate of the mouse because the y position of the paddle is fixed. The only additional wrinkle is that you should not let the paddle move off the edge of the window. Thus, you’ll have to check to see whether the x coordinate of the mouse would make the paddle extend beyond the boundary and change it if necessary to ensure that the entire paddle is visible in the window.

Continue reading “cs106a – Assignment #3 – Task #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail