帆布,尾巴较细的线

时间:2018-10-01 21:16:32

标签: python python-2.7 kivy kivy-language

我想创建类似鼠标尾巴的东西,这是代码。

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line


class BladeEfx(Widget):
    def on_touch_down(self, touch):
        color = (random(), 1, 1)
        with self.canvas:
            Color(*color, mode='hsv')
            touch.ud['line'] = Line(points=(touch.x, touch.y), source='particle.png', cap='round', joint='round', cap_precision=5, width=6)
    def on_touch_up(self, touch):
        self.canvas.remove(touch.ud['line'])
    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]
        #CCBlade efx
        self.clearTail(touch)
    def clearTail(self, touch):
       if len(touch.ud['line'].points) > 10: # 5:
           touch.ud['line'].points = touch.ud['line'].points[-10:]


class BladeApp(App):
    def build(self):
        parent = Widget()
        self.bldEfx = BladeEfx()
        parent.add_widget(self.bldEfx)
        return parent

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

现在,我希望该行的尾巴更细,类似于鼠标的尾巴,我想在我的游戏中使用这种效果 altenative 来获得我无法成功的水果忍者刀锋效果创建。

1 个答案:

答案 0 :(得分:0)

您可以通过使用一系列宽度不同的线来实现这种效果:

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line

MAX_WIDTH = 6.0
MAX_SEGMENTS = 10

class BladeEfx(Widget):
    def __init__(self, **kwargs):
        super(BladeEfx, self).__init__(**kwargs)
        self.lines = []
        self.width_delta = (MAX_WIDTH - 1.0)/ (MAX_SEGMENTS - 2)
        self.color = None

    def on_touch_down(self, touch):
        self.color = (random(), 1, 1)
        line = Line(points=(touch.x, touch.y), cap='round', joint='round', cap_precision=5)
        self.lines.append(line)

    def on_touch_up(self, touch):
        self.canvas.clear()
        self.lines = []

    def on_touch_move(self, touch):
        line = Line(points=(touch.x, touch.y), cap='round', joint='round', cap_precision=5)
        self.lines[-1].points += [touch.x, touch.y] # add second point to the previous line
        self.lines.append(line)   # add a new line (for now, it has just one point)
        self.lines = self.lines[-MAX_SEGMENTS:]    # limit number of lines to MAX_SEGMENTS

        self.canvas.clear()
        self.canvas.add(Color(*self.color, mode='hsv'))
        width = 1.0
        for line in self.lines:
            line.width = width
            width += self.width_delta
            self.canvas.add(line)  # add a line to the Canvas with the specified width


class BladeApp(App):
    def build(self):
        parent = Widget()
        self.bldEfx = BladeEfx()
        parent.add_widget(self.bldEfx)
        return parent

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