Jed Rembold
November 4, 2022
The code block to the right starts defining a class. Only 1 of the
below options for defining an increment
method will work. Which one?
class BestCounter:
def __init__(self, start):
self.counter = start
def increment(self, value):
counter += value
def increment(value):
counter += self.value
def increment(self, value):
self.counter += self.value
def increment(self, value):
self.counter += value
Subclass definitions in Python differ from standalone classes in two ways:
Together, they form the following pattern:
class subclass(superclass):
def __init__(self, parameters):
superclass.__init__(self, any_relevant_parameters)
# Rest of subclass initializationclass Human:
def __init__(self, name, age):
self.name = name
self.age = age
self.speed = 10
self.perception = 10
def __repr__(self):
return f'Human({self.name},{self.age})'
def agree(self):
print('Sure!')
class Pirate(Human):
def agree(self):
print("Aye matey!")
def __repr__(self):
return f'Pirate({self.name},{self.age})'
class Pegleg(Pirate):
def __init__(self, name, age):
Pirate.__init__(self, name, age)
self.speed = 5
def __repr__(self):
return f'Pegleg({self.name},{self.age})'
class Patch(Pirate):
def __init__(self, age):
Pirate.__init__(self, name, age)
self.perception = 5
def __repr__(self):
return f'Patch({self.name}, {self.age})'
Pizza class with various subclasses of
pizza, like Pepperoni or Hawaiian.GObject hierarchy make very enticing targets
as the bases for potential extension:
GPolygon class makes it easy to
define new subclasses that have a polygonal outline.
GPolygon is a subclass of
GFillableObjectGCompound class can serve as a great
parent for new subclasses that are composed of a combination of other
shapes.GCompound class as a parent
class makes it possible to apply the strategy of decomposition to
graphical objects!
GCompound
GCompound is also a
GObject, you can go ahead and add a
GCompound to another
GCompoundSnake, which is a subclass of
GCompoundfrom pgl import GWindow, GCompound, GOval, GLine, GPolygon
R = 50
class Segment(GCompound):
""" Create as generic snake body segment. """
def __init__(self):
GCompound.__init__(self)
body = GOval(-R, -R,
2*R, 2*R)
body.set_filled(True)
body.set_fill_color('green')
body.set_line_width(5)
self.add(body)
class Tail(Segment):
def __init__(self):
Segment.__init__(self)
tri = GPolygon()
tri.add_vertex(0, R)
tri.add_vertex(0, -R)
tri.add_vertex(-3*R, 0)
tri.set_filled(True)
tri.set_fill_color('green')
tri.set_line_width(5)
self.add(tri)
tri.send_to_back()
class Eye(GCompound):
def __init__(self):
GCompound.__init__(self)
eye = GOval(-10,-5,20,10)
eye.set_filled(True)
eye.set_color("white")
pupil = GOval(-5,-5,10,10)
pupil.set_filled(True)
self.add(eye,-10,-5)
self.add(pupil, 0,-5)
class Head(Segment):
def __init__(self):
Segment.__init__(self)
self.add(Eye(), 10, 20)
self.add(Eye(), 10, -20)
tongue = GLine(R, 0, 2*R, 0)
tongue.set_line_width(5)
tongue.set_color('red')
self.add(tongue)
class Snake(GCompound):
""" Put all the snake pieces together. """
def __init__(self, length):
GCompound.__init__(self)
self.add(Tail(), 0, 0)
for i in range(1,length-1):
self.add(Segment(), i*R, 0)
self.add(Head(), (length-1)*R, 0)
def make_snake():
def move_snake():
snake.move(10,0)
gw = GWindow(800,400)
snake = Snake(3)
gw.add(snake, -500,200)
gw.set_interval(move_snake, 30)
make_snake()