CS 152: Project 8

Title image Project 8
Spring 2020

Project 8: Object-Oriented Physics Simulation

The focus on this project is to provide you with experience writing classes. Now that you have a working ball class, it's time to make a corresponding block class.


  1. Write a Block class

    Write a Block class that is similar to the Ball class but uses (at least) a Rectangle to represent it in the window. The __init__ method should have three required arguments: the GraphWin window, the width of the block, and the height of the block. You can also give the __init__ method optional arguments for position, velocity, acceleration, and color.

    A Block does not need a radius field, but it does need fields for width and height that specify its horizontal and vertical size, respectively. Define the visualization rectangle so that the Block's position is in the middle of the rectangle.

    In addition to its __init__, the Block class needs to have the following methods.

    • def render(self) - create the graphics objects required for the visualization.
    • def draw(self) - draw the visualization objects into the window.
    • def undraw(self) - undraw the visualization objects.
    • def getPosition(self) - return the position as a 2-element list.
    • def setPosition(self, pos) - update the Block's position given a 2-element list as an argument. The function must also update the graphics objects' positions.
    • def getVelocity(self) - return the Block's velocity as a 2-element list.
    • def setVelocity(self, vel) - update the Block's velocity given a 2-element list as an argument.
    • def getAcceleration(self) - return the Block's acceleration as a 2-element tuple.
    • def setAcceleration(self, acc) - update the Block's acceleration given a 2-element list as an argument.
    • def getWidth(self) - return the Block's width (dx)
    • def setWidth(self, w) - update the Block's width and re-render it.
    • def getHeight(self) - return the Block's height (dy)
    • def setHeight(self, h) - update the Block's height and re-render it.
    • def getColor(self) - return the Block's current color.
    • def setColor(self, c) - update the Block's color and re-color it.
    • def update(self, dt) - implement the equations of motion for the block given time step dt.

    test8-5.py tests block class methods.

  2. Add at least one additional shape to the visual representation of the Ball and the Block classes

    In the render method of both the Ball and Block classes, create at least one more graphics object and make it part of the visualization. For example, put a square inside the Ball, or give the Block stripes.

  3. Write a Block class collision function for the block and a ball

    def collision_ball(self, ball):

    Write a method for the Block class that returns True if the ball is intersecting the block. Use the Ball's getPosition and getRadius functions to get that information. Then check if the ball is intersecting the block. You can assume the ball is a square for the purposes of collision, if that makes it easier.

    If the ball is not colliding with the Block, the function should return False.

    You can use this test function to test the intersection code. When you run it, hit the space bar to move the ball to a randomized location.

    test8-6.py tests ball-block collisions.

    test8-7.py tests ball-block collisions.

  4. Write a Ball class collision function for two balls

    def collision_ball(self, ball):

    Write a function for the Ball class that returns True if the ball is intersecting the other ball. Use the Ball's getPosition and getRadius functions to get the necessary information. The function should return False if the balls are not intersecting.

  5. Add blocks to the fall program

    Add blocks and balls to at least the bottom of the screen in your fall program from the lab. The stationary blocks or balls should do something (e.g. disappear) if hit by the moving ball.

    Required item 1: Capture a short video of this program running. Submit it in your handin directory.

  6. Create some kind of interactive demo

    Give the user some kind of control over the scene, such as being able to launch a ball with a mouse click or control the ball's velocity depending on which key was pressed. Use a mixture of blocks and balls in your scene. Have the ball collide with blocks and do something. The blocks or balls could change color, disappear, move, or do anything else you can code. Likewise, the moving ball could disappear, reposition, or bounce, enabling it to hit other blocks.

    You can use the win.checkKey() function to see if the user has typed a key. If the user has typed the left, right, up, or down arrow keys, then checkKey will return 'Left', 'Right', 'Up', or 'Down' as the return value. The space bar returns the string 'space'.

    Required item 2 Capture a short video of this program running. Submit it in your handin directory.

Follow-up Questions

These are questions you should be able to answer and may be similar to questions on a quiz or final exam. If you have any questions about them, send your answer to the lab instructor along with any questions in return for some feedback.

  1. Why do we call the __init__ method for a class a constructor?
  2. When is the __init__ method called?
  3. What is the value or meaning of the variable self in a class method?
  4. What is the purpose of having get and set functions for a class? Why not just access the object fields directly?


Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own.

Due to the switch to remote learning, extensions are no longer a graded part of CS 152 for the rest of the semester. They are things you can do on your own to learn more and become better at CS, especially if you are interested in taking more CS courses.

Submit your code

Turn in your code (all files ending with .py) by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS152, and within that, a directory with your user name. Within this directory is a directory named Private. Files that you put into that private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, create a new directory, such as project8, and then copy your code into the project directory for that week. Please submit only code that you want to be graded.

When submitting your code, double check the following.

  1. Is your name at the top of each code file?
  2. Does every function have a comment or docstring specifying what it does?
  3. Is your handin project directory inside your Private folder on Courses?