CS 152: Project 8

Title image Project 8
Fall 2019

Project 8: Object-Oriented Physics Simulation

The focus on this project is to provide you with more experience writing classes. Now that you have a working ball class, it's time to for the balls to collide with stuff.


  1. Write a Block class

    Write a Block class that is similar to the Circle class but uses a Rectangle to represent it in the window. The __init__ method should have one required argument, which is the GraphWin object in which it is to be drawn.

    A Block does not need a radius field, but it does need fields for dx and dy 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 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 tuple.
    • def setPosition(self, px, py) - update the Block's position. The function must also update the graphics object's position.
    • def getVelocity(self) - return the Block's velocity as a 2-element tuple.
    • def setVelocity(self, vx, vy) - update the Block's velocity.
    • def getAcceleration(self) - return the Block's acceleration as a 2-element tuple.
    • def setAcceleration(self, ax, ay) - update the Block's acceleration.
    • def getWidth(self) - return the Block's width (dx)
    • def setWidth(self) - (**optional**) changes the Block's width. This method will need to undraw the shapes in the vis list, create a new vis list, then draw the shapes.
    • def getHeight(self) - return the Block's height (dy)
    • def setHeight(self) - (**optional**) changes the Block's height. This method will need to undraw the shapes in the vis list, create a new vis list, then draw the shapes.
    • def update(self, dt) - implement the equations of motion for the block.
  2. Write a collision function

    def collision(self, ball):

    Write a function 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.

    Explaining your collision function should be a key part of your report.

    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.

  3. Add blocks to the fall program

    Add blocks to at least the bottom of the screen in your fall program from the lab. A block should disappear if it is hit by the ball.

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

  4. Create some kind of interactive demo

    Give the user some kind of control over how to launch a ball into the scene. Have the ball collide with blocks and do something. The blocks could change color, disappear, move, or do anything else you can code. Likewise, the ball could disappear 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 either in your report or in your handin directory.

Follow-up Questions

  1. What is the purpose of having get and set functions for a class? Why not just access the object fields directly?
  2. Given a list of Zelle Graphics objects, write a for loop that would move each object in the list by (5, 10) in x and y.
  3. The Zelle GraphWin class has functions getMouse() and checkMouse(). What is the difference between them?
  4. What is the difference between simulation space and visualization space?


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. Be sure to describe any extensions you complete in your report.

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 project6, 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?

Write your project report

For CS 152 please use Google Docs to write your report. Create a new doc for each project. Start the doc with a title and your name. Attach the doc to your project on Google classroom. Make sure you click submit when you are done. The graders cannot provide feedback unless you click submit.

Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it.

Your project report should contain the following elements.