Rotating image behavior between kiwi and python

I am having a problem understanding what kivy is doing behind the scenes when using kiwi when rotating images and moving them. Below is the code which is to draw two images at a 45 degree angle on the screen, then rotate it more for each mouse click and then move it to the right on the screen. The first image is drawn using a rotation defined in the kivy language, where the second is where I am trying to redo it in python only (to better understand what kivy does), but I fail because the python version does not move the image to the right at first when I increase x, but it looks like the entire coordinate system has been rotated for this image as it moves at a 45 degree angle up the screen, and secondly, it doesn't rotate that image when I click. Anyonecan anyone tell me what I am missing, what to do in python (without using the kiwi language) to get the same behavior as the first image?

early!

Dan

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()

      

+3


source to share


1 answer


You never change your team angle Rotate

. You have a property angle

on your widget, but it is not related to anything. Try to update the instruction 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

      

+3


source







All Articles