Skip to content

Instantly share code, notes, and snippets.

@kived
Created September 17, 2014 17:41
Show Gist options
  • Save kived/591ea6d14c489038877c to your computer and use it in GitHub Desktop.
Save kived/591ea6d14c489038877c to your computer and use it in GitHub Desktop.
kivy properties
import kivy
kivy.require('1.8.0')
import random
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.event import EventDispatcher
from kivy.properties import NumericProperty
class Actor(EventDispatcher):
health = NumericProperty(100)
attack = NumericProperty(10)
mana = NumericProperty(20)
class TestApp(App):
def build(self):
root = BoxLayout(orientation='vertical')
self.actors = []
for i in range(14):
root.add_widget(self.create_actor_box())
btn = Button(text='Change')
root.add_widget(btn)
btn.bind(on_press=self.change)
self.change()
return root
def create_actor_box(self):
actor = Actor()
self.actors.append(actor)
box = BoxLayout()
lhealth = Label()
lattack = Label()
lmana = Label()
box.add_widget(lhealth)
box.add_widget(lattack)
box.add_widget(lmana)
actor.bind(health=lambda a, v: setattr(lhealth, 'text', str(v)),
attack=lambda a, v: setattr(lattack, 'text', str(v)),
mana=lambda a, v: setattr(lmana, 'text', str(v)))
return box
def change(self, *args):
for actor in self.actors:
actor.health = random.randint(0, 100)
actor.attack = random.randint(5, 15)
actor.mana = random.randint(0, 20)
if __name__ == '__main__':
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment