Building a game framework

The Component and GameObject classes form the core of our game engine. The interface offered by the Component class is as easy as this:

class Component(object):
    __slots__ = ['gameobject']

    def start(self):
        pass
    def update(self, dt):
        pass
    def stop(self):
        pass
    def on_collide(self, other, contacts):
        pass

These methods define the life cycle of our components:

  • start(): This is called when the component is added to the game object instance. The component keeps a reference to the game object as self.gameobject.
  • update(dt): This is called for each frame, where the dt argument is the elapsed time in seconds since the previous frame.
  • on_collide(other, contacts): This is called when the component's game object collides with another rigid ...

Get Python Game Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.