Skip to content

Instantly share code, notes, and snippets.

@clayote
Created December 31, 2013 22:28
Show Gist options
  • Save clayote/8202892 to your computer and use it in GitHub Desktop.
Save clayote/8202892 to your computer and use it in GitHub Desktop.
The idea is you should be able to drag pawns from one spot to another and they should stick on the spot where you drop them.
from kivy.properties import (
ObjectProperty,
NumericProperty,
ListProperty
)
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
class Spot(Scatter):
pawns_here = ListProperty([])
def collide_point(self, x, y):
return self.ids.img.collide_point(x, y)
def on_pawns_here(self, *args):
for pawn in self.pawns_here:
pawn.pos = self.pos
class Pawn(Scatter):
board = ObjectProperty()
where_upon = ObjectProperty()
offx = NumericProperty(16)
offy = NumericProperty(16)
def collide_point(self, x, y):
return self.ids.img.collide_point(x, y)
def on_touch_up(self, touch):
if touch.grab_current is not self:
return
for spot in self.board.spotlayout.children:
if self.collide_widget(spot):
self.where_upon.pawns_here.remove(self)
spot.pawns_here.append(self)
self.where_upon = spot
class Board(FloatLayout):
pawnlayout = ObjectProperty()
spotlayout = ObjectProperty()
def __init__(self, **kwargs):
super(Board, self).__init__(**kwargs)
self.add_widget(self.spotlayout)
self.add_widget(self.pawnlayout)
def on_touch_down(self, touch):
return (
self.pawnlayout.on_touch_down(touch) or
self.spotlayout.on_touch_down(touch) or
super(Board, self).on_touch_down(touch))
def on_touch_move(self, touch):
return (
self.pawnlayout.on_touch_move(touch) or
self.spotlayout.on_touch_move(touch) or
super(Board, self).on_touch_move(touch))
def on_touch_up(self, touch):
return (
self.pawnlayout.on_touch_up(touch) or
self.spotlayout.on_touch_up(touch) or
super(Board, self).on_touch_up(touch))
kv = """
<Spot>:
AsyncImage:
id: img
source: 'http://rltiles.cvs.sourceforge.net/viewvc/rltiles/rltiles/dc-dngn64/cdoor1.bmp'
pos: root.pos
<Pawn>:
AsyncImage:
id: img
source: 'http://rltiles.cvs.sourceforge.net/viewvc/rltiles/rltiles/dc-mon64/crystal_golem.bmp'
x: root.x + root.offx
y: root.y + root.offy
"""
from kivy.lang import Builder
Builder.load_string(kv)
from kivy.app import App
class BoardApp(App):
def build(self):
board = Board(
pawnlayout=FloatLayout(),
spotlayout=FloatLayout(),
size=(400, 400))
spots = [
Spot(pos=(100, 100)),
Spot(pos=(200, 200)),
Spot(pos=(300, 300))]
for spot in spots:
pawn = Pawn(board=board, where_upon=spot)
spot.pawns_here.append(pawn)
board.pawnlayout.add_widget(pawn)
board.spotlayout.add_widget(spot)
return board
BoardApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment