1
我正在寫一個yahtzee克隆來教自己Kivy(我仍然對Python和編程一般都很陌生),並且我在計算上遇到了一些麻煩了解如何最好地製作骰子滾動動畫。此代碼按預期工作,但我覺得我在概念上錯過了一些東西。在一段時間內發生時鐘事件是否有一種較少干涉或更簡潔的方式?Kivy:更好的方式來做定時事件(骰子滾動動畫)
這是我目前有:
父佈局持有5個骰子作爲孩子的小部件。用戶點擊一個按鈕,用這種方法推出他們都:
def roll_all_dice(self):
for dice in self.children:
Clock.schedule_interval(dice.roll, .1)
Clock.schedule_once(dice.roll_animation_callback, .5)
,如果我理解正確此,每一個在.1時間表一滾,再後來,5S,調用roll_animation_callback,該停止的事件。
下面是相關的骰子的方法:
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)