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

Re: [pygame] time depending animation code



DR0ID schrieb:
Peter Shinners schrieb:
On Sun, 2006-05-28 at 17:24 +0200, DR0ID wrote:
I'm trying to make a time dependent animation, but I'm not sure if I'm doint this right. Please take a look:

This looks pretty close. The code you have for managing the looping seems complex. It also looks like it won't be correct if multiple frames have passed when it loops around.

------------------------------------------------------------------------

def _update(self, dt, *args):
"""update frame on time difference, dt"""
self._delay += dt
numFrames = self._endFrame - self._startFrame
dframes = int(self._delay / self._interval)
self._idx = (dframes % numFrames) + self._startFrame
# self.image = MyClass.images[self._idx]



Hi

well your right, my loop around code is not correct.

This should work the same way as you proposed (or not?):



def _update(self, dt, *args):
    """update frame on time difference, dt"""

    self._delay += dt
    if self._delay>self._interval
        numFrames = self._endFrame - self._startFrame
        dframes = int(self._delay / self._interval) + self._idx
        self._idx = (dframes % numFrames) + self._startFrame
        self._delay %= self._interval
        # self.image = MyClass.images[self._idx]


It has not to make all calculations as long the self._delay is not bigger than the self._interval.



~DR0ID

Hello again

found an easier way to do it:


#------------------------------------------------------------------------------
def _update0(self, dt, *args): # forward nopingpong loop
"""
Update frame on time difference dt.
Forward looping cyle.
"""
if self._playing:
self._delay += dt # accumulate the elapsed time since last image change
if self._delay > self._interval: # only do things if needed
self._idx += int(self._delay/self._interval) # add number of frames in delay
self._idx %= self._numFrames # get frame index
self._delay %= self._interval # time from next interval that has already past by
self.image = self._images[self._idx + self._startFrame] # translation to right place in images list
# self.image = self._images[self._endFrame - self._idx] # for reverse looping (if no translation then self._endFrame=self._numFrames)
#------------------------------------------------------------------------------


The meaning of self._idx changed a bit: it is the index from 0 to self._numFrames. The translation to self._startFrame is done when getting the image. Now the loop around and skipping images should work now and I hope in the correct way.

I'm not sure if the self._startFrame and self._endFrame make any sense (one could put more then one animation into this with different start and end points but I want to implement a multianimation anyway).


I have another question: if I save the images in a instance variable then all instances will operate on the same images right? But then I have to inherit and make a new class for any different animation I want, right?


Example:

class myAnimationClass: #base class for animation, all instances operate on the same images
images = []
def __init__(self):
# whatever


class myUfos(myClass): # ufo animation, all instances operate on the same images
pass


class myBullets(myClass): # bullet animation, all instance operate on the same images
pass



On the other hand if I use an resource manager then its not important that the images where saved in a instance variable (so every instance hold his images list) because the resource manager does the job of the instance variable, right? So I could use the same Animation class for different animations (ok it will use more memory because each instance has to hold an image reference list instead of one for all instances).


So probaly the first one is the better choice because then I have different named classes for each animation and for any number of instances there is only one images list.

~DR0ID