[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Sprite engine



> Why don't pygame has a sprite engine? it is not a silly question, I
> just think that maybe it would be a good thing to have a nice sprite
> engine to make things easier. After all wasn't pygame made to make
> game creation easier?

it turns out writing a generic sprite class isn't all that easy.
the way your sprite stores its data depends 100% on how your sprite
is going to be used.

for example, if you game is going to animate at a locked fps
rate, you'll want to use a different positioning system than
if your game animates using timeslice intervals.

if your game is maintaining "dirty" rects, it will need to render
very differently than if you are just doing fullscreen drawing and
just need to draw the sprite.

there's many other scenarios that make a generic sprite class
pretty much impossible to create. the way the sprite data is
stored just ends up being too dependent on the way the game
works.

the good news is that writing a sprite class in python is
trivial. it shouldn't take longer than 5-10 minutes to get something
really nice up and running, plus it will be hand-crafted for your
game, so no need to worry about strange implementations.

if you just want a basic sprite class, look in the Aliens example.
when writing solarwolf i didn't even create a sprite baseclass.
all the sprites in solarwolf are just their own class, no inheritence.
it's so easy to do, it's not even needed. for example here's the
sprite object for solarwolf's "bullet" objects. 

note that this sprite is just about as complex as it can
get. it keeps track of dirty rectangle updates and moves
on timeslices instead of one tick per frame. i guess if it
had any animation it would be a little trickier :]



class Shot:
    def __init__(self, pos, move):
        self.move = move
        self.image = images[0]
        self.rect = self.image.get_rect()
        self.rect.center = pos
        self.lastrect = None
        self.dead = 0
        self.pos = list(self.rect.topleft)

    def erase(self, background):
        if self.lastrect:
            background(self.lastrect)
            if self.dead:
                gfx.dirty(self.lastrect)

    def draw(self, gfx):
        r = gfx.surface.blit(self.image, self.rect)            
        gfx.dirty2(r, self.lastrect)
        self.lastrect = r

    def tick(self, speedadjust = 1.0):
        self.pos[0] += self.move[0] * speedadjust
        self.pos[1] += self.move[1] * speedadjust
        self.rect.topleft = self.pos
        if not gfx.rect.colliderect(self.rect):
            self.dead = 1
        return self.dead
            


i have been thinking about writing a tutorial on creating a
sprite class for your game. it would go through some of the
different methods of managing your game, and what type of
things to put into a sprite to make it easier.


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org