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()
так же, картинка что нужна для нее: