Пример #1
0
class DiceScreen(Widget):

    def __init__(self, **kwargs):
        super(DiceScreen, self).__init__(**kwargs)
        self.cards = []
        self.rolling = False
        self.roll_sound = None
        self.play_sounds = False
        self.shake_to_roll = False

        self.shake_detector = ShakeDetector(on_shake=self.roll_dice)
        self.roll_sound = SoundLoader.load('diceroll.wav')

    def configure(self, config):
        self.play_sounds = config.getint('sound', 'effects') == 1
        self.shake_to_roll = config.getint('accelerometer', 'shake') == 1

        # Enable / disable the shake detector
        if self.shake_to_roll:
            self.shake_detector.enable()
        else:
            self.shake_detector.disable()

    def renew_cards(self):
        self.cards = list(itertools.product(range(1, 7), range(1, 7)))
        random.shuffle(self.cards)

    def pause(self):
        self.shake_detector.disable()

    def resume(self):
        if self.shake_to_roll:
            self.shake_detector.enable()

    def next_card(self):
        if not self.cards:
            self.renew_cards()
        return self.cards.pop()

    def roll_dice(self):

        def animate_roll(n, *args):
            if n > 0:
                self.dice1.number = random.randrange(1, 7)
                self.dice2.number = random.randrange(1, 7)
                Clock.schedule_once(partial(animate_roll, n - 1), 0.05)
            else:
                self.dice1.number, self.dice2.number = self.next_card()
                self.sum_label.text = str(self.dice1.number + self.dice2.number)
                self.rolling = False

        if not self.rolling:
            self.rolling = True
            self.sum_label.text = '?'
            Clock.schedule_once(partial(animate_roll, 20), 0.05)

            if self.play_sounds:
                if self.roll_sound.state == 'play':
                    self.roll_sound.stop()
                self.roll_sound.play()
Пример #2
0
    def __init__(self, **kwargs):
        super(DiceScreen, self).__init__(**kwargs)
        self.cards = []
        self.rolling = False
        self.roll_sound = None
        self.play_sounds = False
        self.shake_to_roll = False

        self.shake_detector = ShakeDetector(on_shake=self.roll_dice)
        self.roll_sound = SoundLoader.load('diceroll.wav')