Lecture 4 - Classes and Objects


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab4
$ cd ~/cs170/labs/lab4

Test Cases

Hand in your test cases at the beginning of class. I will look at them this evening, and upload comments to inquire by tomorrow.


Quiz

I bet that got your attention! Quiz on Wednesday, on Tk and Classes. I guarantee at least one question from the reading for Wednesday as well.


Classes

In a programming language, classes are simply a way to let users define their own data types. While this may seem like a very simple idea, it grants a lot of power to the users. Let's take a peek, and see what that power is.


Lab Assignment 4
Pong (One Last Time)

Up to now, we have been using global variables to represent our pong paddles. While this works, it is not ideal for code reuse. If we restructure the code to use objects, however…

Details

You are going to create two different classes for the Pong game: Ball and Paddle. The Ball class should contain all of the data and functionality required for create a ball that bounces around the screen. The Paddle class should contain all of the data and the functionality required to create a single paddle.

Notice that you are only writing a single Paddle class. Even though you only have one class, you can create multiple objects which are instances of the Paddle class. You should structure your Paddle class so that it can represent a Paddle at an arbitrary x location in the window.

You are also going to write all of your paddle event listeners in the Paddle class. However, you are only going to bind the once necessary for each player. This way, you can easily change which player is in control of which paddle, by simply changing which function gets bound when you run your program.

The Ball class should have an update function. This function should be responsible for moving the ball to its new location, as well as handling collisions with the walls and the paddles. You will have to call this function from your main loop.

Think about what values we have been using from the global namespace to represent these entities. You should be able to remove most of the global variables with this new setup.

Hint

  • Your Paddle constructor should take the starting_x location, the starting_y location, and the canvas the Paddle should be drawn on. These should be stored in class attributes , so they can be used in other methods within the Paddle class.

    You will also want to store the Id that gets returned from the create_rectangle method as an attribute of the class, so you can use it in your event handlers. You probably want to do the same thing with your Ball class as well.

  • Your current event handlers should just need to be copied and pasted into your Paddle class. You will need to update some of the variable identifiers, so they use the class attributes instead of pulling information out of the global namespace.

    When you bind the event handlers in the main portion of your program, you can simply do the following:

    paddle_1 = Paddle(PADDLE_1_STARTING_X, WINDOW_HEIGHT // 2,
                      canvas)
    canvas.bind("<B1-Motion>", paddle_1.handle_mouse_motion)
    paddle_2 = Paddle(PADDLE_2_STARTING_X, WINDOW_HEIGHT // 2,
                      canvas)
    canvas.bind("<Key>", paddle_2.handle_keys)
    

  • Your Ball constructor should take the same information as the paddle. You will also want to initialize attributes for the X and Y components of the ball's velocity.

  • Your Ball class should also have a method called detect_paddle_collision(self, paddle), which takes a reference to a paddle object. This function should return True if the ball's current location collides with the specified paddle.

  • The Ball class' update function should take a list of paddles which it needs to interact with. This is very important for use with detect_paddle_collision.

 

Challenge

At this point, you have a mostly functioning Pong game. However, there is one key feature not implemented: computing a players score. This will ultimately be used to determine whether a player won or not, so it's probably time to get cracking.

You can store the score as an attribute of the Paddle class. Then you just need to determine when and where to increment a players score.

 

Challenge

Now that you've figured out how to tally a player's score, you can now determine which player wins your game. Your game should stop updating the ball, and display a message indicating which player actually won the game.


Submission

When you have finished, create a tar file of your lab4 directory. To create a tar file, execute the following commands:

cd ~/cs170/labs
tar czvf lab4.tgz lab4/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 4. Make sure you include a header listing the authors of the file.


In-class Notes


Last modified: Mon Jan 20 13:57:55 EST 2014