Search code examples
pythonpython-2.7kivykivy-language

Kivy - fruit ninja Blade effect


Am trying to create a CCBlade effects like that of fruit Ninja for my game and so am trying to clear the tail of the canvas to look like that of fruit ninja blade but i have no idea how to do it. Here is the code

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


class Blade(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), pointsize=5)
    def on_touch_up(self, *touch):
        self.canvas.clear()
    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]

        self.clearTail(touch)
    def clearTail(self, touch):
        #How can i clear the tail of the canvas as i move my finger
        #so that the line looks like it continuesly follows my finger
        #I want the lenght of the line to be about 5cm or a bit long
        #And no matter how long i move my finger 
        #the line must follows it and must not exceed 5cm or a bit longer (maybe 7cm - 15cm)
        #AM TRYING TO ARCHIEVE CCBLADE EFFECT LIKE THAT OF FRUIT NINJA
        pass
class BladeApp(App):

    def build(self):
        parent = Widget()
        self.bldEfx = Blade()
        parent.add_widget(self.bldEfx)
        return parent


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

How can i clear the tail of the canvas as i move my finger so that the line looks like it continuesly follows my finger. It should look like fruit ninja Blade effect. Is there any other way i can achieve fruit ninja blade effect on kivy.


Solution

  • You can try something like:

    def clearTail(self, touch)
        touch.ud['line'].points = touch.ud['line'].points[-10:]
    

    That limits the line to the last five points of the line (Two coordinates per point).