Kivy:物体在移动时生成/画出一条小道

时间:2017-11-17 03:03:47

标签: python python-2.7 kivy

早上好/晚上好: - )

我正在关注视频,试图在移动时从圆圈生成“踪迹”,轨迹是黄线。更好地展示图像而不是长篇解释:
> Link to the image on top of the video to describe what I'm trying to achieve
> Link to the tutorial video (without trail)

- 有人可以帮我摆脱错误吗?

- 我真的不知道如何生成这条线索......

...也许通过绘画点涂画应用,但“on_touch_move”应该像on_move那样......?
(我最后会发一段代码)


错误:

/ex38.kv", line 24:
    ...
         22:    pos: 300,root.top-100
         23:    text:
    >>   24:        ( '[size=32][color=88FF22]' +
         25:        root.ball.anim_choice +
         26:        '[/color][/size]' )
    ...

/ex38.kv", line 24:
    ...
         22:    pos: 300,root.top-100
         23:    text:
    >>   24:        ( '[size=32][color=88FF22]' +
         25:        root.ball.anim_choice +
         26:        '[/color][/size]' )
    ...
    AttributeError: 'Label' object has no attribute 'ball'


主要Python:

#ex38.py    
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.animation import Animation
from kivy.properties import StringProperty

from kivy.lang import Builder

Builder.load_file('ex38.kv')

class Ball(Widget):
    anim_choice = StringProperty('touch_down to animate ball')
    counter = 0
    def on_touch_down(self, touch):
        Animation.cancel_all(self)
        anim_choices = ['in_back','in_bounce','in_circ',
                        'in_cubic','in_elastic','in_expo',
                        'in_out_back','in_out_bounc','in_out_circ',
                        'out_sine']
        self.anim_choice = anim_choices [self.counter]
        anim = Animation(center_x = touch.x,
                         center_y = touch.y,
                         t = self.anim_choice)
        anim.start(self)
        self.counter = (self.counter+1) % len(anim_choices)

class Ex38(Widget):
   pass

class Ex38App(App):
    def build(self):
        return Ex38()

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

Kivy:

# ex38.kv

<Ball>:
    size: 65,60
    canvas:
        Color:
            rgb: 0.75,0,0
            Ellipse:
                pos: self.pos
                size: self.size

<Ex38>:
    ball: ball_id
    canvas:
        Color:
            rgb: 0,0,1
        Rectangle:
            size: root.width,root.height
            pos: 0,0

Label:
    pos: 300,root.top-100
    text:
        ( '[size=32][color=88FF22]' +
        root.ball.anim_choice +
        '[/color][/size]' )
    markup: True 
<Ball>:
    id: ball_id





PS :跟踪生成器的想法:

  

重要事项:
- 轨迹不应该是鼠标移动,而是与椭圆移动有关。   
- 圆圈不会总是直线移动,也可以做随机轨迹。   
生成点应该没问题......

def on_touch_move(self, touch): #That one shouldn't be for the mouse but related to our Ellipse
        global length,n_points,last_x,last_y
        if touch.button=='left':
            touch.ud['line'].points += [touch.x, touch.y]
            x = int(touch.x)
            y = int(touch.y)
            length += np.sqrt(max((x - last_x)**2 + (y - last_y)**2, 2))
            n_points += 1.
            density = n_points/(length)
            touch.ud['line'].width = int(20*density + 1)
            sand[int(touch.x) - 10 : int(touch.x) + 10, int(touch.y) - 10 : int(touch.y) + 10] = 1
            last_x = x
            last_y = y

致以最诚挚的问候,希望您有兴趣。

0 个答案:

没有答案