def insert_diamond(self, column): self.moving_blocks = Diamond(self.frame, self.tile_factory, self.level, 2)
def insert_fruit_pair(self, first, second): self.moving_blocks = FruitPair(self.frame, self.tile_factory, self.level, (first, second))
class FrutrisBox: def __init__(self, frame, tile_factory, level): self.frame = frame self.tile_factory = tile_factory self.level = FrutrisLevel(frame, tile_factory, level) self.moving_blocks = None self.insert_random_fruit_pair() self.drop_delay = DROP_DELAY self.drop_counter = self.drop_delay self._queued_command = None self._fast_drop = 0 self.diamonds_queued = 0 self.update_mode = self.update_drop self.counter = MultipletCounter() self.game_over = False def insert_diamond(self, column): self.moving_blocks = Diamond(self.frame, self.tile_factory, self.level, 2) def insert_fruit_pair(self, first, second): self.moving_blocks = FruitPair(self.frame, self.tile_factory, self.level, (first, second)) def insert_random_fruit_pair(self): blocks = 'abcefg' first = blocks[randint(0, MAX_FRUIT)] second = blocks[randint(0, MAX_FRUIT)] self.insert_fruit_pair(first, second) @property def fast_drop(self): return self._fast_drop >= 2 def insert_new_block(self): """Inserts a random block at top center.""" if self.diamonds_queued > 0: self.insert_diamond(randint(1, 6)) self.diamonds_queued -= 1 play_effect('diamond_drop') self.update_mode = self.update_autodrop else: self.insert_random_fruit_pair() self.update_mode = self.update_drop def remove_blocks(self): """Find vanishing blocks.""" multiplets = self.level.find_multiplets() if len(multiplets) > 0: self.counter.count(multiplets) self.moving_blocks = self.level.get_explosions(multiplets) play_effect('fruit_drop_after_vanish') self.update_mode = self.update_remove else: self.all_moves_finished() def all_moves_finished(self): """All explosions and drops finished.""" # play_effect(self.counter.get_category()) self.counter.reset() if self.level.box_overflow(): self.game_over = True else: self.insert_new_block() def update_autodrop(self): """Drops fruit without user interaction.""" self.moving_blocks = self.level.get_dropped_bricks() if self.moving_blocks.finished: self.remove_blocks() def update_drop(self): """Dropping fruit after some time.""" self.handle_command() if self.moving_blocks.finished: self.drop_counter -= 1 if self.drop_counter == 0 or self.fast_drop: self.drop_counter = self.drop_delay self.moving_blocks.drop() if self.moving_blocks.finished: self.remove_blocks() self._fast_drop = 0 def update_remove(self): """Explosions or dropping finished.""" self.moving_blocks = self.level.get_dropped_bricks() if self.moving_blocks: # play_effect('fruit_drop_after_vanish') self.update_mode = self.update_autodrop else: self.all_moves_finished() def draw(self): if self.moving_blocks.finished: self.update_mode() if not self.moving_blocks.finished: self.moving_blocks.move() self.level.draw() self.moving_blocks.draw() def store_command(self, direction): self._queued_command = direction def handle_command(self): if self._queued_command: direction = self._queued_command self._queued_command = None if direction == DOWN: self.moving_blocks.drop() self._fast_drop += 1 else: self._fast_drop = 0 if direction in (LEFT, RIGHT): self.moving_blocks.shift(direction) elif direction == UP: self.moving_blocks.rotate()