Kivy: What is the correct method for animating canvas images?

I don’t quite understand how to use the canvas correctly for animated images.

See the attached snippet where I load an animated icon into the image and do both: (1) add_widget Image (2) create a Rectangle canvas command with texture = texture image

Animated Image Rectangle Texture Not

I read the entire Kivy tutorial and read Image and Canvas, and I realized that Image is a good high-level class with all this image animation processing, and Canvas is a coarser black and white canvas.

So here is my question: what is the Kivy-proper architecture for processing animations on Canvas? I watched the animation, but it looks like an animation similar to a matrix, such as translation, scaling, rotation.

Here's what I am doing now: I have a game with a large map window, and then a bunch of UX games in auxiliary windows In UX game helper windows, I do all the kivy layouts, etc. And I mainly use images, and therefore my icons are pleasantly animated

However, on the game map I use canvas:

Drawing all my game objects using this paradigm:

r=Rectangle(texture=some_Image.texture) map.canvas.add(r) 

When the world needs to be redrawn:

1) map.canvas.clear()

2) draw all the things in their new positions and state (to be faster, I just have to keep track of dirty objects and locations and just draw them, but to be honest, I get fantastic pictures even with this clear level of clarity in every draw)

This, of course, is much faster and easier than creating and destroying hundreds of widget classes - why does the map canvas take place - right?

But the problem is that my animation icons in the zip file are not animated

Q: I think the canvas is wrong? Should I add an image for each of my game objects instead? (And take advantage of support for all animated images?)

 from kivy.uix.relativelayout import RelativeLayout from kivy.uix.image import Image from kivy.app import App from kivy.graphics import Rectangle class MainApp(App): def __init__(self, **kwargs): super().__init__(**kwargs) self.root = RelativeLayout() # use any zip file of an animated image self.animated_icon = Image(source='factory_icon.zip') # If I add an Image, the icon animates self.root.add_widget(self.animated_icon) # If I add the Image texture on to a Rectangle instruction, no animation r = Rectangle(texture=self.animated_icon.texture, size=(100, 100), pos=(100, 100)) self.root.canvas.add(r) def build(self): return self.root if __name__ == '__main__': MainApp().run() 
+5
source share
1 answer

Image.texture property change over time. He plans internal methods for updating as the animation goes. This change does not apply to your rectangle because you created it while maintaining the texture value for a certain period of time between updates. Consider this example (I use the .gif file for animation, but the principle should be the same):

 from kivy.uix.relativelayout import RelativeLayout from kivy.uix.image import Image from kivy.app import App from kivy.graphics import Rectangle class MainApp(App): def __init__(self, **kwargs): super(MainApp, self).__init__(**kwargs) self.root = RelativeLayout() animated_icon = Image(source='test.gif') animated_icon.bind(texture=self.update_texture) self.r = Rectangle(texture=animated_icon.texture, size=(500, 255), pos=(100, 100)) self.root.canvas.add(self.r) def update_texture(self, instance, value): self.r.texture = value def build(self): return self.root if __name__ == '__main__': MainApp().run() 

Here I bind my own update_texture method to the texture image, so every time it changes, I can update the rectangle accordingly.

+1
source

Source: https://habr.com/ru/post/1274561/


All Articles