Eri*_*hke 6 python animation canvas kivy
我不完全了解如何正确使用画布与动画图像.
请参阅附件中的片段,我将动画图标加载到图像中并同时执行以下操作:(1)add_widget图像(2)创建一个带纹理=图像纹理的矩形画布指令
图像动画矩形纹理没有
我已经阅读了所有Kivy手册并阅读了Image和Canvas,我认为Image是一个很好的高级类,所有这些图像动画处理和Canvas更像是一个原始的低级绘图画布.
所以这是我的问题 - 在Canvas上处理动画的Kivy正确架构是什么?我看了动画,但这似乎更像矩阵般的动画,如平移,缩放,旋转.
这就是我现在正在做的事情:我有大型地图窗口的游戏,然后帮助窗口中的一堆游戏用户体验游戏用户体验助手窗口我做所有的kivy布局等等并且通常使用图像,所以我的图标很好地动画
但是在游戏地图中,我使用的是canvas:
使用此范例绘制我的所有游戏对象:
r=Rectangle(texture=some_Image.texture)
map.canvas.add(r)
Run Code Online (Sandbox Code Playgroud)
当世界需要重新绘制时:
1) map.canvas.clear()
2)在他们的新位置和状态中绘制所有的东西(为了更快,我应该只跟踪脏对象和位置并且只是绘制它们,但说实话我即使在每个核级别清晰的情况下也会获得梦幻般的fps画)
这当然比创建和销毁数百个小部件类要快得多,重量轻 - 地图画布用于什么 - 对吧?
但问题是我在zip文件中带动画的图标不是动画
问:我认为画布错了吗?我应该为每个游戏对象添加一个图像吗?(并利用所有动画图像支持?)
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's 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()
Run Code Online (Sandbox Code Playgroud)
Image.texture属性随时间变化。它安排内部方法来随着动画的进行而更新它。此更改不会传播到您的矩形,因为您使用在更新之间的某个特定时间点捕获的纹理值创建了它。考虑这个例子(我使用 .gif 文件作为动画,但原理应该是相同的):
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()
Run Code Online (Sandbox Code Playgroud)
在这里,我将自己的update_texture方法绑定到图像的texture属性,因此每次更改时我都可以相应地更新矩形。