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

Re: [pygame] help with subsurfaces



Patrick Mullen wrote:
On Dec 12, 2007 1:10 PM, Dan Krol <orblivion@xxxxxxxxx> wrote:
I need to do something similar, but I think I did it by just blitting
the subsection of the original surface I needed when it's time to draw
(and no other times). Doesn't this not use extra memory as well?  Any
reason this is a bad idea?

screen.blit(source,[30,60],[30,60,100,15]) this probably is as fast
and as memory concious, but to me is even more confusing because the
area defined belongs to the source but comes AFTER the coordinates.  I
would forget what that rect is associated with.

What I had been doing was similar to this way. I loaded one big image containing all animation frames for a character, then defined a bunch of Rects marking each animation frame, taking into account that a source image might have "padding" around the edges or between frames. The key part of code is below. The full code is in the "nixie" module that should be in the "Shining Sea Source" ZIP at <kschnee.xepher.net/code>.

<code>
        """Define a set of rectangles (Rects) representing chunks of the
        sprite sheet for different animation frames. Once this is done,
you can say "switch to frame 4" and know that it's, say, "jump." """
        self.animation_frames = []
        for row in range(self.frames_how_many_rows):
y1 = (row * self.framesize[1]) + (row * self.frames_padding) + self.frames_outer_padding
            height = self.framesize[1]
            for frame in range(self.frames_per_row):
x1 = (frame * self.framesize[0]) + (frame * self.frames_padding) + self.frames_outer_padding
                width = self.framesize[0]
                ## Drum_Roll()
self.animation_frames.append( pygame.rect.Rect(x1,y1,width,height) )
        self.highest_animation_frame = len(self.animation_frames) - 1
</code>