As usual, create a directory to hold today's activities:
$ mkdir ~/cs170/labs/lab3 $ cd ~/cs170/labs/lab3
Hand in your Pseudocode at the beginning of class. I will look at them this evening, and upload comments to inquire by tomorrow. My structure is here, and we will talk about it in class today.
Over the past couple of days, we have had to abuse some Python run-time mechanisms in order to achieve our goals. Specifically, we leveraged the fact that lists are mutable objects in order for us to modify information in the global scope. This is usually what we would consider a "hack."
Instead, we would probably like to accomplish our goals the correct way, and the correct way in this case is through the use of user defined classes. User defined classes allow us to create objects which represent some new data-type we create. And the wonderful aspect of this is that, by default, these are mutable objects.
Last class, Scotty showed you how to abuse mutability of lists to be able to store and update positions of paddles as they appear on the tkinter canvas. Instead, today you are going to implement a class that does this for you, using the bouncing ball example as your guide.
In a file called paddle.py, define a class
called Paddle
. Your paddles should have appropriate
attributes for such an object.
In a file called pong.py, create a tkinter window, create a paddle object, and allow the paddle to be controlled using the Up and Down arrow keys. Make sure you are using your created object!
With the ball, we needed 4 attributes: x and y position, and x and y velocities. For a paddle, we need 4 attributes as well: x and y position, width, and height.
You need to define a constructor for this object, which
defaults the 4 attributes you need for the object. The
syntax for the constructor is def __init__(self):
.
In pong.py, you must import the paddle module.
Once you have done this, you can create a paddle object by
calling the constructor: user_paddle = paddle.Paddle()
.
Once you have created this object, you can use the dot (".") operator to access the attributes of the object.
A real pong game has two paddles. Without defining a new class, create a second paddle object which changes positions when the user moves the mouse around the canvas. Make sure this doesn't break the keyboard controlled paddle!
Now we have objects which represent paddles and the ball for a pong game. The only thing that is missing is a mechanism for making the ball bounce off the paddle. Let's practice using our own datatypes to accomplish our goals!
In your pong.py file, create a function
called check_collision(a_paddle, a_ball)
. This
function takes a paddle object, and a ball object as parameters.
Your function should return True if the ball is colliding
with the paddle, and False otherwise.
Modify your animation loop so that it uses the above function to cause the ball to bounce off of the paddles.
You have all of the information necessary to determine collisions inside of your objects. Begin by drawing a diagram, and determine the conditions when there cannot possibly be a collision. This is much easier to code.
Consider the following diagram. In this case, there cannot
possibly be a collision. This is because the left side of
the ball is to the right of the right side of the paddle.
So you cannot have a collision if paddle_right_side < ball_left_side
You simply need a condition for each side of the paddle.
In your animation loop, if the ball is colliding with a paddle, simply multiply the x velocity by -1.
Modify your program so it keeps track of the number of successive bounces the player is able to achieve. You should reset the players score to 0 when the ball hits the wall behind the users paddle. This score should be displayed on the canvas.
When you have finished, create a tar file of your lab3
directory. To create a tar file, execute the following commands:
cd ~/cs170/labs tar czvf lab3.tgz lab3/
To submit your activity, go to inquire.roanoke.edu. You should
see an available assignment called Lab Assignment 3
.
Make sure you include a header listing the authors of the file.