def test_key_rotate(game: Game): game.board.drop() game.board.drop() game.board.drop() blocks = game.board.get_blocks() game.on_key_press(pyglet.window.key.UP, None) assert game.board.get_blocks() != blocks
def __init__(self, width, height, block_size): super().__init__(caption='Block Puzzle', width=width, height=height, fullscreen=False, resizable=False) self.main_batch = pyglet.graphics.Batch() self.text_batch = pyglet.graphics.Batch() self.game = Game(width, height, block_size, self.main_batch, self.text_batch, print_to_console=DEBUG) self.push_handlers(self.game.key_handler) self.push_handlers(self.game) pyglet.clock.schedule_interval(self.update, 1 / 120.0)
def game(): return Game(200, 100, 10, None, None)
def test_score_soft_drop(game: Game): for _ in range(5): game.soft_drop() assert 0 < game.score <= 5
def test_score_hard_drop(game: Game): game.on_key_press(pyglet.window.key.SPACE, None) assert 0 < game.score <= game.height * 2
def test_key_full_drop(game: Game): blocks = game.board.get_blocks() game.on_key_press(pyglet.window.key.SPACE, None) assert game.board.get_blocks() != blocks assert not game.board.is_piece_active()
def test_game_pause(game: Game): game.pause() assert game.is_paused() game.pause() assert not game.is_paused()
def test_game_init(game: Game): assert not game.game_over assert not game.is_paused()
class BlockPuzzle(pyglet.window.Window): def __init__(self, width, height, block_size): super().__init__(caption='Block Puzzle', width=width, height=height, fullscreen=False, resizable=False) self.main_batch = pyglet.graphics.Batch() self.text_batch = pyglet.graphics.Batch() self.game = Game(width, height, block_size, self.main_batch, self.text_batch, print_to_console=DEBUG) self.push_handlers(self.game.key_handler) self.push_handlers(self.game) pyglet.clock.schedule_interval(self.update, 1 / 120.0) def update(self, dt): if self.game.game_over: if not self.game.is_paused(): self.game.pause(show_text=False) print('Game Over!') else: self.game.update(dt) def on_draw(self): self.clear() self.main_batch.draw() self.text_batch.draw() def on_key_press(self, symbol, modifiers): super().on_key_press(symbol, modifiers) if symbol == pyglet.window.key.R: self.game.reset() if symbol == pyglet.window.key.P: self.game.pause() if symbol == pyglet.window.key.L: self.game.level_up()