在Kivy和Python之间旋转图像的行为

在Kivy和Python之间旋转图像的行为,python,kivy,Python,Kivy,在旋转图像和移动图像时,使用Kivy语言时,我很难理解Kivy在幕后做了什么 下面是一个代码,它应该在屏幕上以45度角绘制两个图像,然后每单击一次鼠标,将其旋转更多,然后在屏幕上向右移动 第一个图像是通过使用Kivy语言中定义的旋转来绘制的,第二个是我尝试仅用python来重做它(以便更好地理解Kivy实际上在做什么),但我失败了,因为python版本在增加x时首先没有将图像向右移动,但是看起来整个坐标系都被旋转了,因为它在屏幕上以45度角向上移动,第二,当我点击时,它不会旋转图像 我缺少什么,

在旋转图像和移动图像时,使用Kivy语言时,我很难理解Kivy在幕后做了什么

下面是一个代码,它应该在屏幕上以45度角绘制两个图像,然后每单击一次鼠标,将其旋转更多,然后在屏幕上向右移动

第一个图像是通过使用Kivy语言中定义的旋转来绘制的,第二个是我尝试仅用python来重做它(以便更好地理解Kivy实际上在做什么),但我失败了,因为python版本在增加x时首先没有将图像向右移动,但是看起来整个坐标系都被旋转了,因为它在屏幕上以45度角向上移动,第二,当我点击时,它不会旋转图像

我缺少什么,在Python中(不使用Kivy语言)需要做什么才能获得与第一个图像使用的相同的行为

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.graphics import Rotate
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.graphics.context_instructions import PopMatrix, PushMatrix


Builder.load_string('''
<TestKV>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: self.angle
            axis: (0, 0, 1)
            origin: self.center
    canvas.after:
        PopMatrix
''')

class TestKV(Image):
    angle = NumericProperty(0)

    def __init__(self, x, **kwargs):
        super(TestKV, self).__init__(**kwargs)
        self.x = x
        self.angle = 45

    def on_touch_down(self, touch):
        self.angle += 20
        self.x += 10


class TestPY(Image):
    angle = NumericProperty(0)

    def __init__(self, x, **kwargs):
        super(TestPY, self).__init__(**kwargs)
        self.x = x
        with self.canvas.before:
            PushMatrix()
            rot = Rotate()
            rot.angle = 45
            rot.origin = self.center
            rot.axis = (0, 0, 1)
        with self.canvas.after:
            PopMatrix()

    def on_touch_down(self, touch):
        self.angle += 20
        self.x += 10

class MainWidget(Widget):
    #this is the main widget that contains the game.

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        self.all_sprites = []

        self.k = TestKV(source="myTestImage.bmp", x=10)
        self.add_widget(self.k)

        self.p = TestPY(source="myTestImage.bmp", x=200)
        self.add_widget(self.p)


class TheApp(App):

    def build(self):
        parent = Widget()
        app = MainWidget()
        parent.add_widget(app)

        return parent

if __name__ == '__main__':
    TheApp().run()
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.image导入图像
从kivy.graphics导入旋转
从kivy.uix.widget导入widget
从kivy.properties导入NumericProperty
从kivy.graphics.context\u指令导入PopMatrix、PushMatrix
Builder.load_字符串(“”)
:
在以下情况之前:
推矩阵
轮换:
角度:self.angle
轴:(0,0,1)
来源:自我中心
在下列情况之后:
流行音乐
''')
TestKV级(图):
角度=数值属性(0)
定义初始值(self,x,**kwargs):
超级(测试千伏,自)
self.x=x
自转角=45
def on_触控向下(自身,触控):
自转角+=20
自身x+=10
类TestPY(图像):
角度=数值属性(0)
定义初始值(self,x,**kwargs):
超级(TestPY,self)。\uuuuu初始化(**kwargs)
self.x=x
使用self.canvas.before:
PushMatrix()
旋转
旋转角度=45
rot.origin=self.center
rot.axis=(0,0,1)
使用self.canvas.after:
PopMatrix()
def on_触控向下(自身,触控):
自转角+=20
自身x+=10
类MainWidget(Widget):
#这是包含游戏的主要小部件。
定义初始(自我,**kwargs):
超级(MainWidget,self)。\uuuuu初始化(**kwargs)
self.all_精灵=[]
self.k=TestKV(source=“myTestImage.bmp”,x=10)
self.add_小部件(self.k)
self.p=TestPY(source=“myTestImage.bmp”,x=200)
self.add_小部件(self.p)
类应用程序(应用程序):
def生成(自):
parent=Widget()
app=MainWidget()
parent.add_小部件(应用程序)
返回父级
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
TheApp().run()

您永远不会更改
旋转指令的角度。小部件上有一个
angle
属性,但它没有链接到任何东西。请尝试更新
Rotate
指令:

class TestPY(Image):
    def __init__(self, **kwargs):
        super(TestPY, self).__init__(**kwargs)
        # self.x = x -- not necessary, x is a property and will be handled by super()
        with self.canvas.before:
            PushMatrix()
            self.rot = Rotate()
            self.rot.angle = 45
            self.rot.origin = self.center
            self.rot.axis = (0, 0, 1)
        with self.canvas.after:
            PopMatrix()

    def on_touch_down(self, touch):
        self.x += 10
        self.rot.origin = self.center  # center has changed; update here or bind instead
        self.rot.angle += 20

感谢您的回答:这是唯一的答案,这样就不会假设
.kv
文件中存在过于简单的静态旋转。