CPSC170 Lab 1

Inheritance

Extending Classes

In this section you will create several simple (but less than useful) classes to explore inheritance.

  1. The file dog.py contains a declaration for a Dog class. Save this file to your directory and study it. Notice what instance variables and methods are provided. The file dog_test.py contains a simple driver program that creates a Dog and makes it speak. Study dog_test.py, save it to your directory, and then run it to confirm what it does.

  2. One use of inheritance is to change the behavior of specific methods. Create a file called pomeranian.py that contains a class called Pomeranian that extends Dog. Override the speak the Dog class's speak method by defining a method in the Pomeranian class with the same method signature. The method should print something typical of a pomeranian.

    Modify dog_test.py to add statements to create a Pomeranian and make it speak (do not remove Spike). Run the program. Both dogs should speak something different because both classes have different bodies for the speak method. Notice that you did not define the __init__ method for the Pomeranian class and yet you are still able to call it because it was inherited from the Dog class.

  3. In addition to replacing the behavior of a method in the parent class it is also possible to extend the behavior of a method in the parent class. Create a file called labrador.py that contains a class called Labrador that also extends Dog. Add a second parameter to the __init__ method that specify the color of the lab and use an instance variable to store the lab's color. Again override the speak method with something typical of a lab. Also include something about the lab's color in the speak print statement.

    Modify dog_test.py to add statements to create a Labrador. Be sure to call the __init__ method that specifies the name and the color. Also add a statement to make it speak. Run the program. The dog and pomeranian should speak, but there should be an error with the lab. The name instance variable is not defined because the Dog class's __init__ method was overridden in the Labrador class. To fix this add a call to the Dog class's __init__ method by using built-in method super by add the following statement to the Labrador __init_ method:

        super().__init__(name)
    	

    Run the dog_test program to verify that the program now works.

Timer Events

The file bouncing_ball.py contains code that creates a window that will continually update. This is done using the using the turtle.ontimer method to "schedule" events. Note that ontimer takes two parameters: 1) the name of the function you want to execute after a fixed amount of time, and 2) how long to wait before executing the function. For this section of the lab you will use that idea in order to create program that animates a bouncing ball. We will do this by adding in code to the rest of the bouncing_ball.py file.

  1. Fill out the methods in the ball class, enough that you can draw a circle to represent the ball at the origin.

  2. Modify UpdatingWindow such that your ball is drawn EVERY time a window update occurs

Notice that the Ball class has a method called update_ball that does nothing. The idea is that the Ball class represents a generic ball that can be used in animations. The update_ball method can be written so that the ball's location changes over time. We allso know that any class that inherits Ball will have an update_ball method. We will make a new class called BouncingBall, which will take advantage of this.

  1. Create a new class called BouncingBall, and make it inherit the Ball class.

  2. Override the update_ball method for BouncingBall, so that the ball can move around the screen. It should move by a fixed amount in both the x and y direction. Note, the fixed amount is the velocity of the ball, and should be stored as instance variables of the BouncingBall class. If you run your code at this point, you should see your ball move off the screen.

  3. Finally, modify the program so that when the ball hits the edge of the window, it bounces. In order to do this two things need to occur. First, a collision with the edge of the window needs to be detected. the boundaries of the rectangle encapsulating the ball should to be compared to the boundaries of the screen. The boundaries of the screen can be computed from the size of the screen and the boundaries of the rectangle can be computed using the x and y coordinates, as well as well as the radius.

    You will need one if statement for each edge of the window. Begin by writing an if statement inside the move_ball method for the first window edge that the ball will hit. The body of the if statement should modify the velocity instance variables so that the next time the move_ball method is called, the ball will move away from the wall. Note, that when the ball hits either the left or right edge of the window only the sign of the x velocity needs to change and when the ball hits either the top or bottom edge of the window, only the sign of the y velocity needs to change.

Pong

For this section of the lab you will create a simple one-player version of Pong. If you have never played pong, it is a game with a ball bouncing around the screen. The player controls a paddle that the ball can bounce off of but can only move laterally along the bottom edge of the window. The goal is to prevent the ball from getting past the paddle. Create a new file called pong.py and do the following:

  1. Add the code that animates the ball from the bouncing ball program to the pong program and test it to verify that it works.

  2. Add a paddle class, which will be responsible for drawing and updating the paddle. Make it draw a rectangle to represent the paddle centered along the bottom edge of the screen.

  3. Write a method that can move the paddle to the left or to the right. bind this method to the arrow keys using the turtle.onkeypress method. the key string for these are "Left" and "Right". Run the program. It should have a ball bouncing around the window and a paddle that moves laterally when ever the arrow keys are pressed, but nothing happens if they hit each other.

  4. Finally, remove the code that causes the ball to bounce off of the bottom of the window. Also add code that will cause the ball to bounce off of the paddle if the ball moves beyond the top of the paddle and is not to the left or right of the paddle.

  5. Submission

    Submit a zip file of your code on the course Inquire site using the format FirstLast.zip as the file name.

    If you worked with a partner, use the file format FirstLastFirstLast.zip. Don't forget to also email your partner the lab material.