CSC111 Homework 10 2011
--D. Thiebaut 12:41, 29 November 2011 (EST)
This is an A/E type of homework. You have several problems to solve. You either get an A or an E on each one. What is important here is Python, not documentation (this is an exception to the rule). You have to solve different problems and make your program output the correct answer. Correct answers get A, incorrect ones get E. I will not read the code closely, and will not take point for lack of documentation. The goal here is for you to use classes and objects and make them work.
This assignment is due Dec. 6 evening, at midnight.
Problem 1
- Modify the car class we saw in class (some code available on the class Web page), and make the class contain
- a rectangle for the body,
- a rectangle for the cab part,
- a rectangle (or rectangles) for the window (or windows),
- a Text caption in the middle of the body (s.a. "Taxi" ),
- and a move() method, so that we can make the car move out of the screen with this loop:
for i in range( 500 ): car.move( 2, 0 );
- (we assume that car is an object instantiated from the class Car.)
- Make your program implement this loop so that we can see the car move out of the window.
- Store your program in a file called hw10a.py and submit it this way:
rsubmit hw10 hw10a.py (if you are working on beowulf)
- or use the http://cs.smith.edu/~111a/submit10.htm submit form.
Problem 2
- Play with the program below:
from graphics import *
class Ball:
def __init__(self, c, r ):
self.c = Circle( c, r )
self.dx = 0
self.dy = 0
def draw( self, win ):
self.c.draw( win )
def setFill( self, col ):
self.c.setFill( col )
def setDirection( self, dx, dy ):
self.dx = dx
self.dy = dy
def move( self ):
self.c.move( self.dx, self.dy )
def main():
width = 800
height = 600
win = GraphWin( "111a-xx HW10", width, height )
b1 = Ball( Point( width/4, height/2 ), 10 )
b1.setFill( "blue" )
b1.setDirection( 1, 2 )
b1.draw( win )
b2 = Ball( Point( width/4, height/2 ), 10 )
b2.setFill( "green" )
b2.setDirection( 2, 1 )
b2.draw( win )
while True:
b1.move()
b2.move()
if win.checkMouse() != None:
break
win.close()
main()
- It's not documented, but for this homework documentation is not required.
- Note how each ball remembers its direction of movement in the two member variables self.dx and self.dy.
Assignment
- Add a new method (or more if that makes your code simpler) to the Ball class called changeDirIfHittingWall(). This method changes the sign self.dx or self.dy when the ball hits a vertical or horizontal wall (the boundaries of the window).
- If your method is written correctly, the main function below will see the balls bouncing off all the windows borders in a natural fashion, in a way similar to what we have done many times before.
def main():
width = 800
height = 600
win = GraphWin( "111a-xx HW10", width, height )
b1 = Ball( Point( width/4, height/2 ), 10 )
b1.setFill( "blue" )
b1.setDirection( 1, 2 )
b1.draw( win )
b2 = Ball( Point( width/4, height/2 ), 10 )
b2.setFill( "green" )
b2.setDirection( 2, 1 )
b2.draw( win )
while True:
b1.move()
b2.move()
if win.checkMouse() != None:
break
b1.changeDirIfHittingWall( width, height )
b2.changeDirIfHittingWall( width, height )
win.close()
Additional Information
- the balls must bounce off all four borders of the window
- the balls do not have to bounce off each other
Problem 3
TBA
Problem 4
TBA