Moving multiple Widgets in Kivy independently

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Moving multiple Widgets in Kivy independently



I'm trying to create a Royal game of Ru with python using Kivy libs.



What I'm trying to do is to create a board (Which is done, but more complicated work is ahead) and 7 pieces (figures) for each player. I managed to create all 7, but had no idea how to manipulate them independently or move at all.



I found a code that allowed me to move an object by using mouse, but I need to only move the object that my mouse is on top of. Sort of like grabbing only the chess piece you need, and not the queen all the time.



Code:


from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Ellipse
from kivy.clock import Clock
from random import random

class CircleWidget(Widget):
def __init__(self, **kwargs):
Widget.__init__(self, **kwargs)
self.size = (50,50)
for i in range(0, 7):
self.circle = Ellipse(pos = self.pos, size = self.size)
self.canvas.add(self.circle)

# handle position change
def on_pos(self, obj, new_pos):
self.circle.pos = new_pos # when widget moves, so does the graphic instruction

class RootWidget(Widget):

def __init__(self, **kwargs):
Widget.__init__(self, **kwargs)
self.cw = CircleWidget()
self.add_widget(self.cw)

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
touch.grab(self)
# do whatever else here

def on_touch_move(self, touch):
if touch.grab_current is self:
print("This prints all the time...")
self.cw.pos = (touch.x,touch.y)

def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
# and finish up here

def update(self, dt):
print("No idea why I need this")

class MyApp(App):
def build(self):
rw = RootWidget()
# call update() every second
Clock.schedule_interval(rw.update, 1.0)
return rw

MyApp().run()



Also, I forgot to edit the positions of all figures to be next to each other, but that's not a hard task.



Any help with moving them one by one?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS