让一个物体旋转并在Kivy中移动

时间:2014-08-29 15:53:07

标签: python animation rotation kivy

我在游戏中遇到一个对象时遇到了一些麻烦,我正在开发一种在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]

我想要的是当用户触摸(并保持屏幕)时,"播放器"连续旋转以匹配触摸的位置并逐渐朝触摸方向移动。建议? 我会继续工作,让你知道我用的是什么。

提前致谢, Ilmiont

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

    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)

1 个答案:

答案 0 :(得分:2)

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

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)

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

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

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