CS125 Fall 2008

Lab # 1

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 in the Asprey Laboratory.  
     
  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.

    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. 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: 

  1. Add a mouseMoved method with the method definition line 

        public void mouseMoved(MouseEvent e) {

    where only the name of the argument can vary. This method detects mouse movement. It should contain code to make the paddle at the bottom of the canvas move horizontally across the canvas when the mouse moves back and forth, like in the game of Pong. The paddle should not move vertically.
      
  2. Modify the CheckForWallBouncing 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.
     
  3. Change the program so that, when the ball misses the paddle and moves off the bottom of the screen, a GLabel "Game over" or "Have a nice day!" (or some other phrase of your choosing)  appears on the canvas.
      
  4. Add a "scoring" capability to the program so that it counts the number of paddle bounces and reports the score when the ball finally falls off the bottom of the screen.
     
  5. Modify the starting position of the ball so that it starts moving at about the vertical midpoint of the canvas instead of at the top.
     
  6. Add a "brick" near the top of 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 too.
     
  7. Show your masterpiece to your professor or our lab coach Sonia before you submit your code. As always, make sure your code is well-documented and properly indented, contains no wrapped lines, all outdated comments have been removed, and that your name is at the top of your program. 

 

 

Submitting your work

      From a terminal window, type the following commands (assuming you save your work in a folder called Lab1):

 
        cd
        cd cs125
        submit125 Lab1

Log out

When you are done, be sure to logout.