【问题标题】:Make an object rotate and move towards a touch in Kivy在 Kivy 中使对象旋转并朝触摸方向移动
【发布时间】:2014-08-29 15:53:07
【问题描述】:

在我正在开发的游戏中,我在获取对象时遇到了一些麻烦,以便在 Kivy 中进行触摸。到目前为止,这是我的代码:

class Player(Widget):
angle = NumericProperty(0)

def on_touch_move(self, touch):
    y = (touch.y - self.center[1])
    x = (touch.x - self.center[0])
    calc = math.degrees(math.atan2(y, x))
    new_angle = calc if calc > 0 else 360+calc

    self.angle = new_angle
    self.pos = [touch.x - 50, touch.y - 50]

我想要的是,当用户触摸(并握住屏幕)时,“播放器”会不断旋转以匹配触摸的位置并逐渐向触摸移动。建议? 我会继续努力,让你知道我用的是什么。

提前致谢, 伊尔蒙

编辑: 自发布以来,我已经尝试过这个并且效果更好......但是对象经常停止从光标向侧面移动几个像素。我希望它停止,因此光标应该直接位于上方……即移动设备上的玩家手指会握住它。

    def on_touch_move(self, touch):
    y = (touch.y - self.center[1])
    x = (touch.x - self.center[0])
    calc = math.degrees(math.atan2(y, x))
    new_angle = calc if calc > 0 else 360+calc

    self.angle = new_angle
    anim = Animation(x = touch.x, y = touch.y)
    anim.start(self)

【问题讨论】:

  • 当您说“不断旋转以匹配触摸的位置”时,您的意思是玩家旋转以面对触摸的位置吗? “持续”是什么意思?
  • 是的,“旋转以面对触摸的位置”。不过请看我的编辑,这有点不起作用,但它似乎相当不稳定和不准确。

标签: python animation rotation kivy


【解决方案1】:

这是一个非常简单的例子:

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.image import Image

from kivy.graphics import Rotate
from kivy.properties import NumericProperty

from math import atan2, degrees, abs

from kivy.animation import Animation

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

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

    def on_touch_down(self, touch):
        Animation.cancel_all(self)
        angle = degrees(atan2(touch.y - self.center_y, 
                              touch.x - self.center_x))

        Animation(center=touch.pos, angle=angle).start(self)


root = Builder.load_string('''                                                                                                                                 
Widget:                                                                                                                                                        
    PlayerImage:                                                                                                                                               
        source: 'colours.png'                                                                                                                                  
        allow_stretch: True                                                                                                                                    
        keep_ratio: False                                                                                                                                      
''')

runTouchApp(root)

这只是最基本的,但也许它可以帮助你回答你的问题。

您可能想要改变的一件大事是使用动画有点不灵活。如果这是一个动态类型的游戏,这个任务可能会更适合您的游戏更新循环,每个滴答声都会增量地移动和旋转。除此之外,这将对变化更加灵活,并且更容易以恒定速率移动/旋转,而不是在这种情况下总是精确地取 1s。

当然还有其他一些小事,比如让角度从 -pi 环绕到 pi,而不是在发生这种情况时几乎一直旋转。

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多