Difference between revisions of "CSC111 Programs for Week 9 2015"
(→Cats) |
(→Cats) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 58: | Line 58: | ||
<br /> | <br /> | ||
=Cats= | =Cats= | ||
+ | <br /> | ||
+ | <br /> | ||
+ | ==Cat CSV File== | ||
<br /> | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # create csv file | ||
+ | # D. Thiebaut | ||
+ | |||
+ | text = """Minou, 3, vaccinated, stray | ||
+ | Max, 1, not vaccinated, Burmese | ||
+ | Gizmo, 2, vaccinated, Bengal | ||
+ | Garfield, 4, not vaccinated, Orange Tabby | ||
+ | Silky, 3, vaccinated, Siemese | ||
+ | Winston, 1, not vaccinated, stray | ||
+ | """ | ||
+ | |||
+ | open( "cats.csv", "w" ).write( text ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Cat Program== | ||
::<source lang="python"> | ::<source lang="python"> | ||
# cats1.py | # cats1.py | ||
Line 159: | Line 178: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
+ | =Graphics= | ||
+ | <br /> | ||
+ | The programs below are not fully documented. Just examples of putting a program together in class. | ||
+ | <br /> | ||
+ | == skeletonGraphics.py == | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # skeleton.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | center = Point( WIDTH//4, HEIGHT//4 ) | ||
+ | c1 = Circle( center, 30 ) | ||
+ | c1.setFill( "red" ) | ||
+ | c1.draw( win ) | ||
+ | |||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | == wheel1.py == | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # wheel1.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | class Wheel: | ||
+ | """a Wheel is 2 concentric circles, the larger one black, | ||
+ | the smaller one grey.""" | ||
+ | |||
+ | def __init__(self, centr, rad1, rad2 ): | ||
+ | # make circ1 the smaller of the 2 circles | ||
+ | self.circ1 = Circle( centr, min( rad1, rad2 ) ) | ||
+ | self.circ2 = Circle( centr, max( rad1, rad2 ) ) | ||
+ | self.circ1.setFill( "grey" ) | ||
+ | self.circ2.setFill( "black" ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.circ2.draw( win ) | ||
+ | self.circ1.draw( win ) | ||
+ | |||
+ | class Car: | ||
+ | """a class containing a rectangle and 2 wheels, forming | ||
+ | a simple car""" | ||
+ | |||
+ | def __init__( self, refPoint ): | ||
+ | # create rectangle | ||
+ | P1 = refPoint | ||
+ | xP1 = P1.getX() | ||
+ | yP1 = P1.getY() | ||
+ | xP2 = xP1 + 180 | ||
+ | yP2 = yP1 + 60 | ||
+ | P2 = Point( xP2, yP2 ) | ||
+ | self.body = Rectangle( P1, P2 ) | ||
+ | self.body.setFill( "yellow" ) | ||
+ | |||
+ | # create the Wheels | ||
+ | self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) | ||
+ | self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.body.draw( win ) | ||
+ | self.w1.draw( win ) | ||
+ | self.w2.draw( win ) | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | #w1 = Wheel( Point( WIDTH//4, HEIGHT//4 ), 30, 50 ) | ||
+ | #w1.draw( win ) | ||
+ | car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) | ||
+ | car1.draw( win ) | ||
+ | |||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | == wheel2.py == | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # wheel1.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | class Wheel: | ||
+ | """a Wheel is 2 concentric circles, the larger one black, | ||
+ | the smaller one grey.""" | ||
+ | |||
+ | def __init__(self, centr, rad1, rad2 ): | ||
+ | # make circ1 the smaller of the 2 circles | ||
+ | self.circ1 = Circle( centr, min( rad1, rad2 ) ) | ||
+ | self.circ2 = Circle( centr, max( rad1, rad2 ) ) | ||
+ | self.circ1.setFill( "grey" ) | ||
+ | self.circ2.setFill( "black" ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.circ2.draw( win ) | ||
+ | self.circ1.draw( win ) | ||
+ | |||
+ | class Car: | ||
+ | """a class containing a rectangle and 2 wheels, forming | ||
+ | a simple car""" | ||
+ | |||
+ | def __init__( self, refPoint ): | ||
+ | # create rectangle | ||
+ | P1 = refPoint | ||
+ | xP1 = P1.getX() | ||
+ | yP1 = P1.getY() | ||
+ | xP2 = xP1 + 180 | ||
+ | yP2 = yP1 + 60 | ||
+ | P2 = Point( xP2, yP2 ) | ||
+ | self.body = Rectangle( P1, P2 ) | ||
+ | self.body.setFill( "yellow" ) | ||
+ | |||
+ | # create the Wheels | ||
+ | self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) | ||
+ | self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.body.draw( win ) | ||
+ | self.w1.draw( win ) | ||
+ | self.w2.draw( win ) | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | #w1 = Wheel( Point( WIDTH//4, HEIGHT//4 ), 30, 50 ) | ||
+ | #w1.draw( win ) | ||
+ | car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) | ||
+ | car1.draw( win ) | ||
+ | |||
+ | # animation loop | ||
+ | while True: | ||
+ | car1.setSpeed( 5, 0 ) | ||
+ | |||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | == car1.py == | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # wheel1.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | class Wheel: | ||
+ | """a Wheel is 2 concentric circles, the larger one black, | ||
+ | the smaller one grey.""" | ||
+ | |||
+ | def __init__(self, centr, rad1, rad2 ): | ||
+ | # make circ1 the smaller of the 2 circles | ||
+ | self.circ1 = Circle( centr, min( rad1, rad2 ) ) | ||
+ | self.circ2 = Circle( centr, max( rad1, rad2 ) ) | ||
+ | self.circ1.setFill( "grey" ) | ||
+ | self.circ2.setFill( "black" ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.circ2.draw( win ) | ||
+ | self.circ1.draw( win ) | ||
+ | |||
+ | class Car: | ||
+ | """a class containing a rectangle and 2 wheels, forming | ||
+ | a simple car""" | ||
+ | |||
+ | def __init__( self, refPoint ): | ||
+ | # create rectangle | ||
+ | P1 = refPoint | ||
+ | xP1 = P1.getX() | ||
+ | yP1 = P1.getY() | ||
+ | xP2 = xP1 + 180 | ||
+ | yP2 = yP1 + 60 | ||
+ | P2 = Point( xP2, yP2 ) | ||
+ | self.body = Rectangle( P1, P2 ) | ||
+ | self.body.setFill( "yellow" ) | ||
+ | |||
+ | # create the Wheels | ||
+ | self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) | ||
+ | self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.body.draw( win ) | ||
+ | self.w1.draw( win ) | ||
+ | self.w2.draw( win ) | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | #w1 = Wheel( Point( WIDTH//4, HEIGHT//4 ), 30, 50 ) | ||
+ | #w1.draw( win ) | ||
+ | car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) | ||
+ | car1.draw( win ) | ||
+ | |||
+ | |||
+ | |||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | == car2.py == | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # wheel1.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | class Wheel: | ||
+ | """a Wheel is 2 concentric circles, the larger one black, | ||
+ | the smaller one grey.""" | ||
+ | |||
+ | def __init__(self, centr, rad1, rad2 ): | ||
+ | # make circ1 the smaller of the 2 circles | ||
+ | self.circ1 = Circle( centr, min( rad1, rad2 ) ) | ||
+ | self.circ2 = Circle( centr, max( rad1, rad2 ) ) | ||
+ | self.circ1.setFill( "grey" ) | ||
+ | self.circ2.setFill( "black" ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.circ2.draw( win ) | ||
+ | self.circ1.draw( win ) | ||
+ | |||
+ | def move( self, dx, dy ): | ||
+ | self.circ1.move( dx, dy ) | ||
+ | self.circ2.move( dx, dy ) | ||
+ | |||
+ | class Car: | ||
+ | """a class containing a rectangle and 2 wheels, forming | ||
+ | a simple car""" | ||
+ | |||
+ | def __init__( self, refPoint ): | ||
+ | # create rectangle | ||
+ | P1 = refPoint | ||
+ | xP1 = P1.getX() | ||
+ | yP1 = P1.getY() | ||
+ | xP2 = xP1 + 180 | ||
+ | yP2 = yP1 + 60 | ||
+ | P2 = Point( xP2, yP2 ) | ||
+ | self.body = Rectangle( P1, P2 ) | ||
+ | self.body.setFill( "yellow" ) | ||
+ | |||
+ | # create the Wheels | ||
+ | self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) | ||
+ | self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) | ||
+ | |||
+ | # set the initial speed | ||
+ | self.dx = 1 | ||
+ | self.dy = 0 | ||
+ | |||
+ | def setSpeed( self, deltaX, deltaY ): | ||
+ | self.dx = deltaX | ||
+ | self.dy = deltaY | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.w1.draw( win ) | ||
+ | self.body.draw( win ) | ||
+ | self.w2.draw( win ) | ||
+ | |||
+ | def autoMove( self ): | ||
+ | self.w1.move( self.dx, self.dy ) | ||
+ | self.w2.move( self.dx, self.dy ) | ||
+ | self.body.move( self.dx, self.dy ) | ||
+ | |||
+ | def reverseDirection( self ): | ||
+ | self.dx = -self.dx | ||
+ | |||
+ | def getX( self ): | ||
+ | return self.body.getP1().getX() | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | |||
+ | car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) | ||
+ | car1.draw( win ) | ||
+ | car1.setSpeed( 5, 0 ) | ||
+ | |||
+ | while True: | ||
+ | car1.autoMove() | ||
+ | |||
+ | if car1.getX() > WIDTH or car1.getX() < 0: | ||
+ | car1.reverseDirection() | ||
+ | |||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | == car3.py == | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # wheel1.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | import random | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | class Wheel: | ||
+ | """a Wheel is 2 concentric circles, the larger one black, | ||
+ | the smaller one grey.""" | ||
+ | |||
+ | def __init__(self, centr, rad1, rad2 ): | ||
+ | # make circ1 the smaller of the 2 circles | ||
+ | self.circ1 = Circle( centr, min( rad1, rad2 ) ) | ||
+ | self.circ2 = Circle( centr, max( rad1, rad2 ) ) | ||
+ | self.circ1.setFill( "grey" ) | ||
+ | self.circ2.setFill( "black" ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.circ2.draw( win ) | ||
+ | self.circ1.draw( win ) | ||
+ | |||
+ | def move( self, dx, dy ): | ||
+ | self.circ1.move( dx, dy ) | ||
+ | self.circ2.move( dx, dy ) | ||
+ | |||
+ | class Car: | ||
+ | """a class containing a rectangle and 2 wheels, forming | ||
+ | a simple car""" | ||
+ | |||
+ | def __init__( self, refPoint ): | ||
+ | # create rectangle | ||
+ | P1 = refPoint | ||
+ | xP1 = P1.getX() | ||
+ | yP1 = P1.getY() | ||
+ | xP2 = xP1 + 180 | ||
+ | yP2 = yP1 + 60 | ||
+ | P2 = Point( xP2, yP2 ) | ||
+ | self.body = Rectangle( P1, P2 ) | ||
+ | self.body.setFill( "yellow" ) | ||
+ | |||
+ | # create the Wheels | ||
+ | self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) | ||
+ | self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) | ||
+ | |||
+ | # set the initial speed | ||
+ | self.dx = 1 | ||
+ | self.dy = 0 | ||
+ | |||
+ | def setSpeed( self, deltaX, deltaY ): | ||
+ | self.dx = deltaX | ||
+ | self.dy = deltaY | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.w1.draw( win ) | ||
+ | self.body.draw( win ) | ||
+ | self.w2.draw( win ) | ||
+ | |||
+ | def autoMove( self ): | ||
+ | self.w1.move( self.dx, self.dy ) | ||
+ | self.w2.move( self.dx, self.dy ) | ||
+ | self.body.move( self.dx, self.dy ) | ||
+ | |||
+ | def reverseDirection( self ): | ||
+ | self.dx = -self.dx | ||
+ | |||
+ | def getX( self ): | ||
+ | return self.body.getP1().getX() | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | cars = [] | ||
+ | for x in range( 10, WIDTH, 80 ): | ||
+ | car1 = Car( Point( x, random.choice( [10,50,100, 125, 175, 300 ]) ) ) | ||
+ | car1.draw( win ) | ||
+ | car1.setSpeed( random.choice( [-5, 3, 2, 5, 4] ), 0 ) | ||
+ | cars.append( car1 ) | ||
+ | |||
+ | while True: | ||
+ | for car1 in cars: | ||
+ | car1.autoMove() | ||
+ | |||
+ | if car1.getX() > WIDTH or car1.getX() < 0: | ||
+ | car1.reverseDirection() | ||
+ | |||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | == car3randomColors.py == | ||
<br /> | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # wheel1.py | ||
+ | # D. Thiebaut | ||
+ | # a skeleton program to get started with the graphics | ||
+ | # | ||
+ | from graphics import * | ||
+ | import random | ||
+ | |||
+ | # define geometry (constants) | ||
+ | WIDTH = 700 | ||
+ | HEIGHT = 600 | ||
+ | |||
+ | |||
+ | class Wheel: | ||
+ | """a Wheel is 2 concentric circles, the larger one black, | ||
+ | the smaller one grey.""" | ||
+ | |||
+ | def __init__(self, centr, rad1, rad2 ): | ||
+ | # make circ1 the smaller of the 2 circles | ||
+ | self.circ1 = Circle( centr, min( rad1, rad2 ) ) | ||
+ | self.circ2 = Circle( centr, max( rad1, rad2 ) ) | ||
+ | self.circ1.setFill( "grey" ) | ||
+ | self.circ2.setFill( "black" ) | ||
+ | |||
+ | def draw( self, win ): | ||
+ | self.circ2.draw( win ) | ||
+ | self.circ1.draw( win ) | ||
+ | |||
+ | def move( self, dx, dy ): | ||
+ | self.circ1.move( dx, dy ) | ||
+ | self.circ2.move( dx, dy ) | ||
+ | |||
+ | class Car: | ||
+ | """a class containing a rectangle and 2 wheels, forming | ||
+ | a simple car""" | ||
+ | |||
+ | def __init__( self, refPoint ): | ||
+ | # create rectangle | ||
+ | P1 = refPoint | ||
+ | xP1 = P1.getX() | ||
+ | yP1 = P1.getY() | ||
+ | xP2 = xP1 + 180 | ||
+ | yP2 = yP1 + 60 | ||
+ | P2 = Point( xP2, yP2 ) | ||
+ | self.body = Rectangle( P1, P2 ) | ||
+ | self.body.setFill( random.choice( ["yellow", "black", "beige", | ||
+ | "magenta"] ) ) | ||
+ | |||
+ | # create the Wheels | ||
+ | self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) | ||
+ | self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) | ||
+ | |||
+ | # set the initial speed | ||
+ | self.dx = 1 | ||
+ | self.dy = 0 | ||
+ | |||
+ | def setSpeed( self, deltaX, deltaY ): | ||
+ | """change the speed of the car""" | ||
+ | self.dx = deltaX | ||
+ | self.dy = deltaY | ||
+ | |||
+ | def draw( self, win ): | ||
+ | """draw the car on the window""" | ||
+ | self.w1.draw( win ) | ||
+ | self.body.draw( win ) | ||
+ | self.w2.draw( win ) | ||
+ | |||
+ | def autoMove( self ): | ||
+ | """move the car in the current direction (dx, dy)""" | ||
+ | self.w1.move( self.dx, self.dy ) | ||
+ | self.w2.move( self.dx, self.dy ) | ||
+ | self.body.move( self.dx, self.dy ) | ||
+ | |||
+ | def reverseDirection( self ): | ||
+ | """reverses the direction of movement. If going right (dx>0), | ||
+ | change to going left (dx<0). If going left, change to going right.""" | ||
+ | self.dx = -self.dx | ||
+ | |||
+ | def getX( self ): | ||
+ | """return the x-coordinate of the top-left point of the rectangle""" | ||
+ | return self.body.getP1().getX() | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | def main(): | ||
+ | global W, H | ||
+ | win = GraphWin( "wheel demo", WIDTH, HEIGHT ) | ||
+ | |||
+ | # wait for user to click the window... | ||
+ | waitForClick( win, "click to start" ) | ||
+ | |||
+ | # create a list of several cars, with random positions and random | ||
+ | # speeds | ||
+ | cars = [] | ||
+ | for x in range( 10, WIDTH, 120 ): | ||
+ | # create a new car at a random height in the window | ||
+ | car1 = Car( Point( x, random.choice( [10,50,100, 125, 175, 300 ]) ) ) | ||
+ | |||
+ | # draw the car | ||
+ | car1.draw( win ) | ||
+ | |||
+ | # set its speed randomly | ||
+ | car1.setSpeed( random.choice( [-5, 30, 2, 15, -4] ), 0 ) | ||
+ | |||
+ | # add the car to the list | ||
+ | cars.append( car1 ) | ||
+ | |||
+ | |||
+ | # animation loop: keep on going as long as the user does not click | ||
+ | # on the window. | ||
+ | while win.checkMouse()==None: | ||
+ | |||
+ | # move each car of the list | ||
+ | for car1 in cars: | ||
+ | # move it | ||
+ | car1.autoMove() | ||
+ | |||
+ | # if it's hitting a left or right edge of the window | ||
+ | # change the direction of movement of the car. | ||
+ | if car1.getX() > WIDTH or car1.getX() < 0: | ||
+ | car1.reverseDirection() | ||
+ | |||
+ | # wait for the user to click one last time... | ||
+ | waitForClick( win, "click to end" ) | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 12:47, 2 April 2015
--D. Thiebaut (talk) 09:03, 30 March 2015 (EDT)
Contents
Die
# dice.py # D. Thiebaut # Program for Week 9 # an example program that defines a Die class, and # demonstrates how to create 2 objects from this class. import random class Die: """a class implementing a Die, with a given number of sides, and a value (the number of the top face)""" def __init__( self, n ): """initializes the die with n faces, and a value 1, by default""" self.noSides = n self.value = 1 def roll( self ): """roll the die and pick a new random value for it""" self.value = random.randrange( 1, self.noSides+1 ) def getValue( self ): """return the current value, i.e. the number on the top side""" return self.value def main(): # Create 2 dice, one with 6 sides, one with 8 d1 = Die( 6 ) d2 = Die( 8 ) while True: # Roll both dice d1.roll( ) d2.roll( ) # display their value print( "Die 1: ", d1.getValue() ) print( "Die 2: ", d2.getValue() ) ans = input( "Press enter to continue. Any other key to quit: " ) if len( ans.strip() )!= 0: break if __name__=="__main__": main()
Cats
Cat CSV File
# create csv file # D. Thiebaut text = """Minou, 3, vaccinated, stray Max, 1, not vaccinated, Burmese Gizmo, 2, vaccinated, Bengal Garfield, 4, not vaccinated, Orange Tabby Silky, 3, vaccinated, Siemese Winston, 1, not vaccinated, stray """ open( "cats.csv", "w" ).write( text )
Cat Program
# cats1.py # D. Thiebaut # program developed in class on 4/1/15 # class Cat: """Cat class. contains information about a cat: name, whether vaccinated, breed, and age""" def __init__( self, na, vacc, bree, ag ): self.name = na self.vaccinated = vacc self.breed = bree self.age = ag def __str__( self ): """default string representation for a cat. Will be use by print() if we ask it to print a cat object.""" if self.vaccinated: vac = "vaccinated" else: vac = "not vaccinated" # create the string representation of the object s = "{0:10} ==> ({1:1}), {2:1}, {3:1}".format( self.name, self.breed, self.age, vac ) # return the string representation return s def getName( self ): """returns the name of the cat""" return self.name def isVaccinated( self ): """returns True if the cat is vaccinated, False otherwise.""" #if self.vaccinated: # return True #else: # return False return self.vaccinated # ------------------------------------------------------------- # MAIN # ------------------------------------------------------------- def main(): # create an empty list of cats catList = [] # Create 3 cat objects and add them to the list # Minou, 3, vaccinated, stray #catList.append( Cat( "Minou", True, "stray", 3 ) ) #catList.append( Cat( "Ralph", False, "Burmese", 1 ) ) #catList.append( Cat( "Garfield", True, "Orange Tabby", 10 ) ) # open csv file containing cats. Example of line in file shown below. # Winston, 1, not vaccinated, stray file = open( "cats.csv", "r" ) lines = file.readlines() file.close() # the contents of the file is now in a list of strings. The list is called lines. # for each line in the list of lines... for line in lines: #print( line ) # split the line into 4 fields fields = line.split( ',' ) # if the line doesn't contain 4 fields, it must be invalid. Skip it! if len( fields ) != 4: continue # assign each one of the 4 items in the list fields to 4 different variables name, age, vacc, breed = fields # transform vacc from "vaccinated" or "not vaccinated" to True or False if vacc.lower().strip() == "vaccinated": vacc = True else: vacc = False # add a new cat with this information to the list of cats cat = Cat( name.strip(), vacc, breed.strip(), int(age.strip()) ) catList.append( cat ) # list all the cats in the list for cat in catList: print( cat ) main()
Graphics
The programs below are not fully documented. Just examples of putting a program together in class.
skeletonGraphics.py
# skeleton.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * # define geometry (constants) WIDTH = 700 HEIGHT = 600 #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) waitForClick( win, "click to start" ) center = Point( WIDTH//4, HEIGHT//4 ) c1 = Circle( center, 30 ) c1.setFill( "red" ) c1.draw( win ) waitForClick( win, "click to end" ) win.close() main()
wheel1.py
# wheel1.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * # define geometry (constants) WIDTH = 700 HEIGHT = 600 class Wheel: """a Wheel is 2 concentric circles, the larger one black, the smaller one grey.""" def __init__(self, centr, rad1, rad2 ): # make circ1 the smaller of the 2 circles self.circ1 = Circle( centr, min( rad1, rad2 ) ) self.circ2 = Circle( centr, max( rad1, rad2 ) ) self.circ1.setFill( "grey" ) self.circ2.setFill( "black" ) def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) class Car: """a class containing a rectangle and 2 wheels, forming a simple car""" def __init__( self, refPoint ): # create rectangle P1 = refPoint xP1 = P1.getX() yP1 = P1.getY() xP2 = xP1 + 180 yP2 = yP1 + 60 P2 = Point( xP2, yP2 ) self.body = Rectangle( P1, P2 ) self.body.setFill( "yellow" ) # create the Wheels self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) def draw( self, win ): self.body.draw( win ) self.w1.draw( win ) self.w2.draw( win ) #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) waitForClick( win, "click to start" ) #w1 = Wheel( Point( WIDTH//4, HEIGHT//4 ), 30, 50 ) #w1.draw( win ) car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) car1.draw( win ) waitForClick( win, "click to end" ) win.close() main()
wheel2.py
# wheel1.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * # define geometry (constants) WIDTH = 700 HEIGHT = 600 class Wheel: """a Wheel is 2 concentric circles, the larger one black, the smaller one grey.""" def __init__(self, centr, rad1, rad2 ): # make circ1 the smaller of the 2 circles self.circ1 = Circle( centr, min( rad1, rad2 ) ) self.circ2 = Circle( centr, max( rad1, rad2 ) ) self.circ1.setFill( "grey" ) self.circ2.setFill( "black" ) def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) class Car: """a class containing a rectangle and 2 wheels, forming a simple car""" def __init__( self, refPoint ): # create rectangle P1 = refPoint xP1 = P1.getX() yP1 = P1.getY() xP2 = xP1 + 180 yP2 = yP1 + 60 P2 = Point( xP2, yP2 ) self.body = Rectangle( P1, P2 ) self.body.setFill( "yellow" ) # create the Wheels self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) def draw( self, win ): self.body.draw( win ) self.w1.draw( win ) self.w2.draw( win ) #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) waitForClick( win, "click to start" ) #w1 = Wheel( Point( WIDTH//4, HEIGHT//4 ), 30, 50 ) #w1.draw( win ) car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) car1.draw( win ) # animation loop while True: car1.setSpeed( 5, 0 ) waitForClick( win, "click to end" ) win.close() main()
car1.py
# wheel1.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * # define geometry (constants) WIDTH = 700 HEIGHT = 600 class Wheel: """a Wheel is 2 concentric circles, the larger one black, the smaller one grey.""" def __init__(self, centr, rad1, rad2 ): # make circ1 the smaller of the 2 circles self.circ1 = Circle( centr, min( rad1, rad2 ) ) self.circ2 = Circle( centr, max( rad1, rad2 ) ) self.circ1.setFill( "grey" ) self.circ2.setFill( "black" ) def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) class Car: """a class containing a rectangle and 2 wheels, forming a simple car""" def __init__( self, refPoint ): # create rectangle P1 = refPoint xP1 = P1.getX() yP1 = P1.getY() xP2 = xP1 + 180 yP2 = yP1 + 60 P2 = Point( xP2, yP2 ) self.body = Rectangle( P1, P2 ) self.body.setFill( "yellow" ) # create the Wheels self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) def draw( self, win ): self.body.draw( win ) self.w1.draw( win ) self.w2.draw( win ) #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) waitForClick( win, "click to start" ) #w1 = Wheel( Point( WIDTH//4, HEIGHT//4 ), 30, 50 ) #w1.draw( win ) car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) car1.draw( win ) waitForClick( win, "click to end" ) win.close() main()
car2.py
# wheel1.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * # define geometry (constants) WIDTH = 700 HEIGHT = 600 class Wheel: """a Wheel is 2 concentric circles, the larger one black, the smaller one grey.""" def __init__(self, centr, rad1, rad2 ): # make circ1 the smaller of the 2 circles self.circ1 = Circle( centr, min( rad1, rad2 ) ) self.circ2 = Circle( centr, max( rad1, rad2 ) ) self.circ1.setFill( "grey" ) self.circ2.setFill( "black" ) def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) def move( self, dx, dy ): self.circ1.move( dx, dy ) self.circ2.move( dx, dy ) class Car: """a class containing a rectangle and 2 wheels, forming a simple car""" def __init__( self, refPoint ): # create rectangle P1 = refPoint xP1 = P1.getX() yP1 = P1.getY() xP2 = xP1 + 180 yP2 = yP1 + 60 P2 = Point( xP2, yP2 ) self.body = Rectangle( P1, P2 ) self.body.setFill( "yellow" ) # create the Wheels self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) # set the initial speed self.dx = 1 self.dy = 0 def setSpeed( self, deltaX, deltaY ): self.dx = deltaX self.dy = deltaY def draw( self, win ): self.w1.draw( win ) self.body.draw( win ) self.w2.draw( win ) def autoMove( self ): self.w1.move( self.dx, self.dy ) self.w2.move( self.dx, self.dy ) self.body.move( self.dx, self.dy ) def reverseDirection( self ): self.dx = -self.dx def getX( self ): return self.body.getP1().getX() #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) waitForClick( win, "click to start" ) car1 = Car( Point( WIDTH//4, HEIGHT//4 ) ) car1.draw( win ) car1.setSpeed( 5, 0 ) while True: car1.autoMove() if car1.getX() > WIDTH or car1.getX() < 0: car1.reverseDirection() waitForClick( win, "click to end" ) win.close() main()
car3.py
# wheel1.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * import random # define geometry (constants) WIDTH = 700 HEIGHT = 600 class Wheel: """a Wheel is 2 concentric circles, the larger one black, the smaller one grey.""" def __init__(self, centr, rad1, rad2 ): # make circ1 the smaller of the 2 circles self.circ1 = Circle( centr, min( rad1, rad2 ) ) self.circ2 = Circle( centr, max( rad1, rad2 ) ) self.circ1.setFill( "grey" ) self.circ2.setFill( "black" ) def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) def move( self, dx, dy ): self.circ1.move( dx, dy ) self.circ2.move( dx, dy ) class Car: """a class containing a rectangle and 2 wheels, forming a simple car""" def __init__( self, refPoint ): # create rectangle P1 = refPoint xP1 = P1.getX() yP1 = P1.getY() xP2 = xP1 + 180 yP2 = yP1 + 60 P2 = Point( xP2, yP2 ) self.body = Rectangle( P1, P2 ) self.body.setFill( "yellow" ) # create the Wheels self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) # set the initial speed self.dx = 1 self.dy = 0 def setSpeed( self, deltaX, deltaY ): self.dx = deltaX self.dy = deltaY def draw( self, win ): self.w1.draw( win ) self.body.draw( win ) self.w2.draw( win ) def autoMove( self ): self.w1.move( self.dx, self.dy ) self.w2.move( self.dx, self.dy ) self.body.move( self.dx, self.dy ) def reverseDirection( self ): self.dx = -self.dx def getX( self ): return self.body.getP1().getX() #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) waitForClick( win, "click to start" ) cars = [] for x in range( 10, WIDTH, 80 ): car1 = Car( Point( x, random.choice( [10,50,100, 125, 175, 300 ]) ) ) car1.draw( win ) car1.setSpeed( random.choice( [-5, 3, 2, 5, 4] ), 0 ) cars.append( car1 ) while True: for car1 in cars: car1.autoMove() if car1.getX() > WIDTH or car1.getX() < 0: car1.reverseDirection() waitForClick( win, "click to end" ) win.close() main()
car3randomColors.py
# wheel1.py # D. Thiebaut # a skeleton program to get started with the graphics # from graphics import * import random # define geometry (constants) WIDTH = 700 HEIGHT = 600 class Wheel: """a Wheel is 2 concentric circles, the larger one black, the smaller one grey.""" def __init__(self, centr, rad1, rad2 ): # make circ1 the smaller of the 2 circles self.circ1 = Circle( centr, min( rad1, rad2 ) ) self.circ2 = Circle( centr, max( rad1, rad2 ) ) self.circ1.setFill( "grey" ) self.circ2.setFill( "black" ) def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) def move( self, dx, dy ): self.circ1.move( dx, dy ) self.circ2.move( dx, dy ) class Car: """a class containing a rectangle and 2 wheels, forming a simple car""" def __init__( self, refPoint ): # create rectangle P1 = refPoint xP1 = P1.getX() yP1 = P1.getY() xP2 = xP1 + 180 yP2 = yP1 + 60 P2 = Point( xP2, yP2 ) self.body = Rectangle( P1, P2 ) self.body.setFill( random.choice( ["yellow", "black", "beige", "magenta"] ) ) # create the Wheels self.w1 = Wheel( Point( xP1+40, yP1+60 ), 10, 20 ) self.w2 = Wheel( Point( xP1+140, yP1+60 ), 10, 20 ) # set the initial speed self.dx = 1 self.dy = 0 def setSpeed( self, deltaX, deltaY ): """change the speed of the car""" self.dx = deltaX self.dy = deltaY def draw( self, win ): """draw the car on the window""" self.w1.draw( win ) self.body.draw( win ) self.w2.draw( win ) def autoMove( self ): """move the car in the current direction (dx, dy)""" self.w1.move( self.dx, self.dy ) self.w2.move( self.dx, self.dy ) self.body.move( self.dx, self.dy ) def reverseDirection( self ): """reverses the direction of movement. If going right (dx>0), change to going left (dx<0). If going left, change to going right.""" self.dx = -self.dx def getX( self ): """return the x-coordinate of the top-left point of the rectangle""" return self.body.getP1().getX() #---------------------------------------------------------------- def waitForClick( win, message ): """ waitForClick: stops the GUI and displays a message. Returns when the user clicks the window. The message is erased.""" # wait for user to click mouse to start startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message ) startMsg.draw( win ) # display message win.getMouse() # wait startMsg.undraw() # erase def main(): global W, H win = GraphWin( "wheel demo", WIDTH, HEIGHT ) # wait for user to click the window... waitForClick( win, "click to start" ) # create a list of several cars, with random positions and random # speeds cars = [] for x in range( 10, WIDTH, 120 ): # create a new car at a random height in the window car1 = Car( Point( x, random.choice( [10,50,100, 125, 175, 300 ]) ) ) # draw the car car1.draw( win ) # set its speed randomly car1.setSpeed( random.choice( [-5, 30, 2, 15, -4] ), 0 ) # add the car to the list cars.append( car1 ) # animation loop: keep on going as long as the user does not click # on the window. while win.checkMouse()==None: # move each car of the list for car1 in cars: # move it car1.autoMove() # if it's hitting a left or right edge of the window # change the direction of movement of the car. if car1.getX() > WIDTH or car1.getX() < 0: car1.reverseDirection() # wait for the user to click one last time... waitForClick( win, "click to end" ) win.close() main()