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

[pygame] Sprite Code



I've been looking at my sprite code again and finding a different way than
I'd used for loading animation frames. That is, Pygame.sprite doesn't seem
to have any way to set up the notion of having multiple frames, and my old
method involved loading a single "sprite sheet" image and finding a set of
rect objects refering to sections of the sprite sheet.

What I'm doing lately involves looking in a graphics directory
(\graphics\sprites) for text files, loading some info from those, and using
them to define the rects to be used from a sprite sheet image. The input is
some simple text files and the images. The output is a dictionary that
looks like this (for a sheet called "guy" having 4 rows of a 3-frame
walking animation):
{"guy":
  {"frame_size":(96,128),"image":<surface 388x512>,
   "animations":{ "stand":[ (45,[(0,0,96,128),(96,0,96,128)]) [...] ]}
  }
}

...Well, something like that! Anyway it's listed by sprite, then by
animation name (like "stand"), then by some angle that's used to determine
which frames to use based on the character's facing direction, and finally
as a list of rects marking the individual animation frames. (An alternative
method might be to use the text files just to say things like "walk =
frames 0,1,2,3" and then automatically load frames by number only and trust
the program using this code to know how to use them.) With this code you
should be able to auto-load all sprite data on startup and then, for any
movement angle, look up which frames to cycle through.

Any interest in this code? What methods do you use to get your sheet
together?