CSC111 Homework 6 2011
--D. Thiebaut 13:21, 25 October 2011 (EDT)
This homework assignment is due on Nov. 1st, in the evening, at midnight. You can work on Problem #1 in pairs. |
Problem #1
Demo Program
- Create the following graphics program, either on beowulf, or on your own computer. Make sure the graphics.py library is in the same folder as your program.
# hw6a.py
# D. Thiebaut
# This program shows two balls moving in the graphics window.
# One is yellow, one is magenta. The magenta ball is not bound
# by the dimensions of the window, while the yellow one is and
# bounces off the edges as it moves.
# The initial location and speed of the balls are random, but such
# that the balls appear in the window, and are visible. It is possible
# for a ball to have a speed of 0, in which case it does not move.
from graphics import *
import random
def main():
"""Opens a graphics window 600x300 and draws two circles on it"""
W = 600
H = 300
win = GraphWin( "Two Moving Balls", W, H )
# draw a yellow ball, at a random place, with a random velocity
c1 = Circle( Point( random.randrange( 100, W-100 ), # random X
random.randrange( 100, H-100 ) ),# random Y
random.randrange( 10, 20 ) ) # random radius
c1.setFill( "yellow" )
dirX1 = random.randrange( -5, 5 ) # random horizontal displacement
dirY1 = random.randrange( -5, 5 ) # random vertical displacement
c1.draw( win )
# draw a magenta ball, at a random place, with a random velocity
c2 = Circle( Point( random.randrange( 100, W-100 ), # random X
random.randrange( 100, H-100 ) ),# random Y
random.randrange( 10, 20 ) ) # random radius
c2.setFill( "magenta" )
dirX2 = random.randrange( -5, 5 ) # random horizontal displacement
dirY2 = random.randrange( -5, 5 ) # random vertical displacement
c2.draw( win )
for i in range( 1000 ):
c1.move( dirX1, dirY1 )
c2.move( dirX2, dirY2 )
if c1.getCenter().getX() < 0 or c1.getCenter().getX() > W:
dirX1 = -dirX1
if c1.getCenter().getY() < 0 or c1.getCenter().getY() > H:
dirY1 = -dirY1
#--- wait for one more click and close up window---
Text( Point( W//2, H//2 ), "Click me to quit" ).draw( win )
win.getMouse()
win.close()
main()
Assignment
- Modify the program hw6a.py so that the two balls bounce off the walls of the window, not just one.
- Make the balls bounce when their side hits the walls, not their center.
- Make the two balls bounce off when they hit each other (the way pool-table balls would).
- There should be three black boxes in the graphics window, and if any one of the two balls happens to move completely inside any one of the boxes, it should stop there.
- the program should stop and display click me to quit when the simulation has gone through 1000 steps, or as soon as the two balls have been immobilized in boxes.
- there should be a fourth box, this one white, which should act as an obstacle. Any time a ball hits the white box, it should bounce off the walls of the box, in a same way it bounces off the walls.
Requirements
- Use a list to hold the black boxes (defined by their coordinates).
- You are free to select the size of the white and black boxes, but make them big enough to allow balls to hit them regularly, but not too big so as to make the balls "fall" in a black box before having time to hit a few walls or the white box.
- Include your account name(s) or your first name(s) in the title of the graphics window to make it easier to identify printed screen captures of your running program:
win = GraphWin( W, H, "111a-xx's billiard program" )
Realism
- It is not easy nor possible with our current knowledge of Python to make the balls bounce off each other in a realistic way. When a ball hits a wall, the rule is very nice and clear: we change the sign of either dirX or dirY, depending on which is perpendicular to the wall. When the balls hit each other, both dirX and dirY would normally change. The real equation for how dirX and dirY change is too complicated for us. Invent your own laws of physics and implement them in python!
Submission
- Make sure the header of your program contains your names and account numbers, if working in pairs, or just your name and account number if working individually.
- Submit your program as follows:
rsubmit hw6 hw6a.py
Additional Information
- You may find the following function useful for computing the distance between two graphics points:
from math import * # this should be at the beginning of the program def distance( P1, P2 ): """Computes the distance between Point P1 and Point P2. The returned value is a float""" return sqrt( pow( P1.getX() - P2.getX(), 2 ) + pow( P1.getY() - P2.getY(), 2 ) )
- Here is a way it could be used:
P1 = Point( x1, y1 ) P2 = Point( x2, y2 ) ... if distance( P1, P2 ) > 30: # do something if the points are more than 30 pixels away from each other else # do something else if the points are closer than 30 pixels