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

cs106a – Assignment #3 – Task #1

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

Set up the bricks

Before you start playing the game, you have to set up the various pieces. Thus, it probably makes sense to implement the run method as two method calls: one that sets up the game and one that plays it. An important part of the setup consists of creating the rows of bricks at the top of the game, which look like this:

cs106a – assignment #3 – task #1

The number, dimensions, and spacing of the bricks are specified using named constants in the starter file, as is the distance from the top of the window to the first line of bricks. The only value you need to compute is the x coordinate of the first column, which should be chosen so that the bricks are centered in the window, with the leftover space divided equally on the left and right sides. The color of the bricks remain constant for two rows and run in the following rainbow-like sequence: RED, ORANGE, YELLOW, GREEN, CYAN.

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #4

In high-school geometry, you learned the Pythagorean theorem for the relationship of the lengths of the three sides of a right triangle:
a2 + b2 = c2
which can alternatively be written as:
c = √(a2 + b2)
Most of this expression contains simple operators covered in Chapter 3. The one piece that’s missing is taking square roots, which you can do by calling the standard function Math.sqrt. For example, the statement

double y = Math.sqrt(x);

sets y to the square root of x.
Write a ConsoleProgram that accepts values for a and b as ints and then calculates the solution of c as a double. Your program should be able to duplicate the following sample run:

cs106a – assignment #2 – task #4

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #3

Write a GraphicsProgram subclass that draws a partial diagram of the acm.program class hierarchy, as follows:

cs106a – assignment #2 – task #3

The only classes you need to create this picture are GRect, GLabel, and GLine. The major part of the problem is specifying the coordinates so that the different elements of the picture are aligned properly. The aspects of the alignment for which you are responsible are:

  • The width and height of the class boxes should be specified as named constants so that they are easy to change.
  • The labels should be centered in their boxes. You can find the width of a label by calling label.getWidth() and the height it extends above the baseline by calling label.getAscent(). If you want to center a label, you need to shift its origin by half of these distances in each direction.
  • The connecting lines should start and end at the center of the appropriate edge of the box.
  • The entire figure should be centered in the window.

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail