Difference between revisions of "CSC111 Lab 12 2011"

From dftwiki3
Jump to: navigation, search
(Part 1: A Bus)
(Fish!)
 
(17 intermediate revisions by the same user not shown)
Line 18: Line 18:
 
<source lang="python">
 
<source lang="python">
 
# A program with a car and a wheel class.
 
# A program with a car and a wheel class.
# This is slightly different from what we did in class, but works
+
# Thanks to Victoria for providing the code!
# the same.  Figure out what the differences are.
 
 
#
 
#
 +
 
from graphics import *
 
from graphics import *
W = 800
+
W = 400
H = 600
+
H = 400
+
# creating a class for a taxicab
#----------------------------------------------------------------
+
 
 +
#start with class for wheel; two concentric circles of different colors
 
class Wheel:
 
class Wheel:
    """A class with two concentric circles"""
+
     def __init__(self, center, r1, r2 ): #__init__() is constructor; self is part of init; center, r1, r2 match parameters
+
         self.center = center
     def __init__( self, center, r1, r2 ):
+
         r1, r2 = min(r1, r2), max(r1, r2)
        self.circ1 = Circle( center, r1 )
 
         self.circ2 = Circle( center, r2 )
 
         r1, r2 = min( r1, r2 ), max( r1, r2 )
 
 
         self.radius1 = r1
 
         self.radius1 = r1
 
         self.radius2 = r2
 
         self.radius2 = r2
+
         self.c1 = Circle( center, r1 )
    def draw( self, win ):
+
         self.c2 = Circle( center, r2 )
        """draws the wheel on the window"""
+
 
         self.circ2.draw( win )
+
     def setFill( self, col1, col2 ):
         self.circ1.draw( win )
+
         self.c2.setFill( col1 )
+
         self.c1.setFill( col2 )
     def setFill( self, color1, color2 ):
+
       
        """colors the outside and inside circles.  First color for small circle"""
+
     def draw( self, win): #win matches parameter sent to draw in main
         self.circ1.setFill( color1 )
+
         self.c2.draw( win )
         self.circ2.setFill( color2 )
+
         self.c1.draw( win )
+
 
     def getRadius1( self ):
+
#class for car
         """returns radius of small circle"""
 
        return self.radius1
 
 
    def getRadius2( self ):
 
        """returns radius of large circle"""
 
         return self.radius2
 
 
#----------------------------------------------------------------
 
 
class Car:
 
class Car:
     """A class for a car, which is a rectangle and two wheels"""
+
     def __init__(self, p1, p2 ):
 +
        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 __init__( self, P1, P2 ):
 
        """Get 2 points and create the box and two wheels from the dimensions"""
 
        self.P1 = P1
 
        self.P2 = P2
 
        self.width = abs( P1.getX()-P2.getX() )
 
        self.height= abs( P1.getY()-P2.getY() )
 
 
        self.rect    = Rectangle( P1, P2 )
 
        center1      = Point( P1.getX()+self.width/8, P2.getY() )
 
        center2      = Point( P1.getX()+self.width*7/8, P2.getY() )
 
        radius2      = self.height/3
 
        radius1      = radius2/2
 
        self.wheel1  = Wheel( center1, radius1, radius2 )
 
        self.wheel2  = Wheel( center2, radius1, radius2 )
 
 
 
     def draw( self, win ):
 
     def draw( self, win ):
        """draws the car on the screen.  One of the wheels is covered"""
+
         self.w2.draw( win )
         self.wheel2.draw( win )
 
 
         self.rect.draw( win )
 
         self.rect.draw( win )
         self.wheel1.draw( win )
+
         self.w1.draw( win )
  
     def move( self, dx, dy ):
+
     def setFill( self, col1, col2, col3 ):
        """moves the car by dx and dy"""
+
         self.rect.setFill( col1 )
         self.wheel2.move( dx, dy )
+
         self.w1.setFill( col2, col3 )
         self.rect.move( dx, dy )
+
         self.w2.setFill( col2, col3 )
         self.wheel1.move( dx, dy )  
+
       
 
#----------------------------------------------------------------
 
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():
 
def main():
 
     global W, H
 
     global W, H
     win = GraphWin( "wheel demo", W, H )
+
     win = GraphWin( "Wheel Class Demo", W, H)
+
 
    waitForClick( win, "click to start" )
+
     car = Car( Point( 50,50 ), Point( 250, 100 ) )
     car = Car( Point( 100,100 ), Point( 250, 170 ) )
+
    car.setFill( "yellow", "white", "black" )
 
 
     car.draw( win )
 
     car.draw( win )
     waitForClick( win, "click to end" )
+
      
+
    win.getMouse()
 +
    win.close()  
 +
 
 
main()
 
main()
 +
 
</source>
 
</source>
  
Line 120: Line 92:
 
* Add a for-loop that makes your bus disappear from the window.  Make it go in the forward direction (i.e. left for the bus in the image above).
 
* Add a for-loop that makes your bus disappear from the window.  Make it go in the forward direction (i.e. left for the bus in the image above).
 
<br />
 
<br />
 +
 +
  
 
=Part 2: Trees=
 
=Part 2: Trees=
Line 126: Line 100:
 
<center>[[Image:CSC111Bus_and_Tree.png|500px]]</center>
 
<center>[[Image:CSC111Bus_and_Tree.png|500px]]</center>
 
<br />
 
<br />
Create a new class in your program called '''Tree'''.  It will represent a simplified tree, with its green head of leaves, and its brown trunk.  
+
* Create a new class in your program called '''Tree'''.  It will represent a simplified tree, with its green head of leaves, and its brown trunk.
 +
