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

Re: [pygame] kind of animation



On Wed, 2006-05-24 at 16:50 +0200, DR0ID wrote:
> If I understand you right you suggest to make the objects and functions 
> time dependent. What do you mean with "you end up with a consistent game"?

I mean it plays the same everywhere. If you go with frame-based
animation you usually set some sort of maximum frame rate, and make the
minimum requirement a computer that can play it at the fastest speed.
But even with that a computer may miss a frame occasionally which will
look like a pause in the animation. With the time based movement it will
look smoother, even if a frame is missed.

> As said, I was more interested how to update the image of an animation. 
> But this leads to a other question which I will ask in an other email.

Solarwolf uses this extensively. All the animations are stored as image
strips. There is a function to load this into a list of images. The
objects can then move through these images with time based animations.
Actually in Solarwolf some of the objects animate forwards or backwards.
Here is the bulk of the "spike" class in Solarwolf, the spinning spikes.

You can see it picks an initial frame and an animation speed. Each time
the tick method is called it is passed an amount of time as the
'speedadjust' argument.

from pygame.locals import *
import game, gfx, snd

spikeimages = []


def load_game_resources():
    global spikeimages
    spikes = gfx.load_raw('spikeball.png')
    spikeimages = gfx.animstrip(spikes)


class Spike:
    blockrocks = 0
    def __init__(self, pos):
        self.rotate = random.random() * 90.0
        self.rotspeed = random.random() - 0.5
        if random.randint(0, 1):
            self.rotspeed = -self.rotspeed
        self.images = spikeimages
        self.numframes = len(self.images[0])
        self.rect = self.images[0][0].get_rect().move(pos)

    def erase(self, background):
        background(self.rect)

    def draw(self, gfx):
        frame = int(self.rotate)%self.numframes
        img = self.images[frame]
        r = gfx.surface.blit(img, self.rect)
        gfx.dirty(r)

    def tick(self, speedadjust):
        self.rotate += self.rotspeed * speedadjust * .2