ObjectivesDrawable ObjectsAnimationCollision detection24/9/2020CS172 - Drexel University
Drawable ObjectsIn real graphics applications, there tend to be many things drawn.Each of these may be a kind of particular type of drawable object.And each type of thing must have a way for it to be drawn.Sounds like a perfect application of polymorphism, abstract base classes, and inheritance!4/9/2020CS172 - Drexel University3
Drawable ObjectsAt the very least, all of our objects should have a locationand a method to draw onto a surface.So let’s start off with an abstract base class that has a location for its object and requires others who derive from it to implement a draw methodWe may want to have more methods in our public interface44/9/2020CS172 - Drexel University
Drawable Objectsimport abcclass Drawable(metaclass = abc.ABCMeta):def __init__(self, x = 0, y = 0):self.__x = xself.__y = ydef getLocation(self):return (self.__x, self.__y)def setLocation(self, point):self.__x = pointself.__y = email@example.com draw(self,surface):pass54/9/2020CS172 - Drexel University
Drawable Derived ObjectNow let’s create a class that: derives from Drawableis used to store (and render) a houseobject (as a rectangle and triangle) at a specified location and in a specified color.64/9/2020CS172 - Drexel University
Drawable Derived Objectclass House(Drawable):def __init__(self, x = 0, y = 0, color = (0,0,0)):Drawable.__init__(self, x, y)self.__color = colordef draw(self, surface):location = self.getLocation()pygame.draw.rect(surface, self.__color, [location-15, location-10, 30, 20])pygame.draw.polygon(surface, self.__color, [(location-15, location-10), \(location+15, location-10), (location, location-20)])74/9/2020CS172 - Drexel University
Main ScriptIn our main program we can have a list of Drawableobjects, appending new objects as needed.