* Make your program display the bus and the tree.  It doesn't matter if the tree is in front or behind the bus.
 +
*  Below is a minimum class outline for the tree class...
 +
 
 +
<source lang="python">
 +
 
 +
class Tree:
 +
 +
    def __init__( ... ):
 +
        ...
 +
 
 +
    def draw( ... ):
 +
        ...
 +
 
 +
 +
</source>
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 2==
 +
|}
 +
[[Image:QuestionMark2.jpg|right|120px]]
 +
<br />
 +
* Make the bus stand still and the tree move in the direction opposite to that of the bus.
 +
* Make the tree move '''out of the window''' for right now.  No need to make it come back...
  
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 3==
 +
|}
 +
[[Image:QuestionMark3.jpg|right|120px]]
 +
<br />
 +
* Add another tree object.  One class: two objects.
 +
* Place the trees in different locations so that they do not overlap 
 +
* Make your program display the two trees and the bus
  
  
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 4==
 +
|}
 +
[[Image:QuestionMark4.jpg|right|120px]]
 +
<br />
  
 +
* Make the two trees move to the right and disappear from the window
  
 +
<br />
  
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 5==
 +
|}
 +
[[Image:QuestionMark12.jpg|right|120px]]
 +
<br />
  
 +
* Make the trees that disappear from the right reappear on the left, giving the illusion of a real cartoon!
 +
* One way to make this happen is to edit the '''move''' ''method'' of the '''Tree''' class so that if the '''x''' location of some point associated with the tree is ''out of the window'' then you make the tree trunk and head move by a large x value proportional to the width of the window.
  
  
 +
<br />
 +
<source lang="python">
  
 +
    class Tree:
 +
        ...
 +
        def move( ... ):
 +
              ...
 +
              x = ...
 +
              if x > ...:
 +
                trunk.move( ... )
 +
                head.move( ... )
  
  
 +
 +
    def main():
 +
        ...
 +
 +
 +
</source>
 +
<br />
 +
<br />
 +
 +
=Fish! =
 +
The Fish section has been reported to next week's lab, [[CSC111 Lab 13 2011 | Lab 13]].
  
 
<br />
 
<br />

Latest revision as of 18:27, 1 December 2011

--D. Thiebaut 09:42, 30 November 2011 (EST)


This lab will have you play with classes and objects: the essence of Object Oriented Programming!


Part 1: A Bus

CSC111 bus.png


  • Using the example we did in class on Tuesday as inspiration (see similar code below), create a new program that draws the bus shown above.
  • Your program should have two classes:
    • class Wheel
    • class Bus



# A program with a car and a wheel class.
# Thanks to Victoria for providing the code! 
#

from graphics import *
W = 400
H = 400
# creating a class for a taxicab

#start with class for wheel; two concentric circles of different colors
class Wheel:
    def __init__(self, center, r1, r2 ): #__init__() is constructor; self is part of init; center, r1, r2 match parameters
        self.center = center
        r1, r2 = min(r1, r2), max(r1, r2)
        self.radius1 = r1
        self.radius2 = r2
        self.c1 = Circle( center, r1 )
        self.c2 = Circle( center, r2 )

    def setFill( self, col1, col2 ):
        self.c2.setFill( col1 )
        self.c1.setFill( col2 )
        
    def draw( self, win): #win matches parameter sent to draw in main
        self.c2.draw( win )
        self.c1.draw( win )

#class for car
class Car:
    def __init__(self, p1, p2 ):
        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 ):
        self.w2.draw( win )
        self.rect.draw( win )
        self.w1.draw( win )

    def setFill( self, col1, col2, col3 ):
        self.rect.setFill( col1 )
        self.w1.setFill( col2, col3 )
        self.w2.setFill( col2, col3 )
        
def main():
    global W, H
    win = GraphWin( "Wheel Class Demo", W, H)

    car = Car( Point( 50,50 ), Point( 250, 100 ) )
    car.setFill( "yellow", "white", "black" )
    car.draw( win )
    
    win.getMouse()
    win.close() 
   
main()


Challenge 1

QuestionMark1.jpg


  • Add a for-loop that makes your bus disappear from the window. Make it go in the forward direction (i.e. left for the bus in the image above).



Part 2: Trees


CSC111Bus and Tree.png


  • Create a new class in your program called Tree. It will represent a simplified tree, with its green head of leaves, and its brown trunk.
  • Make your program display the bus and the tree. It doesn't matter if the tree is in front or behind the bus.
  • Below is a minimum class outline for the tree class...
class Tree:
 
    def __init__( ... ):
        ...

    def draw( ... ):
        ...


Challenge 2

QuestionMark2.jpg


  • Make the bus stand still and the tree move in the direction opposite to that of the bus.
  • Make the tree move out of the window for right now. No need to make it come back...

Challenge 3

QuestionMark3.jpg


  • Add another tree object. One class: two objects.
  • Place the trees in different locations so that they do not overlap
  • Make your program display the two trees and the bus


Challenge 4

QuestionMark4.jpg


  • Make the two trees move to the right and disappear from the window


Challenge 5

QuestionMark12.jpg


  • Make the trees that disappear from the right reappear on the left, giving the illusion of a real cartoon!
  • One way to make this happen is to edit the move method of the Tree class so that if the x location of some point associated with the tree is out of the window then you make the tree trunk and head move by a large x value proportional to the width of the window.



     class Tree:
         ...
         def move( ... ):
              ...
              x = ...
              if x > ...:
                 trunk.move( ... )
                 head.move( ... )



     def main():
         ...



Fish!

The Fish section has been reported to next week's lab, Lab 13.