Question

I am coding a game using Kivy. I have a Screen class where I put my animation code. It's not a usual game, it's more like several screens, each with its own animation, with button commands for going back and forth to different screens.

It works ok, but when I make more classes like this and put it all in a ScreenManager, the animation is disrupted with random white screens.

class Pas(Screen):
def __init__(self, **kwargs):   
    super(Pas, self).__init__(**kwargs)
    Clock.schedule_interval(self.update, 1 / 60.0)

    self.ani_speed_init = 15
    self.ani_speed = self.ani_speed_init
    self.ani = glob.glob("img/pas_ani*.png")
    self.ani.sort() 
    self.ani_pos = 0
    self.ani_max = len(self.ani)-1
    self.img = self.ani[0]  
    self.update(1)

    back = Button(
            background_normal=('img/back-icon.png'),
            background_down=('img/back-icon.png'),
            pos=(380, 420)) 
    self.add_widget(back)
    def callback(instance):
        sm.current = 'game'
    back.bind(on_press=callback)

def update(self, dt):
    self.ani_speed -= 1
    if self.ani_speed == 0:
        self.img = self.ani[self.ani_pos]
        self.ani_speed = self.ani_speed_init
        if self.ani_pos == self.ani_max:
            self.ani_pos = 0
        else:
            self.ani_pos += 1
    with self.canvas:
        image = Image(source=self.img, pos=(0, 0), size=(320, 480))

What am I doing wrong? I am also accepting ideas for a different way of doing this.

Was it helpful?

Solution

If you want to use Screen and ScreenManager for your screens, it would be better to use the transition system they define and use, so, to define your own Transitions, and apply them. If you want more control, i would advise getting ride of Screen and ScreenManager, and just using Widgets, to control the whole drawing/positioning process.

Also, Clock.schedule_interval(self.update, 0) is equivalent to the call you are making, the animation will be called each frame, and you can use dt to manage the animation progress.

Also, kivy can manage gifs, as well as zip archives of images to directly do animations (useful to have animated pngs), you can let kivy manage the whole animation process this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top