Difference between revisions of "CSC111 A class for a simple car"

From dftwiki3
Jump to: navigation, search
(wheel2.py)
 
(9 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 66: Line 67:
 
<source lang="python">
 
<source lang="python">
 
# wheel2.py
 
# wheel2.py
# first program using a class to define
+
# Same program as wheel1.py, but this time we add
# a new graphic object.
+
# 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 129: Line 131:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
# wheel1.py
+
# wheel3.py
# first program using a class to define
+
# A new method is added to wheel2.py so that the
# a new graphic object.
+
# 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 196: 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">
# wheel1.py
+
# car1.py
# first program using a class to define
+
# We add a rectangle and two wheels to create a new class for a
# a new graphic object.
+
# car!
  
 
from graphics import *
 
from graphics import *
Line 208: Line 214:
 
H = 400
 
H = 400
  
 +
#----------------------------------------------------------------
 
class Wheel:
 
class Wheel:
 
     """A class with two concentric circles"""
 
     """A class with two concentric circles"""
Line 232: Line 239:
 
         return self.radius2
 
         return self.radius2
  
 +
#----------------------------------------------------------------
 
class Car:
 
class Car:
 
     def __init__( self, P1, P2 ):
 
     def __init__( self, P1, P2 ):
Line 279: Line 287:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
==car2.py==
 
==car2.py==
 
<br />
 
<br />
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
# wheel1.py
+
# car2.py
# first program using a class to define
+
# Same as car1, but we add a method to define the color of
# a new graphic object.
+
# the car, including the wheels
 +
#
  
 
from graphics import *
 
from graphics import *
Line 291: Line 301:
 
H = 400
 
H = 400
  
 +
#----------------------------------------------------------------
 
class Wheel:
 
class Wheel:
 
     """A class with two concentric circles"""
 
     """A class with two concentric circles"""
Line 315: Line 326:
 
         return self.radius2
 
         return self.radius2
  
 +
#----------------------------------------------------------------
 
class Car:
 
class Car:
 
     def __init__( self, P1, P2 ):
 
     def __init__( self, P1, P2 ):
Line 368: Line 380:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
==car3.py==
 
==car3.py==
 
<br />
 
<br />
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
# wheel1.py
+
# car3.py
# first program using a class to define
+
# Now the car can move!
# a new graphic object.
 
  
 
from graphics import *
 
from graphics import *
Line 380: Line 392:
 
H = 400
 
H = 400
  
 +
#----------------------------------------------------------------
 
class Wheel:
 
class Wheel:
 
     """A class with two concentric circles"""
 
     """A class with two concentric circles"""
Line 408: 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 415: 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 424: 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 429: 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 434: 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 470: Line 494:
 
<br />
 
<br />
 
<br />
 
<br />
 +
</onlydft>

Latest revision as of 05:51, 29 March 2015

--D. Thiebaut 12:51, 5 April 2010 (UTC)



...