cout_HelloWorld_andl
@cout_HelloWorld_andl
Обожаю змею

Как сделать коллизию в python kivy?

Здравствуйте! можно ли и как сделать столкновения на python kivy?
у меня есть программа где есть два класса-объекта, и их можно по сцене перетягивать, как сделать так что бы
при столкновении этих объектов что-то писало в консоль?

помогите пожалуйста!
код этой программы

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Ellipse, Color
from kivy.core.window import Window
from random import randint
from kivy.uix.image import Image
from kivy.graphics import Rectangle


class Circle2Widget(Widget):
    def __init__(self, **kwargs):
        super(Circle2Widget, self).__init__(**kwargs)
        self.size = (50, 50)
        with self.canvas:
            Color(1, 0, 0, 0.5)
            self.circle = Rectangle(source='man1.png', pos=self.pos, size=self.size)
        self.bind(pos=self.redraw, size=self.redraw)

    def redraw(self, *args):
        self.circle.size = self.size
        self.circle.pos = self.pos

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            # if the touch collides with our widget, let's grab it 
            touch.grab(self)

            # and accept the touch.
            return True

        return super(Circle2Widget, self).on_touch_down(touch)

    def on_touch_up(self, touch):
        # check if it's a grabbed touch event
        if touch.grab_current is self:
            # don't forget to ungrab ourself, or you might have side effects
            touch.ungrab(self)

            # and accept the last up
            return True

        return super(Circle2Widget, self).on_touch_up(touch)

    def on_touch_move(self, touch):
        # check if it's a grabbed touch event
        if touch.grab_current is self:
            self.pos = touch.pos

            # and accept the last move
            return True

        return super(Circle2Widget, self).on_touch_move(touch)





class Circle1Widget(Widget):
    def __init__(self, **kwargs):
        super(Circle1Widget, self).__init__(**kwargs)
        self.size = (50, 50)
        with self.canvas:
            Color(1, 1, 0, 0.5)
            self.circle = Rectangle(source='man1.png', pos=self.pos, size=self.size)#Ellipse(pos=self.pos, size=self.size)
        self.bind(pos=self.redraw, size=self.redraw)

    def redraw(self, *args):
        self.circle.size = self.size
        self.circle.pos = self.pos

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            # if the touch collides with our widget, let's grab it 
            touch.grab(self)

            # and accept the touch.
            return True

        return super(Circle1Widget, self).on_touch_down(touch)

    def on_touch_up(self, touch):
        # check if it's a grabbed touch event
        if touch.grab_current is self:
            # don't forget to ungrab ourself, or you might have side effects
            touch.ungrab(self)

            # and accept the last up
            return True

        return super(Circle1Widget, self).on_touch_up(touch)

    def on_touch_move(self, touch):
        # check if it's a grabbed touch event
        if touch.grab_current is self:
            self.pos = touch.pos

            # and accept the last move
            return True

        return super(Circle1Widget, self).on_touch_move(touch)


class RootWidget(Widget):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        for i in range(5):
            self.add_widget(Circle1Widget(pos=(randint(0, Window.width - 50), randint(0, Window.height - 50))))
            self.add_widget(Circle2Widget(pos=(randint(0, Window.width - 50), randint(0, Window.height - 50))))


        


class MyApp(App):
    def build(self):
        return RootWidget()


if __name__ == "__main__":
    MyApp().run()

так же, картинка что нужна для нее:
61ebde58253e9956017479.png
  • Вопрос задан
  • 172 просмотра
Пригласить эксперта
Ответы на вопрос 1
HemulGM
@HemulGM Куратор тега Python
Delphi Developer, сис. админ
При движении объекта проверяй входит ли один в другой
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы