Book Image

Python Game Programming By Example

Book Image

Python Game Programming By Example

Overview of this book

Table of Contents (14 chapters)
Python Game Programming By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 body. The other argument is the other game object of the collision pair, and contacts includes the contact points between the objects. This last argument is directly passed from the Pymunk...