如何用鼠标旋转kivy中的小部件

问题描述 投票:0回答:1

我正在尝试在

kivy
中创建一个跟随鼠标的矩形,以便矩形的顶部(短边)始终垂直于矩形和鼠标之间的线。

由于我对 Kivy 不太了解,所以我无法尝试任何东西。

完整代码:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.core.audio import SoundLoader
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.config import Config 
from kivy.graphics import *
from kivy.utils import *

class Myapp(Widget):
    Config.set('graphics', 'resizable', True) 
    def __init__(self, **kwargs):
        super(Myapp, self).__init__(**kwargs)
        
    sound = SoundLoader.load('theme.mp3')
    sound.play()

class MenuScreen(Screen):
    pass

class Game(Screen, Widget):
    def __init__(self, **kwargs):
        super(Game, self).__init__(**kwargs)

        Config.set('graphics', 'resizable', True) 

        with self.canvas:
            Color(0, 0, 0)
            Rectangle(pos=(10, 10), size=(150, 50))

class Myapp(ScreenManager):
    pass

class MyApp(App):
    def build(self):
        return Myapp()

if __name__ == '__main__':
    MyApp().run()

我正在尝试做的事情的完整描述: 在初始启动屏幕后,我单击开始按钮转到主位置。 现在,在这个地方,我需要有一个跟随鼠标移动的矩形。

python kivy kivy-language
1个回答
0
投票

找到了,基本上每次都是使用PopMatrix和PushMatrix来创建一个矩形,并且在update_cannon_rotation函数中使用self.cannon_angle将矩形的位置指向你的鼠标。每次移动鼠标时都应用此过程(查看游戏类中的 Window.bind(mouse_pos = self.update_cannon_rotation)

我的应用程序.py

    `python
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.widget import Widget
import math
from kivy.graphics import Color, Rectangle, Ellipse, PushMatrix, PopMatrix, Rotate, Line
from kivy.config import Config 


class MyApp(Widget):
    def __init__(self, **kwargs):
        super(Myapp, self).__init__(**kwargs)
        Config.set('graphics', 'resizable', True)

class MenuScreen(Screen):
    pass

class Game(Screen, Widget):
    def __init__(self, **kwargs):
        super(Game, self).__init__(**kwargs)

        self.cannon_size = (150, 50)
        self.cannon_pos = (0, 0) 
        self.fixed_point = (self.cannon_pos[0], self.cannon_pos[1] + self.cannon_size[1] / 2)
        Window.bind(mouse_pos=self.update_cannon_rotation)

    def update_cannon_rotation(self, *args):
        mouse_x, mouse_y = Window.mouse_pos
        delta_x = mouse_x - self.fixed_point[0]
        delta_y = mouse_y - self.fixed_point[1]
        self.cannon_angle = math.degrees(math.atan2(delta_y, delta_x))

        self.canvas.clear()
        with self.canvas:
            PushMatrix()
            Rotate(angle=self.cannon_angle, origin=self.fixed_point)
            Color(1, 1, 1)  
            self.cannon = Rectangle(pos=self.cannon_pos, size=self.cannon_size)
            PopMatrix()

class Myapp(ScreenManager):
    pass

class MyApp(App):
    def build(self):
        return Myapp()

if __name__ == '__main__':
    MyApp().run()

    `

我的.kv:

<Myapp>
    canvas.before:
        Rectangle:
            size: self.size
            pos: self.pos
            source: "background.jpg"
    MenuScreen:
        id: name
        name: "home"
    Game:
        id: name
        name: "game"
    


<MenuScreen>:
    BoxLayout:
        Button:
            text: "I'M READY."
            font_size: 72
            size_hint_y : None
            height: 100
            size_hint_x: None 
            width: 600
            on_release:
                root.manager.current = 'game'
        
© www.soinside.com 2019 - 2024. All rights reserved.