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.
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.
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:
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.
Once you are satisfied that the Ball class is working
properly, move on to implementing the game itself.
You will need to define the following methods within the Pong class:
while the game is not over: call checkMouse and save the result if the result contains a point: move the paddle to that point move the ball if the ball is contacting the paddle: increment the number of hits if the number of hits is evenly divisible by 5: increment the level of the game increase the speed of the ball sleep briefly state that the game is over report the number of successful hits
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!
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.