The following should be in the play() method.
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? What happens if you call
getElement(x, y)
where x and y are the coordinates of the ball? If the point (x,y) is underneath an object, this call returns the graphical object with which the ball has collided. If there are no objects at the point (x,y), youll get the value null.
So far, so good. But, unfortunately, the ball is not a single point. It occupies physical area and therefore may collide with something on the screen even though its center does not. The easiest thing to do which is in fact typical of the simplifying assumptions made in real computer games is to check a few carefully chosen points on the outside of the ball and see whether any of those points has collided with anything. As soon as you find something at one of those points, you can
declare that the ball has collided with that object.
In your implementation, the easiest thing to do is to check the four corner points on the square in which the ball is inscribed. Remember that a GOval is defined in terms of its bounding rectangle so that if the upper left corner of the ball is at the point (x, y), the other corners will be at the locations shown in this diagram:
These points have the advantage of being outside the ball which means that getElementAt cant return the ball itself but nonetheless close enough to make it appear that collisions have occurred. Thus, for each of these four points, you need to:
It would be very useful to write this section of code as a separate method:
private GObject getCollidingObject()
that returns the object involved in the collision, if any, and null otherwise. You could then use it in a declaration like:
GObject collider = getCollidingObject();
which assigns that value to a variable called collider.
From here, the only remaining thing you need to do is decide what to do when a collision occurs. There are only two possibilities. First, the object you get back might be the paddle, which you can test by checking
if (collider == paddle) . . .
If it is the paddle, you need to bounce the ball so that it starts traveling up. If it isn’t the paddle, the only other thing it might be is a brick, since those are the only other objects in the world. Once again, you need to cause a bounce in the vertical direction, but you also need to take the brick away. To do so, all you need to do is remove it from the screen by calling the remove method.