Difference between revisions of "CSC111 Lab 12 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Building a car using the Graphics111 library= * Get a copy of the graphics111.py library * Save it in your main directory (where ...")
 
(Building a car using the Graphics111 library)
Line 7: Line 7:
 
* Create a car using the new classes now available in the graphics111 library.  Call it '''lab12.py'''.
 
* Create a car using the new classes now available in the graphics111 library.  Call it '''lab12.py'''.
 
<br />
 
<br />
<source lang="python">
+
::<source lang="python">
 +
from graphics111 import *
 +
 
 +
MAXWIDTH  = 600
 +
MAXHEIGHT = 400
 +
 
 +
class Car:
 +
    def __init__(self, x, y, w, h, color ):
 +
        self._body = Rectangle( x, y, w, h, color )
 +
        self._w1  = Wheel( x+w//4, y+h, w//4 )
 +
        self._w2  = Wheel( x+3*w//4, y+h, w//4 )
 +
       
 +
    def draw( self, canvas ):
 +
        self._body.draw( canvas )
 +
        self._w1.draw( canvas )
 +
        self._w2.draw( canvas )
 +
       
 +
def main():   
 +
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
 +
    canvas = win.canvas()
 +
 
 +
    p1 = Car( 100, 100, 50, 20, (250, 250, 0) )
 +
    p1.draw( canvas )
 +
 
 +
    win.wait()
 +
    win.close()
 +
 
 +
main()
 +
</source>
 +
<br />
 +
* Notice how short the program is now that the classes for the wheels and rectangles are in the graphics111 library!
 +
 
 +
=Adding A Menu=
 +
* Add a menu at the top left of the window.  Don't try to activate it, it won't respond yet...
 +
<br />
 +
<source lang="python" highlight="22-23">
 
from graphics111 import *
 
from graphics111 import *
  
Line 25: Line 60:
 
def main():
 
def main():
 
     global menu  
 
     global menu  
   
+
 
 
     win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
 
     win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
 
     canvas = win.canvas()
 
     canvas = win.canvas()
  
 +
    menu = Menu()
 +
    menu.draw( canvas )
 +
   
 
     p1 = Car( 100, 100, 50, 20, (250, 250, 0) )
 
     p1 = Car( 100, 100, 50, 20, (250, 250, 0) )
 
     p1.draw( canvas )
 
     p1.draw( canvas )
Line 36: Line 74:
  
 
main()
 
main()
 +
 
</source>
 
</source>
 
<br />
 
<br />

Revision as of 17:53, 21 April 2014

--D. Thiebaut (talk) 18:47, 21 April 2014 (EDT)


Building a car using the Graphics111 library

  • Get a copy of the graphics111.py library
  • Save it in your main directory (where you keep you python programs) and save it as graphics111.py
  • Create a car using the new classes now available in the graphics111 library. Call it lab12.py.


from graphics111 import *

MAXWIDTH  = 600
MAXHEIGHT = 400

class Car:
    def __init__(self, x, y, w, h, color ):
        self._body = Rectangle( x, y, w, h, color )
        self._w1   = Wheel( x+w//4, y+h, w//4 )
        self._w2   = Wheel( x+3*w//4, y+h, w//4 )
        
    def draw( self, canvas ):
        self._body.draw( canvas )
        self._w1.draw( canvas )
        self._w2.draw( canvas )
        
def main():    
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()

    p1 = Car( 100, 100, 50, 20, (250, 250, 0) )
    p1.draw( canvas )

    win.wait()
    win.close()

main()


  • Notice how short the program is now that the classes for the wheels and rectangles are in the graphics111 library!

Adding A Menu

  • Add a menu at the top left of the window. Don't try to activate it, it won't respond yet...


from graphics111 import *

MAXWIDTH  = 600
MAXHEIGHT = 400
class Car:
    def __init__(self, x, y, w, h, color ):
        self._body = Rectangle( x, y, w, h, color )
        self._w1   = Wheel( x+w//4, y+h, w//4 )
        self._w2   = Wheel( x+3*w//4, y+h, w//4 )
        
    def draw( self, canvas ):
        self._body.draw( canvas )
        self._w1.draw( canvas )
        self._w2.draw( canvas )
        
def main():
    global menu 

    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()

    menu = Menu()
    menu.draw( canvas )
    
    p1 = Car( 100, 100, 50, 20, (250, 250, 0) )
    p1.draw( canvas )

    win.wait()
    win.close()

main()