Difference between revisions of "CSC111 Lab 12 2011"
(→Challenge 5) |
(→Part 1: A Bus) |
||
Line 21: | Line 21: | ||
# the same. Figure out what the differences are. | # the same. Figure out what the differences are. | ||
# | # | ||
+ | """ | ||
+ | Objects: | ||
+ | Rectangle | ||
+ | Circle | ||
+ | Text | ||
+ | Point | ||
+ | Line | ||
+ | |||
+ | r = Rectangle( Point(x1, y1), Point(x2, y2) ) | ||
+ | # object = class( information) | ||
+ | r.draw(win) | ||
+ | # .draw() is a method | ||
+ | |||
+ | class + parameters becomes the data; can apply methods, like "buttons" | ||
+ | """ | ||
from graphics import * | from graphics import * | ||
− | W = | + | W = 400 |
− | H = | + | H = 400 |
− | + | # creating a class for a taxicab | |
− | # | + | |
+ | #start with class for wheel; two concentric circles of different colors | ||
class Wheel: | class Wheel: | ||
− | + | 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. | ||
− | 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 ) | |
− | + | self.c2 = Circle( center, r2 ) | |
− | + | ||
− | self. | + | def setFill( self, col1, col2 ): |
− | self. | + | self.c2.setFill( col1 ) |
− | + | self.c1.setFill( col2 ) | |
− | def setFill( self, | + | |
− | + | def draw( self, win): #win matches parameter sent to draw in main | |
− | self. | + | self.c2.draw( win ) |
− | self. | + | self.c1.draw( win ) |
− | + | ||
− | def | + | #class for car |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | # | ||
class 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 ): | def draw( self, win ): | ||
− | + | self.w2.draw( win ) | |
− | self. | ||
self.rect.draw( win ) | self.rect.draw( win ) | ||
− | self. | + | self.w1.draw( win ) |
− | def | + | def setFill( self, col1, col2, col3 ): |
− | + | self.rect.setFill( col1 ) | |
− | self. | + | self.w1.setFill( col2, col3 ) |
− | self. | + | self.w2.setFill( col2, col3 ) |
− | self. | + | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
def main(): | def main(): | ||
global W, H | global W, H | ||
− | win = GraphWin( " | + | win = GraphWin( "Wheel Class Demo", W, H) |
− | + | ||
− | |||
− | |||
+ | """ | ||
+ | w = Wheel( Point( W/2, H/2 ), 20, 30 ) #20 and 30 are two radii | ||
+ | w.setFill( "yellow", "black" ) #small circle then big circle | ||
+ | w.draw( win ) | ||
+ | """ | ||
+ | |||
+ | car = Car( Point( 50,50 ), Point( 250, 100 ) ) | ||
+ | car.setFill( "yellow", "white", "black" ) | ||
car.draw( win ) | car.draw( win ) | ||
− | + | ||
− | + | win.getMouse() | |
+ | win.close() | ||
+ | |||
main() | main() | ||
+ | |||
</source> | </source> | ||
Revision as of 12:15, 30 November 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!
Contents
Part 1: A Bus
- 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.
# This is slightly different from what we did in class, but works
# the same. Figure out what the differences are.
#
"""
Objects:
Rectangle
Circle
Text
Point
Line
r = Rectangle( Point(x1, y1), Point(x2, y2) )
# object = class( information)
r.draw(win)
# .draw() is a method
class + parameters becomes the data; can apply methods, like "buttons"
"""
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)
"""
w = Wheel( Point( W/2, H/2 ), 20, 30 ) #20 and 30 are two radii
w.setFill( "yellow", "black" ) #small circle then big circle
w.draw( win )
"""
car = Car( Point( 50,50 ), Point( 250, 100 ) )
car.setFill( "yellow", "white", "black" )
car.draw( win )
win.getMouse()
win.close()
main()
Challenge 1 |
- 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).
class Tree:
def __init__( ... ):
...
def draw( ... ):
...
def move( ... ):
...
- Make your program display the tree with the bus. No need for any of them to move, but that's fine if the bus is moving out of the window...
Part 2: Trees
- 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.
Challenge 2 |
- Make the bus stand still and the tree move in the direction opposite of the bus direction.
- Make the tree move out of the window
Challenge 3 |
- Add another tree object. One class: two objects.
- Locate the trees in different locations so that they do not overlap (hint: you can change the constructor so that you can specify the x-coordinate of the tree object when you create it:)
- Make your program display the two trees and the bus
Challenge 4 |
- Make the two trees move to the right and disappear from the window
Challenge 5 |
- Make the trees that disappear from the right reappear on the left, giving the illusion of a real cartoon!
- For this your program will have to ask the tree object to return its x position, and if this position is larger than the width plus some offset, then the tree will have to move by a large negative dx value...
class Tree:
...
def getX( ... ):
return ...
def main():
...
if tree.getX() > width:
tree.move( -nnnn, 0 )