< Back

Lecture 3- Object Oriented Programming


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

$ mkdir ~/cs170/labs/lab3 
$ cd ~/cs170/labs/lab3

BouncingBall.py

Pseudocode

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.


Classes

We have been working with objects since essentially day one of CPSC 170. Turtles were objects, as well as Strings, Lists, Dictionaries, etc. These are examples of objects which are provided by the language. One of the most powerful features of the Python language, however, is their built in mechanism for allowing users to create their own objects, through the definition of classes. Today, we will explore this power.


Lab Activity 1
Pong Paddles

Pong was one of the first console games available to the general public. Back in the 1970's, it took an entire computer just to render and control the game of Pong. Today, we can easily create a game of Pong as a program in our computers.

The nice thing about a game of pong is that it can easily be broken down into obvious objects for use within the program. During lecture today, I showed you how to create an object representing the Pong ball. You will practice with the notion of objects by writing a class to represent the paddles!

Details

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 graphics 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!

Hint

  • 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.

 

Challenge

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!


Lab Activity 2
Collisions

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!

Details

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.

Hint

  • 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.

 

Challenge

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 window.