I am working on some code to animate objects.. at the moment I have something like this (pseudo code)..
class Sprite:
x = 0
y = 0
def animate(fps=10):
x += blah
y += blah
time.sleep(1000 / fps)
self.animate()
mario = new Sprite()
mario.animate(fps=10)
bird = new Sprite()
bird.animate(fps=5)
As you can see, for each sprite created it has its animate function which changes it’s x and y position, and the fps calculation is simple. What I’d like to achieve is something like this:
instances = []
fps = 30
def animate():
for each object in instances:
# NEED AN 'IF TIME TO ANIMATE?' PART HERE.
object.x = blah
object.y = blah
time.sleep(1 / fps)
animate()
class Sprite:
x = 0
y = 0
def animate(fps = 5):
instances.append(self)
mario = new Sprite()
mario.animate(fps=10)
bird = new Sprite()
bird.animate(fps=5)
The difference between this and the first piece of pseudo code is that the second piece has a single animation call to animate each object. I’m just unsure of how you would work out exactly when to animate each object. The only way this would work (currently) is if each object/sprite had the same FPS.
Add an attribute to your
Spriteclass to hold the time at which it was last animated. Then you can easily calculate if it’s time to change frame.