Difference between revisions of "CSC111 Car and Trees program"
(Created page with "--~~~~ ---- <source lang="python"> from graphics import * import random W = 800 H = 600 #---------------------------------------------------------------- class Wheel: """A ...") |
|||
Line 12: | Line 12: | ||
def __init__( self, center, r1, r2 ): | def __init__( self, center, r1, r2 ): | ||
+ | """create the two wheels. Make sure r1 is the smallest radius of the two""" | ||
r1, r2 = min( r1, r2 ), max( r1, r2 ) | r1, r2 = min( r1, r2 ), max( r1, r2 ) | ||
self.circ1 = Circle( center, r1 ) | self.circ1 = Circle( center, r1 ) | ||
Line 19: | Line 20: | ||
def setFill( self, col1, col2 ): | def setFill( self, col1, col2 ): | ||
+ | """set the colors. col1 is the color of the smaller of the two radii""" | ||
self.circ1.setFill( col1 ) | self.circ1.setFill( col1 ) | ||
self.circ2.setFill( col2 ) | self.circ2.setFill( col2 ) | ||
def draw( self, win ): | def draw( self, win ): | ||
+ | """draw the two circles on the window""" | ||
self.circ2.draw( win ) | self.circ2.draw( win ) | ||
self.circ1.draw( win ) | self.circ1.draw( win ) | ||
Line 33: | Line 36: | ||
def DTmove( self, dx, dy ): | def DTmove( self, dx, dy ): | ||
+ | """moves the 2 circles by dx and dy""" | ||
self.circ1.move( dx, dy ) | self.circ1.move( dx, dy ) | ||
self.circ2.move( dx, dy ) | self.circ2.move( dx, dy ) | ||
class Car: | class Car: | ||
+ | """A class for a car, made of 1 rectangle and 2 wheels""" | ||
+ | |||
def __init__( self, p1, p2, width ): | def __init__( self, p1, p2, width ): | ||
+ | """create the car, with 1 rectangle, 2 wheels. Store width for | ||
+ | future operations with the move method""" | ||
self.width = width | self.width = width | ||
self.rect = Rectangle( p1, p2 ) | self.rect = Rectangle( p1, p2 ) | ||
Line 50: | Line 58: | ||
def draw( self, win ): | def draw( self, win ): | ||
+ | """draws the wheels and the rectangle""" | ||
self.w1.draw( win ) | self.w1.draw( win ) | ||
self.rect.draw( win ) | self.rect.draw( win ) | ||
Line 55: | Line 64: | ||
def setFill( self, col1, col2, col3 ): | def setFill( self, col1, col2, col3 ): | ||
+ | """colors the car. col1 is the body color. col2 the inner color, | ||
+ | col3 with tire color""" | ||
self.rect.setFill( col1 ) | self.rect.setFill( col1 ) | ||
self.w1.setFill( col2, col3 ) | self.w1.setFill( col2, col3 ) | ||
Line 60: | Line 71: | ||
def DTmove( self, dx, dy ): | def DTmove( self, dx, dy ): | ||
+ | """moves the car. If it goes off the window, make it | ||
+ | reappear on the other side""" | ||
+ | |||
self.rect.move( dx, dy ) | self.rect.move( dx, dy ) | ||
self.w1.DTmove( dx, dy ) | self.w1.DTmove( dx, dy ) | ||
self.w2.DTmove( dx, dy ) | self.w2.DTmove( dx, dy ) | ||
x = self.rect.getP1().getX() | x = self.rect.getP1().getX() | ||
+ | |||
if x > self.width: | if x > self.width: | ||
self.rect.move( -self.width, 0 ) | self.rect.move( -self.width, 0 ) | ||
Line 70: | Line 85: | ||
class Tree: | class Tree: | ||
+ | """class for a tree, made of a trunk and a head of leaves""" | ||
+ | |||
def __init__( self, point, height, width ): | def __init__( self, point, height, width ): | ||
+ | """The tree is defined by the point at the base of its trunk, | ||
+ | and its height. The width of the window is passed for later | ||
+ | use by the move method""" | ||
+ | |||
self.width = width | self.width = width | ||
x = point.getX() | x = point.getX() | ||
Line 82: | Line 103: | ||
def draw( self, win ): | def draw( self, win ): | ||
+ | """draws the trunk and head of the tree""" | ||
self.trunk.draw( win ) | self.trunk.draw( win ) | ||
self.head.draw( win ) | self.head.draw( win ) | ||
def DTmove( self, dx, dy ): | def DTmove( self, dx, dy ): | ||
+ | """moves the tree. If the tree moves off to the left of the | ||
+ | window, it reappears on the right-hand side""" | ||
self.trunk.move( dx, dy ) | self.trunk.move( dx, dy ) | ||
self.head.move( dx, dy ) | self.head.move( dx, dy ) | ||
Line 105: | Line 129: | ||
+ | #---------------------------------------------------------------- | ||
def main(): | def main(): | ||
global W, H | global W, H | ||
win = GraphWin( "wheel demo", W, H ) | win = GraphWin( "wheel demo", W, H ) | ||
+ | # create a list of trees | ||
trees = [] | trees = [] | ||
− | for i in range( | + | for i in range( 4 ): |
tree = Tree( Point( random.randrange(10,W), 550 ), 400, W ) | tree = Tree( Point( random.randrange(10,W), 550 ), 400, W ) | ||
tree.draw( win ) | tree.draw( win ) | ||
trees.append( tree ) | trees.append( tree ) | ||
+ | # create the car | ||
car = Car( Point( 50,450 ), Point( 250, 550 ), W ) | car = Car( Point( 50,450 ), Point( 250, 550 ), W ) | ||
car.setFill( "yellow", "white", "black" ) | car.setFill( "yellow", "white", "black" ) | ||
car.draw( win ) | car.draw( win ) | ||
− | + | # animation loop | |
while True: | while True: | ||
− | |||
for tree in trees: | for tree in trees: | ||
tree.DTmove( -3, 0 ) | tree.DTmove( -3, 0 ) |
Latest revision as of 14:24, 1 December 2011
--D. Thiebaut 11:49, 1 December 2011 (EST)
from graphics import *
import random
W = 800
H = 600
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
"""create the two wheels. Make sure r1 is the smallest radius of the two"""
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
self.radius1 = r1
self.radius2 = r2
def setFill( self, col1, col2 ):
"""set the colors. col1 is the color of the smaller of the two radii"""
self.circ1.setFill( col1 )
self.circ2.setFill( col2 )
def draw( self, win ):
"""draw the two circles on the window"""
self.circ2.draw( win )
self.circ1.draw( win )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
def DTmove( self, dx, dy ):
"""moves the 2 circles by dx and dy"""
self.circ1.move( dx, dy )
self.circ2.move( dx, dy )
class Car:
"""A class for a car, made of 1 rectangle and 2 wheels"""
def __init__( self, p1, p2, width ):
"""create the car, with 1 rectangle, 2 wheels. Store width for
future operations with the move method"""
self.width = width
self.rect = Rectangle( p1, p2 )
length = abs( p1.getX()-p2.getX() )
xWheel1 = p1.getX() + length/4
yWheel1 = p2.getY()
xWheel2 = p2.getX() - length/4
yWheel2 = yWheel1
self.w1 = Wheel( Point( xWheel1, yWheel1 ), length/16, length/8 )
self.w2 = Wheel( Point( xWheel2, yWheel2 ), length/16, length/8 )
def draw( self, win ):
"""draws the wheels and the rectangle"""
self.w1.draw( win )
self.rect.draw( win )
self.w2.draw( win )
def setFill( self, col1, col2, col3 ):
"""colors the car. col1 is the body color. col2 the inner color,
col3 with tire color"""
self.rect.setFill( col1 )
self.w1.setFill( col2, col3 )
self.w2.setFill( col2, col3 )
def DTmove( self, dx, dy ):
"""moves the car. If it goes off the window, make it
reappear on the other side"""
self.rect.move( dx, dy )
self.w1.DTmove( dx, dy )
self.w2.DTmove( dx, dy )
x = self.rect.getP1().getX()
if x > self.width:
self.rect.move( -self.width, 0 )
self.w1.DTmove( -self.width, 0 )
self.w2.DTmove( -self.width, 0 )
class Tree:
"""class for a tree, made of a trunk and a head of leaves"""
def __init__( self, point, height, width ):
"""The tree is defined by the point at the base of its trunk,
and its height. The width of the window is passed for later
use by the move method"""
self.width = width
x = point.getX()
y = point.getY()
p1 = Point( x-20, y )
p2 = Point( x+20, y - height * 2 /3 )
self.trunk = Rectangle( p1, p2 )
self.trunk.setFill( "brown" )
self.head = Circle( Point( x, y - height * 5/ 6 ), height/6 )
self.head.setFill( "green" )
def draw( self, win ):
"""draws the trunk and head of the tree"""
self.trunk.draw( win )
self.head.draw( win )
def DTmove( self, dx, dy ):
"""moves the tree. If the tree moves off to the left of the
window, it reappears on the right-hand side"""
self.trunk.move( dx, dy )
self.head.move( dx, dy )
x = self.trunk.getP1().getX()
if x < 0:
self.trunk.move( self.width, 0 )
self.head.move( self.width, 0 )
#----------------------------------------------------------------
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", W, H )
# create a list of trees
trees = []
for i in range( 4 ):
tree = Tree( Point( random.randrange(10,W), 550 ), 400, W )
tree.draw( win )
trees.append( tree )
# create the car
car = Car( Point( 50,450 ), Point( 250, 550 ), W )
car.setFill( "yellow", "white", "black" )
car.draw( win )
# animation loop
while True:
for tree in trees:
tree.DTmove( -3, 0 )
if win.checkMouse() != None:
break
waitForClick( win, "click to end" )
win.close()
main()