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

Re: [pygame] duplicate images



You actually can still share the same image between sprites:

image = pygame.image.load("image.py")
s1 = Sprite()
s1.image = image
s2 = Sprite()
s2.image = image

They have the same image but can be placed in different groups, or put
at different places.

On Fri, Oct 9, 2009 at 9:02 AM, Thiago Petruccelli
<thiagopetruccelli@xxxxxxxxx> wrote:
> Thanks everybody! Problem solved.
>
> I couldn't just blit the same img in different objects because they're
> contained in sprite.Groups (OrderedUpdates). Every image in my game is in a
> class that extends Sprite. But the copy command of the image solved the
> problem.
>
> thanks everybody =)
> --
> Thiago Henrique Petruccelli
>
>
> On Fri, Oct 9, 2009 at 3:09 AM, Jake b <ninmonkeys@xxxxxxxxx> wrote:
>>
>> How are you handling the windows. ( Do you have a class that stores its
>> location? )
>> It sounds like what happens if two variables point to / and modify the
>> same object.
>> [ Whereas you want the same image, but different location ]
>>
>> You can create a new sprite object, for every unit. But you are able to
>> load it's image one time, shared among 100 (or more) units on screen.
>>
>> Or you can skip using SpriteGroup()'s entirely, by just blitting the
>> surface at a location. ( like RB[0] said ).
>>
>> Here are a couple tutorials that may interest you:
>>
>> pinman's sprite group tutorial:
>> http://www.sacredchao.net/~piman/writing/sprite-tutorial.shtml
>>
>> pygame.org's  sprite group tutorial:
>> http://www.pygame.org/docs/tut/SpriteIntro.html
>>
>> and pygame.org's Sprite documentation:
>> http://www.pygame.org/docs/ref/sprite.html
>>
>> example shows how you draw multiple sprites with one image.
>>
>> """Psuedo-code example 1"""
>> close_img = pygame.image.load("close.png")
>> windows = [ Window(), Window() ] # two windows, wiih defaults
>>
>> # for every window
>> for current in windows:
>>     x,y = current.location()
>>     screen.blit( close_img, (x,y) )
>>
>>
>> # ======================
>>
>>
>> This second example is taken from piman's tutorial: If you want to load
>> the image the first time the class is instantiated, and every instance uses
>> the first image, you can do this.
>>
>> "real code: example 2"
>> class MySprite(pygame.sprite.Sprite):
>>     image = None
>>
>>     def __init__(self):
>>         pygame.sprite.Sprite.__init__(self)
>>
>>         if MySprite.image is None:
>>             # This is the first time this class has been instantiated.
>>             # So, load the image for this and all subsequence instances.
>>             MySprite.image = pygame.image.load("image.png")
>>
>>         self.image = MySprite.image
>>
>>
>> # ======================
>>
>> # Then you could have code like this:
>>
>> # ======================
>>
>> # load map
>> units = []
>> units.append( Wolf() )
>> units.append( Troll() )
>>
>> # draw
>> for unit in self.units:
>>     unit.move()
>>     unit.draw()
>>
>> # your loading and drawing code is now defined inside class Wolf() and
>> class Troll().
>> # You could even write a wrapper, so that you can do something like this:
>>
>> class Unit(pygame.sprite.Sprite)
>>     # base class.
>>     # ... like the above MySprite class, but adds a filename as argument.
>>     # also can handles locations, movement, and .draw() that all units
>> need
>>     def __init__(self, filename ):
>>         # ... code snipped, saves to variable class.
>>
>> # Wolf uses default blit, movement, attack, etc... functions
>> # only needs to define image.
>> class Wolf(Unit):
>>     def __init__(self):
>>         Unit.__init__(self, "wolf.png")
>>
>> # troll draws extra ontop of default
>> class Troll(Unit):
>>     def __init__(self):
>>         Unit.__init__(self, "troll.png")
>>     def draw(self):
>>         #overwrides default blit, here.
>>
>> see also: cookbook: sprite sheet.
>> http://www.pygame.org/wiki/Spritesheet?parent=CookBook
>>
>> hope that helps :P
>> --
>> Jake
>
>