arrested (gaming) development: flap your wings or we’re going to die!
please note in order to see the code and the videos, you’ll need to go to the full post. feedburner doesn’t appear to play nice with my embedded videos.
whilst moving sprites is relatively easy to do, it would appear that animating them is trickier to do in pygame; there are no builtin animation routines. just to clarify what i’m looking for, if you give game maker an animated gif it will cycle through images. it’s small graces like this that make me appreciate how easier it is make games in that environment.
a quick search on “teh interwebs” revealed that i was not alone in wishing there was native support for animated gifs. i was even willing to switch to png format (the only other image format that support rudimentry animation) if it had pygame support! i came across someone who was toying around with the idea of using python imaging library (pil) to explode an animated gif into individual pygame image objects. i was starting to head towards this approach until i saw that [pil] wasn’t platform neutral and wasn’t nearly as easy to install as its stablemate pygame.
in the end i decided to go for the manual process of extracting the individual frame from the animated gifs. after a bit more research, i discovered this bash script that uses imagemagick to extract the individual frames and create new images based on them.
i revised the the sprite class’s render method to cycle through the frames by using this code written by jaime moreno. i like this code, because it doesn’t lock up the main loop whilst trying to animate the sprite.
def render(self): # A nice bit of animation code (cowMooDelay.py) written by Jaime Moreno self.pause += 1 if self.pause >= self.delay: self.pause = 0 self.frame += 1 if self.frame >= len(self.frames): self.frame = 0 self.bitmap = self.frames[self.frame] # own little bit to transparentimatizeeach frame based on # the colour of the bottom left pixel self.colorkey = self.bitmap.get_at((0,self.bitmap.get_height()-1)) self.bitmap.set_colorkey(self.colorkey) screen.blit(self.bitmap, (self.x, self.y))
next up is the sprite class method to preload the multiple frames, i modified the code to allow me to specify the number of frames in an animated sprite (during object initialisation). i also amended the file mask to match my gif splitting tool’s naming convention.
def loadFrames(self, numFrames):
...
self.frames = []
...
for i in range(1,numFrames):
imgName = os.path.join("data",self.imgPrefix + "-10%d.gif" % i)
tmpImage = image.load(imgName)
self.frames.append(tmpImage.convert())
so as an example of initialising an animated gif i’ve used the dragon which has 6 frames of animation. the gif splitting turns this into Dragon-101.gif, Dragon-102.gif, … Dragon-106.gif. the object initialisation code would look like this:
dragon = Sprite(20, 200, 'Dragon',6)
finally i wrote a bit of code in the sprite class to avoid having to create a new class for sprites that are not animated. it’s a bit of a bodge, but the logic goes like this, during object initialisation if i specify the number of frames as 1 i.e.
fireball = Sprite(0,480, 'Fireball.gif', 1)
this triggers the loadImages() method to use the image prefix as the actual file name to load.
def loadFrames(self, numFrames):
...
if numFrames == 1:
imgName = os.path.join("data", self.imgPrefix)
self.frames.append(image.load(imgName))
...
so how does my pygame port of evil clutches compare? here’s the game maker reference video.
and here’s the pygame port
as you can see, it’s slightly jittery, i gotta admit i’m stumped as to cause of this. for now its usable, i’ll post an update if i work out cause. if anyone knows why, please post a comment. so tune in next week when we have to take cover as the bullets start to fly!
prev (sprites) | about | next (bullets)
About this entry
You’re currently reading “arrested (gaming) development: flap your wings or we’re going to die!,” an entry on archived blog – booyaa dot org
- Published:
- 12.1.08 / 1pm
- Category:
- Tags:
- development, gamemaker, games, pygame, python
Technorati
Flickr
del.icio.us
Ice Rocket
Wikipedia
Comments are closed
Comments are currently closed on this entry.