From 50ab6e17ef5848baf53b94574ac06fa4b9ad14d4 Mon Sep 17 00:00:00 2001 From: Thaloria Date: Mon, 24 Jul 2023 09:59:13 +0200 Subject: [PATCH] added async stone detection thread --- field.py | 9 +++++++++ litris.py | 48 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/field.py b/field.py index a9adb65..88933c8 100644 --- a/field.py +++ b/field.py @@ -13,6 +13,7 @@ class Field(): else: self.state = [[' ' for cols in range(Field.WIDTH)] for rows in range(Field.HEIGHT)] + self.cleared_rows = 0 def __str__(self): BAR = ' ' + '-' * (Field.WIDTH * 2 + 1) + '\n ' + \ @@ -21,6 +22,13 @@ class Field(): '{:2d} |'.format(i) + ' '.join(row) + '|' for i, row in enumerate(self.state)]) + '\n' + BAR + + def rotate_state(self): + self.state = list(zip(*self.state[::-1])) + + def get_line_count(self): + return self.cleared_rows + def _test_tetromino(self, tetromino, row, column): """ Tests to see if a tetromino can be placed at the specified row and @@ -80,6 +88,7 @@ class Field(): self.state = list(filter(lambda row: row.count(' ') != 0, self.state)) while len(self.state) < Field.HEIGHT: self.state.insert(0, [' ' for col in range(Field.WIDTH)]) + self.cleared_rows = self.cleared_rows + 1 def copy(self): """ diff --git a/litris.py b/litris.py index a90b74d..66cba80 100644 --- a/litris.py +++ b/litris.py @@ -45,6 +45,8 @@ class Litris(GameBase): self.stone_id_thread = NewStoneID() + self.move_mode = 1 + def reset_field(self): self.state = [[' ' for cols in range(Field.WIDTH)] @@ -93,14 +95,24 @@ class Litris(GameBase): offset_col = current_tetromino.get_offset_column(rotation) print("offset column:", offset_col) self.field.drop(current_tetromino, column) - #self.move_stone(column - offset_col, rotation) - for i in range(1,5,1): + self.move_stone(column - offset_col, rotation) + for i in range(1,8,1): self.keyboard.press(Key.down) self.keyboard.release(Key.down) print("direction pressed: drop down") cv.waitKey(50) self.stone_id_thread.set_pick_up_status(False) print(self.field) + if self.field.get_line_count() % 5 == 0: + self.field.rotate_state() + self.update_move_mode() + + + def update_move_mode(self): + if self.move_mode <=3: + self.move_mode = self.move_mode + 1 + elif self.move_mode == 4: + self.move_mode = 1 def get_current_board_state(self): # get an updated image of the game @@ -241,12 +253,30 @@ class Litris(GameBase): else: return None - def move_stone(self, col_movement, rotation): + def move_stone(self, col_movement, rotation, move_mode): if col_movement is None: return + + if move_mode == 1: + down = Key.down + left = Key.left + right = Key.right + elif move_mode == 2: + down = Key.left + left = Key.up + right = Key.down + elif move_mode == 3: + down = Key.up + left = Key.right + right = Key.left + elif move_mode == 4: + down = Key.right + left = Key.down + right = Key.up + # Press and release space - self.keyboard.press(Key.down) - self.keyboard.release(Key.down) + self.keyboard.press(down) + self.keyboard.release(down) print("direction pressed: down") cv.waitKey(120) @@ -280,14 +310,14 @@ class Litris(GameBase): if col_movement < 0: for i in range(0, col_movement, - 1): - self.keyboard.press(Key.left) - self.keyboard.release(Key.left) + self.keyboard.press(left) + self.keyboard.release(left) print("move left 3 pressed") cv.waitKey(40) else: for i in range(0, col_movement, 1): - self.keyboard.press(Key.right) - self.keyboard.release(Key.right) + self.keyboard.press(right) + self.keyboard.release(right) print("move right 3 pressed") cv.waitKey(40)