Difference between revisions of "CSC111 A class for a simple car"
(Created page with '--~~~~ ---- ==wheel1.py== <br /> <br /> <source lang="python"> # wheel1.py # first program using a class to define # a new graphic object. from graphics import * W = 400 H = 40…') |
|||
(10 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 65: | Line 66: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | # wheel1.py | + | # wheel2.py |
− | # | + | # Same program as wheel1.py, but this time we add |
− | + | # a method to define the color of the wheel. | |
from graphics import * | from graphics import * | ||
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 124: | Line 126: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
==wheel3.py== | ==wheel3.py== | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | # | + | # 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 136: | Line 140: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 195: | Line 200: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
==car1.py== | ==car1.py== | ||
+ | See last version of car.py on this page for full documentation. | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | # | + | # car1.py |
− | # | + | # We add a rectangle and two wheels to create a new class for a |
− | # | + | # car! |
from graphics import * | from graphics import * | ||
Line 207: | Line 214: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 231: | Line 239: | ||
return self.radius2 | return self.radius2 | ||
+ | #---------------------------------------------------------------- | ||
class Car: | class Car: | ||
def __init__( self, P1, P2 ): | def __init__( self, P1, P2 ): | ||
Line 278: | Line 287: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
==car2.py== | ==car2.py== | ||
<br /> | <br /> | ||
<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 290: | Line 301: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 314: | Line 326: | ||
return self.radius2 | return self.radius2 | ||
+ | #---------------------------------------------------------------- | ||
class Car: | class Car: | ||
def __init__( self, P1, P2 ): | def __init__( self, P1, P2 ): | ||
Line 367: | 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 379: | Line 392: | ||
H = 400 | H = 400 | ||
+ | #---------------------------------------------------------------- | ||
class Wheel: | class Wheel: | ||
"""A class with two concentric circles""" | """A class with two concentric circles""" | ||
Line 407: | 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 414: | 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 423: | 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 428: | 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 433: | 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 469: | Line 494: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | </onlydft> |