[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] global class instances?



pygame.fps = FPS()

import pygame
print pygame.fps

It's somewhat hacky but it works, because the import statement uses
the already imported module if it exists.

I tend to have one entry point that I pass to all constructors, as in
the displaymanager example.  Sometimes I'll make it a part of the
class instead:

class DisplayManager:
    def test(self):
        print "I am the root object"

class ObjectInstance:
    def test(self):
        self.root.test()

#initialization code at end of main.py
root = DisplayManager()
ObjectInstance.root = root

a = ObjectInstance()
a.test()

On Nov 24, 2007 12:48 PM, Ghislain Leveque <ghislain.leveque@xxxxxxxxx> wrote:
> 2007/11/24, Jake b <ninmonkeys@xxxxxxxxx>:
> > (2) I'm going to be creating a sprite group: "bullet_sprites". Do you
> > think my sprite groups should be global? Because I need to spawn
> > bullets from within Player().
>
> I don't know for your other question but may be really interested by
> the answers.
>
> For this point, here is what I do :
> - have a class named "DisplayManager" and instantiate it
> - this instance sould be global or passed to every object in the
> constructor (as you do now)
> - this class has a method "create_bullet" that creates the bullet with
> the right sprite group. This will also be the class that handle the
> "draw" method and holds the sprites group, the pygame.display...
>
> hope that helps
>
> --
> Ghislain Lévêque
>