A class serves to encapsuate data using a a set of variables and methods to manipulate this data. We have previously used predefined classes like the graphics and time. In today's lab, we will define our own class to represent a simple graphics object. |
|
Here is an example from the textbook illustrating a simple class,
|
Save graphics.py in the same directory as Fish.py below.
############################################################################
#
# fish.py
# A class which illustrates a class , using John Zelle's graphics.py module.
# Author: Aaron Stevens (azs@bu.edu)
#
import time
from graphics import *
############################################################################
class Fish:
# init method is used to initialize class member variables, similar in spirit to init method in our Student example
def __init__(self, pt, color, win):
# 'pt' is the coordinate for the Fish within the window
self.pt = pt
self.color = color
self.win = win
self.size = 40
# The 'direction' variable serves to point the Fish in the correct direction.
self.direction = "left"
############################################################################
# The draw method displays a Fish in the window at coordinate specified by 'self.pt'.
# The fish is defined by three parts, self.eye, self.body, self.fin.
# We also use 'self.direction' to flip the fish to point in the correct direction.
# Nothing to change in this method unless you want to change how the fish looks.
def draw(self):
# Flip the fish based on direction.
if self.direction == "right":
eye_offset = self.size/3
fin_offset1 = -self.size/3
fin_offset2 = -1.2*self.size
else:
eye_offset = -self.size/3
fin_offset1 = self.size/3
fin_offset2 = 1.2*self.size
# Setup the fin coordinates as a polygon and display it.
self.fin = Polygon([ Point(self.pt.getX()+fin_offset1, self.pt.getY()),
Point(self.pt.getX()+fin_offset2, self.pt.getY()+self.size),
Point(self.pt.getX()+fin_offset2, self.pt.getY()-self.size)
])
self.fin.setFill(self.color)
self.fin.draw(self.win)
# Setup the body as circle and display it.
self.body = Circle(self.pt, self.size)
self.body.setFill(self.color)
self.body.draw(self.win)
# Setup the eye as circle and display it.
self.eye = Circle(Point(self.pt.getX()+eye_offset,self.pt.getY()-self.size/3),self.size/8)
self.eye.setFill("black")
self.eye.draw(self.win)
############################################################################
# The undraw() method erases all parts of the Fish.
# TASK TO DO
# Write the prototype for undraw() method. This method needs to call undraw()
# for each part of the fish (i.e. for self.eye, self.fin and self.body).
############################################################################
# The setColor() method sets the color for various parts of the Fish.
def setColor(self, color):
#*** TASK TO DO ***
# Set the class member variable 'set.color' to the input 'color' value.
# Also set colors for the 'self.fin' and 'self.body' using their setColor() method.
return
############################################################################
# The setSize() method changes size of the Fish.
def setSize(self, size):
#*** TASK TO DO ***
# Set the class member variable 'set.size' to the input 'size' value.
# Then invoke 'undraw()' followed by 'draw()' on self to redisplay the fish.
return
############################################################################
# move method serves to move the fish in the window.
#*** TASK TO DO ***
# Define the prototype for move() method. The input arguments are displacement in x (dx) and displacement in y (dy).
# See the invocation of move() within main() to understand how it is used.
# Set self.direction to "left" or "right" based on whether 'dx' is positive or negative.
# Set self.pt to the new Point after moving current coordinates by (dx, dy).
# Hint: use self.pt.getX() to get current x coordinate.
# Then invoke 'undraw()' followed by 'draw()' on self to redisplay the fish.
return
############################################################################
def main():
## create our graphical window first
win = GraphWin("Fish", 800, 600)
# Create two instances of Fish.
s1 = Fish(Point(300, 400), "pink", win)
s1.draw()
s2 = Fish(Point(400, 200), "blue", win)
s2.draw()
s2.setSize(100)
# Now lets move the fish around.
for i in range(1, 10):
time.sleep(0.1)
s1.move(20, 0)
s2.move(-20, 0)
s1.setColor("green")
s2.setColor("yellow")
for i in range(1, 10):
time.sleep(0.1)
s2.move(20, 0)
s1.move(-20, 0)
win.close()
############################################################################
main()
|