Canvas,尾部较细的一条线

Canvas, A line with a thinner tail

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

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()

现在我希望这条线有一条更细的尾巴,类似于鼠标尾巴,我想在我的游戏中使用这种效果 替代 水果忍者 blade 我无法成功创建的效果。

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

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()