CS102 2017

Lab # 11

Breakout, Phase 1:

 

In this lab, you will write a graphics program using the acm library. Remember, it is good programming practice to save and compile your program frequently, making sure it runs correctly after each incremental change you make. Maintaining working code at every phase of a project is a good practice, so to start you will download some working starter code.   

 

How to get started

  1. Login to one of the Linux-based machines.  
     
  2. Launch DrJava or whichever editor or IDE you want to use.   
     
  3. Download graphics program  Breakout  as starter code. You will also receive a paper copy of this program so you can see it both on-line and on-screen. This file will be your starting point for the lab today and for the next assignment.  You will also need to have the acm.java file on your machine and have your IDE know the location of the acm.jar file.

    The  Breakout  program features a red ball moving and bouncing off the walls of a canvas. At the bottom of the canvas, there is a rectangle that represents a user-controllable paddle. In the version you download, the ball bounces when it contacts either the edges of the canvas or the paddle, and the paddle is stationary.

    This program starts by displaying the GLabel "Click to Start!".  A MouseListener detects when the user clicks the mouse button while the mouse pointer is within the boundaries of the canvas. This first mouse click causes the Listener to generate a MouseEvent that is sent to the mouseClicked method. The mouseClicked method modifies a boolean instance variable and is also sensed by the waitForClick method, called in the runGame  method, creating a red ball at a random position at the top of the canvas and to start the ball moving. After that, the playBall method keeps the ball moving until you close the window.  
     
    Before you get started making changes to the starter code, take a moment to read through it and through these directions. There are many comments to explain the function of each method and to give you an idea of what your own documentation should look like. Execution begins in the  main method and the call to start causes an unseen call to the run method.  After the run method begins, the execution should be fairly straightforward to trace. Most of the methods called on the GObject classes and the GraphicsProgram class have descriptive names.  For any method calls you don't understand, consult the API for the acm.jar package (the link is on the course web page.) Feel free to ask your professor about anything you need clarified.
     
  4. Modify the starter code to the program described in the next section, Work to be Done.   

 

Work to be Done:

 

For this lab, you are to modify the starter code in the following ways:  (These tasks are scattered throughout the code, but you should do them in order and compile and test the code after every change.)

  1. TASK 1:

    Add a mouseMoved method with the method definition line 

        public void mouseMoved(MouseEvent e) {

    where only the name of the argument e can vary. This method detects mouse movement. It should contain code to cause the paddle at the bottom of the canvas to move horizontally across the canvas when the mouse moves back and forth. The paddle should not move vertically. Also, the paddle should not move off the scene to the left or right.

     
  2. TASK 2:

    Modify the checkForWallBounces method so that the ball does not bounce off the bottom wall of the canvas. After this change, the ball should bounce at the bottom of the canvas only if it hits the paddle. If it misses the paddle, it should keep on going but the game should end.

     
  3. TASK 3:

    Modify the starting position of the ball so that it starts moving downward at about the vertical midpoint of the canvas instead of at the top. This will require a change to the createBall method. The x coordinate the ball starts at should still be random. 

     
  4. TASK 4:

    Add a "brick" about a quarter way down on the canvas that will disappear when the ball contacts it.  For now, make the brick almost as wide as the canvas so the ball can't miss it. The ball should bounce off this brick in the same way it bounces off the paddle, only in this case the brick should disappear. This will require a change to the checkForObjectBounces method and to the setUpGame method. The brick and the paddle should appear when the game starts.


  5. TASK 5:

    Change the program so that, when the ball misses the paddle and moves off the bottom of the screen, a GLabel "Game over" appears on the canvas and the ball stops moving. This will require changes to the checkForWallBounces method.

    Add a "scoring" capability to the program so that it counts the number of paddle bounces and reports the score after the ball finally falls off the bottom of the screen, along with the "Game over" message.  It is your choice where to implement the counting and the final appearance of this message.



    Show your program to your professor or one of our lab coaches before you submit your code. As always, make sure your code is well-documented and properly indented, contains no wrapped lines, has all outdated comments removed, and that your name is at the top of your program.  You will be modifying the file as part of the next homework project.

 

 

Submitting your work

     Compress your lab11 folder and submit it on our course Moodle page.