Project 7: A Basic Physics Simulator
- Write functions to make a block
In your shapes.py file, write a set of block functions that are similar to the ball functions but use a Rectangle for the visualization.
A block does not need a radius, but it does need values for the width and height that specify its horizontal and vertical size, respectively. Define the visualization rectangle so that the block's position corresponds to the middle of the rectangle.
The functions you need to write to implement blocks are as follows.
- Write a block_construct method to create a block list.
def block_construct( width, height, pos, vel, acc, win, color='blue'):
The block_construct function should create a list to represent a block, initialize the elements of the list, call the block_render function, and then return the list. It should store the width and height of the block in a 2-element list in the Isize position of the block list.
The arguments to block_construct are identical to the ball_construct except that instead of a radius, the first arguments are the width and height of the block.
- Write a block_render method to create the graphics objects for visualizing the block.
def block_render( block_list ):
The argument to block_render is a block list as created by block_construct. The function should undraw the existing graphics objects if they are already drawn, define the Zelle graphics objects that make up the block, set the block's color, put the Zelle graphics objects into a list that gets assigned to the Ishape position in the block list, and finally redraw the block if it was already drawn.
- Write block_getWidth and block_getHeight functions.
def block_getWidth( block_list ): # code here, return the width of the block def block_getHeight( block_list ): # code here, return the height of the block
These two functions should return the width or height value from the 2-element list in the Isize position of the block list.
- Write block_setWidth and block_setHeight functions.
def block_setWidth( block_list, width ): # code here, update the width value in the block_list and call block_render def block_setHeight( block_list, height ): # code here, update the height value in the block_list and call block_render
These two functions should first update the width or height value in the 2-element list in the Isize position of the block list. Then they should call the block_render function.
After completing these functions, test them using this test program.
- Write a block_construct method to create a block list.
- Write a collision function for balls and blocks
Video 5: Ball-block collisions
def ball_block_collision(ball, block):
Write a function that returns True if the ball is intersecting the block. Use the getPosition and ball_getRadius functions to get that information from the ball list. Use the getPosition and block_getWidth and block_getHeight to obtain that information from the block list. Then check if the ball is intersecting the block. Assume the ball is a square for the purposes of collision.
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.
- Write a program that lets the user drop a ball onto a set of blocks.
Write a program that draws at least three blocks at the bottom of the window. Then it should enter a continuous loop that uses the GraphWin's checkMouse method to see if and where the user has clicked the mouse. It should then create a ball at the mouse click location with zero initial velocity and a downward acceleration.
Make something happen when the ball hits the block. If the ball exits the screen (y position < 0) it should undraw the ball.
Use the checkKey GraphiWin method to check for a 'q' key press and exit the loop if the user types a 'q'.
You can use the test program test7-5.py as a template.
Required item 1 Capture a short video of this program running and include it in your handin directory.
- Create some kind of demo for your simulation
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 and include it in your handin directory.
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.
- What is the purpose of creating get and set functions to access information about the ball and block? Why not just access the list fields directly?
- 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.
- The Zelle GraphWin class has functions getMouse() and checkMouse(). What is the difference between them?
- What is the difference between simulation space and visualization space? How does that manifest in our simulatoin?
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.
- Create more elaborate or additional setups/scenes/interactions. Include screen videos in your report/handin folder.
- Make other objects for the simulation. Add other collision functions. A reasonable thing to do is to approximate an object with a box or a circle, even if it is not one of those shapes.
- Add the capabilility for balls to collide with one another. Keep it simple and get it working with just 2 balls first.
- Make the simulation more visually stimulating. Add colors, or change the visualization of a the ball or block objects to something more complex (e.g. a smaller ball inside the larger ball.
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.
- Is your name at the top of each code file?
- Does every function have a comment or docstring specifying what it does?
- Is your handin project directory inside your Private folder on Courses?
Due to the switch to remote learning, the standard reports are not required for CS 152 for the rest of the semester. Please submit your acknowledgements section as a Private comment on Google Classroom.
Any required images or videos should be submitted in your handin directory along with your code.