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).
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 gets past the paddle, 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:
-
__str__
This method takes only the self parameter.
It returns a string that contains the data for the ball object. You may
format this data any way you want, but it should contain the x and y coordinates
of the ball and the x and y speeds (self.dx and self.dy).
-
getRightEdge
This method takes only the self
parameter. It returns the x coordinate of the right side of the circle
that represents the ball (hint: use the graphics Circle getCenter method
to get the center of the ball, then add the radius of the ball to the center's x coordinate).
-
goFaster
This method takes only the self
parameter. It updates the self.dx and self.dy
variables to contain slightly bigger values that are chosen randomly.
One way to accomplish this is to multiply the current values by 1 +
random(). Recall that the random() function returns a
random float between 0.0 and 1.0.
-
moveOnce
This method takes only the self parameter.
It should move the circle representing the ball by the
amount self.dx and self.dy. However, before moving
the circle it must first check whether the circle is about to leave the
graphics window. In this case we want to reverse the direction of the
circle, to make it appear to bounce off the edge of the graphics window.
- If the y-coordinate of the top edge of the circle is smaller than 0
or if the y-coordinate of the bottom edge of the circle is larger than
the height of the window, then we want to reverse the ball's movement
in the y direction. You can accomplish this by doing: self.dy *= -1.
- If the x-coordinate of the left edge of the circle is smaller than 0
or the x-coordinate of the right edge of the circle is larger than the
width of the window, then we want to reverse the ball's movement in
the x direction. You can accomplish this by doing: self.dx *= -1.
2. Testing the Ball class
- Update the main program within the file ball.py
to include a while True loop. Within this loop, invoke
the moveOnce method on each of the balls. Be sure to include a
sleep(0.01) within the loop so that you can actually see
the animation. When you run the updated version of the program you
should see two balls continually bouncing around the graphics
window. To stop this program you'll have to type CTRL-C.
- 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 getMouse 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.
However, 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 win.checkMouse() == None:
# move the balls
# sleep
...
Update your test program to do this.
- 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 balls go faster.
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:
-
gameOver
This method takes only the self
parameter. It returns True when the x-coordinate of the center
of the circle representing the ball (the RightEdge!) is larger than the x-coordinate of
the edge of the paddle. Otherwise it returns False.
-
play
This method takes only the self
parameter. It contains the main loop for the game. The player will
use the mouse to indicate where they want the paddle to move. Here is
a list of things it should accomplish:
- keep playing (loop!) until the game is over
- call checkMouse and move the paddle to the correct spot if the mouse was clicked
- move the ball
- check if the ball hits the paddle
- keep a hit counter to track how many times the ball hits the paddle
- keep a level counter and increase the level for every 5 hits
- adjust the ball speed if the level goes up
- sleep appropriately to make the game animation look reasonable
- once the game is over, tell the user and display total hits and level
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! And feel free to add other features...
Submit
Once you are satisfied with your program, hand it in by typing
handin21 in a terminal window.