Because python modules are just like classes, I tend to avoid the
singleton pattern in python and simply use module-level variables. For
example, in my game I have:
---globals.py---
import pygame
pygame.display.init()
screen = pygame.display.set_size(...)
# a handful other commonly used variables, such as font, background, etc
---game.py---
from globals import screen, ...
I don't know if this pattern is encouraged, but I've found it a good way
to organize my game.
--Mike
Jake b wrote:
Summery:
How do I organize my data structures to read or pass my Game() object
in ship.py ?
Here is Pseudo code of what i'm talking about:
http://pastebin.com/m985fe2f
Long:
How do you use your 'game' class? I created one class that handles
screen creation, game main loop, list of Sprites, etc... But I'm not
sure on how I should access the game members.
In Ship.draw() I have a couple of potential examples, where the one
i'm using right now is:
self.screen.blit(self.image)
(It saves a reference to Game.screen)
But, this requires me to create my objects inside of Game(), passing a
reference to the game instance every time a sprite is created, like so:
# def Game.spawn():
self.sprites.add( Ship( self ) )
This seems wrong/hackish to me. Is there a way I can have a (semi?)
global function / method? Or should I do this?
I didn't want to make 'screen' a global variable, because I want
access to Game(), since there are multiple members/methods I need
access to.
Another example: of a function that needs access to game members: This
is called in ship.py when the sprite is spawned.
# def Game.randLoc(self):
"""returns random location that exists in boundries of current map"""
return randint( 0, self.map.width ), randint( 0, self.map.height)
--
Jake