I am writing a yahtzee clone to teach myself Kivy (I'm still very new to Python and programming in general), and I'm having a little trouble figuring out how to best animate the dice rolling. This code works as intended, but I feel like I'm missing something conceptually. Is there a less involved or cleaner way to have a Clock event happen for a set period of time?
This is what I currently have:
The parent layout holds 5 dice as children widgets. The user clicks a button to roll them all using this method:
def roll_all_dice(self):
for dice in self.children:
Clock.schedule_interval(dice.roll, .1)
Clock.schedule_once(dice.roll_animation_callback, .5)
which, if I understand this correctly, schedules a roll every .1s, then .5s later, calls the roll_animation_callback, which stops the events.
Here are the relevant Dice methods:
def roll(self, *args):
'''changes the number of the die and updates the image'''
if self.state != "down":
self.number = randint(1,6)
self.source = self.get_image()
def get_image(self):
'''returns image path for each of the die's sides'''
if self.state == "down":
return "images/down_state/dice" + str(self.number) + ".png"
else:
return "images/up_state/dice" + str(self.number) + ".png"
def roll_animation_callback(self, *args):
'''turns off the dice rolling animation event'''
Clock.unschedule(self.roll)
This seems fine, using the Clock like this is normal.