Difference between revisions of "CSC111 A class for a simple car"
(→wheel2.py) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 12:51, 5 April 2010 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 12:51, 5 April 2010 (UTC) | ||
---- | ---- | ||
− | + | <onlydft> | |
==wheel1.py== | ==wheel1.py== | ||
<br /> | <br /> | ||
Line 14: | Line 14: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 73: | Line 74: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 130: | Line 132: | ||
<source lang="python"> | <source lang="python"> | ||
# wheel3.py | # wheel3.py | ||
− | # | + | # A new method is added to wheel2.py so that the |
− | # a | + | # wheel can now move with a dx, dy speed and |
+ | # direction. | ||
from graphics import * | from graphics import * | ||
Line 137: | Line 140: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 198: | Line 202: | ||
==car1.py== | ==car1.py== | ||
+ | See last version of car.py on this page for full documentation. | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 209: | Line 214: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 233: | Line 239: | ||
return self.radius2 | return self.radius2 | ||
+ | #---------------------------------------------------------------- | ||
class Car: | class Car: | ||
def __init__( self, P1, P2 ): | def __init__( self, P1, P2 ): | ||
Line 285: | Line 292: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | # | + | # car2.py |
− | # | + | # Same as car1, but we add a method to define the color of |
− | # | + | # the car, including the wheels |
+ | # | ||
from graphics import * | from graphics import * | ||
Line 293: | Line 301: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 317: | Line 326: | ||
return self.radius2 | return self.radius2 | ||
+ | #---------------------------------------------------------------- | ||
class Car: | class Car: | ||
def __init__( self, P1, P2 ): | def __init__( self, P1, P2 ): | ||
Line 370: | Line 380: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
==car3.py== | ==car3.py== | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | # | + | # car3.py |
− | # | + | # Now the car can move! |
− | |||
from graphics import * | from graphics import * | ||
Line 382: | Line 392: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 410: | Line 421: | ||
self.circ2.move( dx, dy ) | self.circ2.move( dx, dy ) | ||
+ | #---------------------------------------------------------------- | ||
class Car: | class Car: | ||
+ | """a class containing a rectangle and 2 wheels""" | ||
+ | |||
def __init__( self, P1, P2 ): | def __init__( self, P1, P2 ): | ||
+ | """constructs the car. The top-left and bottom-right points | ||
+ | defining the body of the car are given.""" | ||
self.P1 = P1 | self.P1 = P1 | ||
self.P2 = P2 | self.P2 = P2 | ||
Line 417: | Line 433: | ||
self.height= abs( P1.getY()-P2.getY() ) | self.height= abs( P1.getY()-P2.getY() ) | ||
+ | #--- define rectangle--- | ||
self.rect = Rectangle( P1, P2 ) | self.rect = Rectangle( P1, P2 ) | ||
+ | |||
+ | #--- and the two wheels --- | ||
center1 = Point( P1.getX()+self.width/8, P2.getY() ) | center1 = Point( P1.getX()+self.width/8, P2.getY() ) | ||
center2 = Point( P1.getX()+self.width*7/8, P2.getY() ) | center2 = Point( P1.getX()+self.width*7/8, P2.getY() ) | ||
Line 426: | Line 445: | ||
def draw( self, win ): | def draw( self, win ): | ||
+ | """draw rectangle and 2 wheels on window""" | ||
self.rect.draw( win ) | self.rect.draw( win ) | ||
self.wheel1.draw( win ) | self.wheel1.draw( win ) | ||
Line 431: | Line 451: | ||
def setFill( self, color1, color2, color3 ): | def setFill( self, color1, color2, color3 ): | ||
+ | """defines the color of the car. First is body, then inside wheel, then tire color""" | ||
self.rect.setFill( color1 ) | self.rect.setFill( color1 ) | ||
self.wheel1.setFill( color2, color3 ) | self.wheel1.setFill( color2, color3 ) | ||
Line 436: | Line 457: | ||
def move( self, dx, dy ): | def move( self, dx, dy ): | ||
+ | """defines direction of movement for all 3 elements of car""" | ||
self.rect.move( dx, dy ) | self.rect.move( dx, dy ) | ||
self.wheel1.move( dx, dy ) | self.wheel1.move( dx, dy ) | ||
Line 472: | Line 494: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | </onlydft> |