[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] global class instances?
- To: pygame-users@xxxxxxxx
- Subject: [pygame] global class instances?
- From: "Jake b" <ninmonkeys@xxxxxxxxx>
- Date: Sat, 24 Nov 2007 14:33:39 -0600
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 24 Nov 2007 15:33:48 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; bh=VOVeNpXWEsj/3PBzOR9Y8w63gaVgmVkduThf1DIawM0=; b=pTPYIQEe1ttppqnmmgeUiy6M06fB60eJaxPWNnc90cTuQuVKoMzyKZVjbAfnPySiwJRSajdgtmlrL9T/o9aFXnY73kht64X+FIWFADDLrTN3qCIxx9dd8Kw61jJWj+xDfrPE+GW58XwY6l+VYKUuwmqNwhsJQqSlPpM/9NgI81I=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=RMUsnlD6wHgcv9wtQETpbZG83K6IGuhn9izzdUxsqm68y1pc7y5dg+B4tnFH8cgzwSOGbxP3wnYZptKpajnG7HFhNXCYKvBu7YKmUw5b7EH9k3bqAgZwSE8BBqPGHIwlRfkhLZ0ihCMF9v3wV4LI6IIvfLuNCs9iYI9G5TErDws=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
I'm starting a basic game. I have a few classes. I'm having trouble
figuring out how to handle some global class instances.
Here's my current classes.
FPS() = handles fps rendering, and calc's delta for time-based movement
Player() = Player's class
Console() = buffer that I can pass strings to, and it shows them on screen.
Map() = stores map tile info, and renders it.
Mouse() = some basic mouse helper functions, mouse location, etc...
ZombieHuntMain() = The main entry point, main loop, etc.
I want a few class instances to be global so that I don't have to pass
a pointer in every constructor that needs them. ( Like I am doing
right now in the snippet )
Right now I my game initialization looks like:
# ZombieHunt.py : snippet of main logic class
class ZombieHuntMain:
"""entry point. initialization, and game logic."""
def __init__(self, width=1024, height=768):
"""Initialize PyGame"""
pygame.init()
self.width, self.height = width, height
self.screen = pygame.display.set_mode(( self.width, self.height ))
# some of these should be accessible to any file that includes "Globals.py"
self.console = Console()
self.fps = FPS( self.console )
self.map = Map(self.console)
self.mouse = Mouse(self.console, self.map)
self.player = Player(self.console, self.map, self.mouse, self.fps)
# add player to the sprite group
self.unit_sprites = RenderPlain()
self.unit_sprites.add( self.player )
So you can see that the constructor arguments are getting crazy. Most
will want access to the Console() instance. And probably Map(). Maybe
others.
I tried just moving the constructors to Globals.py, but then pygame
doesn't initialize in time for Console().__init__() to work. ( Fails
because pygame fonts don't load in time ) I even tried "pygame.init()"
before the line "from Globals import *", but It still seemed to not
work. (And that seemed like a bad way to fix it )
I also tried creating the instances like normal ( same as in the above
code ) as members of ZombieHuntMain, then in Globals.py create a
reference to that instance:
# grab pointer / reference of instance in ZombieHunt
console = ZombieHuntMain.console
But I couldn't get that to work, even with "from ZombieHunt import *"
( I don't know if ZombieHunt.py being the entry point has anything to
do with that? If something else happens if you attempt to import the
entry point ? )
(1) How would you organize the classes?
(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().
thanks,
--
Jake