Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 2a: Create the ball and Bounce off the Walls

A. To begin, you’ll want to create a createBall() method that creates a ball out of a GOval.

At one level, creating the ball is easy, given that its 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;

The velocity components represent the change in position that occurs on each time step. Initially, the ball should be heading downward, and you might try a starting velocity of +3.0 for vy (remember that y values in Java increase as you move down the screen). The game would be boring if every ball took the same course, so you should choose the vx component randomly. In generating random numbers, you should simply do the following:

  1. Declare an instance variable rgen, which will serve as a random-number generator:
private RandomGenerator rgen = RandomGenerator.getInstance()

Remember that instance variables are declared outside of any method but inside the class.

2. Initialize the vx variable as follows:

vx = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(0.5)) vx = -vx;

This code sets vx to be a random double in the range of 1.0 to 3.0 and then makes it negative half the time. This strategy works much better for Breakout than calling

nextDouble(-3.0, +3.0)

which might generate a ball going more or less straight down. That would make life far too
easy for the player.

3. You may use the same strategy for vy. This will create a specific velocity for the ball in a random direction.

B. At this point, you’ll want to create a play() method that runs the game in a loop. Make sure to call the play() method in run().

4. In the play() method, use the move method from ball. The following line assumes your ball variable is called “ball”.

while(true)
{
    ball.move(vx, vy);
}

Bouncing

Once youve done that, your next challenge is to get the ball to bounce around the world, ignoring entirely the paddle and the bricks (for now). To do so, you need to check to see if the coordinates of the ball have gone beyond the boundary, taking into account that the ball has a nonzero size. Thus, to see if the ball has bounced off the right wall, you need to see whether the coordinate of the right edge of the ball has become greater than the width of the window; the other three directions are treated similarly. For now, have the ball bounce off the bottom wall so that you can watch it make its path around the world. You can change that test later so that hitting the bottom wall signifies the end of a turn.

Computing what happens after a bounce is extremely simple. If a ball bounces off the top or bottom wall, all you need to do is reverse the sign of vy. Symmetrically, bounces off the side walls simply reverse the sign of vx.

So,

vy = -vy;

OR

vx = -vx;

This would also be called in the play() method’s loop.