CS21 Lab 11: Pong

Due 11:59pm Tuesday, December 7, 2010

Run update21 to create the cs21/labs/11 directory. Then cd into your cs21/labs/11 directory and create the python program for the lab in this directory (handin21 looks for your solutions here).

You should work on your own for this lab.


Introduction

You will use object-oriented programming and the graphics library to create a single player version of the Pong video game. The game consists of a ball and a paddle, as shown below. The ball will bounce around the window and the player must move the paddle vertically to try to contact the ball and keep it in play. If the ball ever reaches the edge of the right side of the screen the game ends. In order to make the game a little more challenging, after every fifth time the player hits the ball, its speed will increase slightly.



Create your solution for this game incrementally. First define and test a class for representing the ball. Next define and test a class for handling the game itself.

1. Defining the Ball class

We have begun the definition of this class for you in the file ball.py. It contains the constructor, a method called checkContact, and a short main program as an initial test. Run this program and be sure you understand what the constructor does.

Notice that the constructor stores a number of instance variables. One called self.circ stores the circle that represents the ball in the graphics window. Two others, called self.dx and self.dy, define how the ball will move in the horizontal and vertical direction. Larger values for these variables will make the ball move faster. These are initially set to random values that will make the ball move at a reasonable starting speed.

You will need to define the following methods within the Ball class:


2. Testing the Ball class

  1. Update the main program within the file ball.py to include a while True loop. Within this loop, invoke the move method on each of the balls. Be sure to include a sleep(0.01) within the loop so that you can actually see the balls move. When you run the updated version of the program you should see all three balls continually bouncing around the graphics window. To stop this program you'll have to do CTRL-C.
  2. It would be nice if we could use a mouse click to end this program, but since we have active objects continually updating within the window, the getMouse method will not work. The problem is that this method waits for a mouse click, and while it is waiting the program execution is interrupted. However, there is an additional mouse-related method that we haven't yet used called checkMouse. This method can recognize mouse clicks while the program continues to execute. Like getMouse it will return a point object indicating where the mouse was clicked. But if a click hasn't happened, then it simply returns None.

    Using checkMouse we can modify the while loop so that it will end as soon as the mouse is clicked:

    while w.checkMouse() == None:
       ...
    
    Update your test program to do this.
  3. You should also test your goFaster method. You can do this by adding a counter to the main program. Increment the counter by one each time the while loop executes. Within the loop, check when the counter is evenly divisible by say 100, and then make one of the ball's go faster.
  4. It will be difficult to test the checkContact method now, since we need the game to be running for it to be useful. You can test this method later.

Once you are satisfied that the Ball class is working properly, move on to implementing the game itself.

3. Implementing the Pong class

You will need to define the following methods within the Pong class:

Once you have completed the definition of the Pong class, update the main program so that it calls the play method and enjoy your game!

Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.