S
S
Stas Povalyaev2022-01-22 13:37:15
Python
Stas Povalyaev, 2022-01-22 13:37:15

How to make collision in python kivy?

Hello! is it possible and how to make collisions in python kivy?
I have a program where there are two class-objects, and they can be dragged across the stage, how to make it so that
when these objects collide, something would be written to the console?

help me please!

code for this program

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2022-01-22
@HemulGM

When an object moves, check if one enters the other

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question