Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Playing multiple animations at the same time in pygame

I have an Animation function that increments the animation counter over time according to the parameters it receives.
When I use the function for a single object it works fine, but when I try to apply it to the second one the first one works and the second one doesn’t.
Direct;

torchACount+=1
if torchACount>3:
    torchACount=0

I could do it by fps by typing inside the main loop but I want to do it by time. I think the error I get in the function I wrote is due to the time element, but I can’t find how to solve it. Do I need to use a class?

My exaple code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pygame
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((480, 480))
clock=pygame.time.Clock()
time=pygame.time.get_ticks()

torchAImageList=[pygame.transform.scale(pygame.image.load(f"torchA{i+1}.png"),(48,96)) for i in range(4)]
torchACount=0
torchADelay=100

torchBImageList=[pygame.transform.scale(pygame.image.load(f"torchB{i+1}.png"),(48,96)) for i in range(4)]
torchBCount=0
torchBDelay=100


def animation(delay,animationNumber,limitOfAnimation):
    global time
    if pygame.time.get_ticks()-time>delay:
        animationNumber+=1
        if animationNumber==limitOfAnimation:
            animationNumber=0
        time=pygame.time.get_ticks()
    return animationNumber          
    
    
while True:
    for ev in pygame.event.get():
        if ev.type == QUIT:
            pygame.quit()
            
    clock.tick(60)
    screen.fill((0, 0, 0))
    
    screen.blit(torchAImageList[torchACount],(100,100))
    torchACount=animation(torchADelay,torchACount,4)
    
    screen.blit(torchBImageList[torchBCount],(300,100))
    torchBCount=animation(torchBDelay,torchBCount,4)
    
    pygame.display.flip()

Images:
a1
a2
a3
a|
b1
b2
b3
b4

>Solution :

You need a separate time variable for each object. I suggest to use a class and to store the time in an attribute:

import pygame
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((480, 480))
clock=pygame.time.Clock()
time=pygame.time.get_ticks()

class Tourch:
    def __init__(self, x, y, imageList):
        self.x = x
        self.y = y
        self.imageList = imageList
        self.count = 0
        self.delay = 100
        self.time = 0

    def animate(self):
        currentTime =  pygame.time.get_ticks()
        if currentTime - self.time > self.delay:
            self.time = currentTime
            self.count += 1
            if self.count >= len(self.imageList):
                self.count = 0

    def draw(self, targetSurf):
        targetSurf.blit(self.imageList[self.count], (self.x, self.y))

tourchA = Tourch(100, 100, [pygame.transform.scale(pygame.image.load(f"torchA{i+1}.png"),(48,96)) for i in range(4)])
tourchB = Tourch(300, 100, [pygame.transform.scale(pygame.image.load(f"torchB{i+1}.png"),(48,96)) for i in range(4)])

run = True
while run:
    for ev in pygame.event.get():
        if ev.type == QUIT:
            run = False

    tourchA.animate()
    tourchB.animate()
            
    clock.tick(60)
    screen.fill((0, 0, 0))
    tourchA.draw(screen)
    tourchB.draw(screen)
    pygame.display.flip()

pygame.quit()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading