Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Game Programming By Example

You're reading from   Python Game Programming By Example A pragmatic guide for developing your own games with Python

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785281532
Length 230 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime