O'Reilly logo

Python Game Programming By Example by Joseph Howse, Alejandro Rodas de Paz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required