[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] main game object using singleton/global/static?
- To: pygame-users@xxxxxxxx
- Subject: [pygame] main game object using singleton/global/static?
- From: Jake b <ninmonkeys@xxxxxxxxx>
- Date: Fri, 17 Jul 2009 18:47:39 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Fri, 17 Jul 2009 19:48:02 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:from:date:message-id :subject:to:content-type; bh=KtUkXu8YFNzjLdaeDyIVqFWK5XEBHaB7P9vFZ39+Rio=; b=WoBn3a6k+mebnT5WPEfIofWnehkm3U3Vtc9k2hRG1625B7TUp4wL+S/xe+fTVjs8ve B8FuNlqJrzcOsQpo/XSgjDQRpCT01lG8zw2dkOtyaungYvK2prHsdsmoB6ySXchcLqdz 6Fh82u066VTeCS07sltiSEbEY3b/j3bMFj+Ug=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=GlpOZMhRdUoFwaK85kCdj8P/tOy2JAxtxfBvcPlLTLeA0DyXbZtt8kNTEpxSwE132r 7n2CDAU3f4B+GgUJq6GQjRuSTTj3qBYXL+YS7OJtidqRU/w5LZaOkKPAWiJYAV0o56jN KvieTTbODsvvLxOq2h/2geKFDiWyI3DZSeCyE=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
Summery:
How do I organize my data structures to read or pass my Game() object in ship.py ?
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