Breakout

Eric Roberts, eroberts@cs.stanford.edu, Stanford University
Summary This assignment plays the classic video arcade game of Breakout, in which the player bounces a ball against rows of bricks at the top of the screen to dislodge them one at a time. The name of the game arises from the fact that the ball will eventually break through the line of bricks and bounce off the top wall, clearing a large number of bricks before the ball returns to the paddle.
Topics Basic coding: using the acm.graphics package, nested for loops to position the rows of bricks, simple animation of a ball based on applying dx and dy displacements at uniform time steps, if tests to check for bounces and collisions. Interestingly, this assignment requires no arrays or compound data structures.
Audience Early CS1. At Stanford, Breakout was the second Java assignment.
Difficulty Surprisingly straightforward, even though the assignment requires a more code than the average assignment at the beginning of CS1. Teaching assistants at Stanford reported that the students “just nailed this assignment.” It was also used successfully at Grand Valley State University and Alfred State College.
Strengths The real strength of this assignment is that students are able to create an enjoyable and eminently playable game in the first few weeks of the term, which serves to increase their excitement about the material. It also provides students with an opportunity to practice placing graphical objects on the screen, which prepares them for other assignments involving the acm.graphics package.
Weaknesses This project seems to have worked very well at the three very different institutions at which it was tried (a research university, a large public university, and a two-year community college). The only weakness I can think of is that the animation can be jerky when the program is running in a browser on a slow machine. This problem does not seem to arise when it is run as an application.
Dependencies Requires the use of the acm.graphics package developed by the Java Task Force.
Variants Students were encouraged to extend the assignment in a variety of ways, including the following:
  • Adding sounds for bounces and clearing bricks.
  • Adding an instructions screen or other user messages.
  • Allowing the user to control the direction of bounces depending on where the ball hits the paddle.
  • Implementing the feature from the arcade game in which the ball started off slowly but increased its velocity after the seventh bounce off the paddle.
  • Keeping score.

Breakout

This assignment plays the classic arcade game of Breakout. In Breakout, the initial configuration of the world appears as shown on the right. The colored rectangles in the top part of the screen are bricks, and the slightly larger rectangle at the bottom is the paddle. The paddle is in a fixed position in the vertical dimension, but moves back and forth across the screen along with the mouse until it reaches the edge of its space.

A complete game consists of three turns. On each turn, a ball is launched from the center of the window toward the bottom of the screen at a random angle. The ball bounces off the paddle and the walls of the world, in accordance with the physical principle generally expressed as “the angle of incidence equals the angle of reflection” (which turns out to be very easy to implement). Thus, after two bounces—one off the paddle and one off the right wall—the ball might have the trajectory shown in the second diagram. (Note that the dotted line is there only to show the ball’s path and won’t actually appear on the screen.)

As you can see from the second diagram, the ball is about to collide with one of the bricks on the bottom row. When that happens, the ball bounces just as it does on any other collision, but the brick disappears. The third diagram shows what the game looks like after that collision and after the player has moved the paddle to put it in line with the oncoming ball.

The play on a turn continues in this way until one of two conditions occurs:

  1. The ball hits the lower wall, which means that the player must have missed it with the paddle. In this case, the turn ends and the next ball is served if the player has any turns left. If not, the game ends in a loss for the player.
  2. The last brick is eliminated. In this case, the player wins, and the game ends immediately.

After all the bricks in a particular column have been cleared, a path will open to the top wall. When this delightful situation occurs, the ball will often bounce back and forth several times between the top wall and the upper line of bricks without the user ever having to worry about hitting the ball with the paddle. This condition is a reward for “breaking out” and was the inspiration for the name of the game. The diagram on the right shows the situation shortly after the first ball has broken through the wall. That ball will go on to clear several more bricks before it comes back down the open channel.

Talk

Click here to view the slides used in the “Nifty Assignments” talk.

Demo

Click here to run the Breakout game as an applet.

Handout

The handout for the assignment used at Stanford is available in PDF form here.