Re: [pygame] main game object using singleton/global/static?

2009-07-22 Thread Daniel Jo
There's not alot you can do about 'self' in general, but if you reference a
particular attribute or function many times you can avoid self by creating a
local reference to the attribute or function.  In your example could replace
the last two lines with:

game = self.game
game.playsound ("boom")
game.spawn ("item")

Of course, this would be a much more useful optimisation in cases with many
more references to 'self.game' within a single function or in cases where
'self.game' appears in a loop.

What it really sounds like you are looking for is something like Pascal's
'with' statement, which basically treats a record or object's attributes as
locals.  Python's 'with' statement has an entirely different function and
purpose, of course. . .

On Wed, Jul 22, 2009 at 4:01 PM, Jake b  wrote:

> Is there a shortcut or easier way than what I am doing? Because having to
> use the same reference in every object seems redundant?
> I am using a
>
> I feel I am spamming "self." A lot of my classes have a lot of "self."
> lines Example of random code:
>
> class Unit():
>  def onCollide(self, other):
>   if self.modifiedHP() - other.damage() < 0:
>   self.die()
>   self.game.playsound("boom")
>   self.game.spawn("item")
>
> So I'm looking for advice on if this is normal? Or bad code?
> --
> Jake
>



-- 
--Ostsol

http://cheesesun.blogspot.com/


Re: [pygame] main game object using singleton/global/static?

2009-07-22 Thread Jake b
Is there a shortcut or easier way than what I am doing? Because having to
use the same reference in every object seems redundant?
I am using a

I feel I am spamming "self." A lot of my classes have a lot of "self." lines
Example of random code:

class Unit():
 def onCollide(self, other):
  if self.modifiedHP() - other.damage() < 0:
  self.die()
  self.game.playsound("boom")
  self.game.spawn("item")

So I'm looking for advice on if this is normal? Or bad code?
-- 
Jake


Re: [pygame] main game object using singleton/global/static?

2009-07-18 Thread Michael George

Daniel Jo wrote:
Either singletons or globals would work fine.  Singletons are more 
useful when you see a need to enforce a rule that the class must be 
instanced only once, but if you are the only developer and can trust 
yourself not to do something silly, globals work just as well.  I tend 
to use singletons because I avoid globals as a matter of principle 
(except in prototype programs).


In python, there's no difference because there's no real notion of 
"private".  In fact the module system provides a nice way of ensuring 
that things are only instantiated once - initialize them right in the 
module.  The import statement will only run the initialization the first 
time that way.


In the end I think it's a matter of preference.

--Mike


Re: [pygame] main game object using singleton/global/static?

2009-07-18 Thread Daniel Jo
Either singletons or globals would work fine.  Singletons are more useful
when you see a need to enforce a rule that the class must be instanced only
once, but if you are the only developer and can trust yourself not to do
something silly, globals work just as well.  I tend to use singletons
because I avoid globals as a matter of principle (except in prototype
programs).

On Fri, Jul 17, 2009 at 5:47 PM, 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
>



-- 
--Ostsol

http://cheesesun.blogspot.com/


Re: [pygame] main game object using singleton/global/static?

2009-07-17 Thread Michael George
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