From 597f7f426ecb7dfd0bdd021dcc36380a4324d14d Mon Sep 17 00:00:00 2001 From: Thaloria Date: Thu, 20 Jul 2023 18:51:12 +0200 Subject: [PATCH] added first draft litris --- litris.py | 71 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/litris.py b/litris.py index 0c10a2b..a76fb7f 100644 --- a/litris.py +++ b/litris.py @@ -73,6 +73,18 @@ class Litris(GameBase): # 2: cv.imread("sodoku/2.jpg", cv.IMREAD_COLOR), } + self.full_stones_dic = {1: BLOCK_FULL, + 2: L1_FULL, + 3: LINE_FULL, + 4: DOT_FULL + } + + self.col_stones_dic = {1: BLOCK_COL, + 2: L1_COL, + 3: LINE_COL, + 4: DOT_COL + } + def fill_data_coordinates(self): # 610 to 1950 = 1340 - 76 / 20 = 63 # 40 to 1380 = 1340 - 76 / 20 = 63 @@ -94,7 +106,7 @@ class Litris(GameBase): # cv.waitKey(2000) current_stone = self.new_stone_detection_and_identification() new_observation, new_screenshot = self.get_current_board_state() - col = self.find_place_for_stone(current_stone, new_screenshot) + col = self.find_place_for_stone(current_stone, new_observation) self.move_stone(col) @@ -103,8 +115,8 @@ class Litris(GameBase): def get_current_board_state(self): # get an updated image of the game - #screenshot = self.capture_window.get_screenshot() - screenshot = cv.imread("litris/main_playfield.jpg") + screenshot = self.capture_window.get_screenshot() + #screenshot = cv.imread("litris/main_playfield.jpg") # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] @@ -134,16 +146,16 @@ class Litris(GameBase): # self.change_value(x, y, int(needle_key)) # print(field.data_value_grid) #cv.circle(screenshot, points[0], 7, (0, 255, 0), -1) - output_image = self.vision_stun.draw_rectangles(screenshot, rectangles) - cv.imshow("output_image", output_image) - cv.waitKey(150) + #output_image = self.vision_stun.draw_rectangles(screenshot, rectangles) + #cv.imshow("output_image", output_image) + #cv.waitKey(150) return data_coords, screenshot def new_stone_detection_and_identification(self): stone_coords = np.zeros((4, 4), dtype=object) while True: - # screenshot = self.capture_window.get_screenshot() - screenshot = cv.imread("litris/main_playfield.jpg") + screenshot = self.capture_window.get_screenshot() + #screenshot = cv.imread("litris/main_playfield.jpg") # 1148 1412 580 845 screenshot = screenshot[580:845, 1148:1412] #cv.imshow("screenshot", screenshot) @@ -170,25 +182,46 @@ class Litris(GameBase): return stone_coords def find_place_for_stone(self, stone, current_board): - # block - for e in range(18, 20, 1): - for i in range(0, 19, 1): - if current_board[e][i] == 0 and current_board[e - 1][i] == 0 and current_board[e][i + 1] == 0 and current_board[e - 1][i + 1] == 0: - return i - BLOCK_COL + if mse(stone, BLOCK_FULL): + # block + for e in range(18, 20, 1): + for i in range(0, 19, 1): + if current_board[e][i] == 0 and current_board[e - 1][i] == 0 and current_board[e][i + 1] == 0 and current_board[e - 1][i + 1] == 0: + return i - BLOCK_COL + elif mse(stone, L1_FULL): + # L1 + for e in range(18, 20, 1): + for i in range(0, 19, 1): + if current_board[e][i] == 0 and current_board[e][i + 1] == 0 and current_board[e][i + 2] == 0 and \ + current_board[e - 1][i + 2] == 0: + return i - L1_COL + elif mse(stone, LINE_FULL): + # Line + for e in range(19, 20, 1): + for i in range(0, 19, 1): + if current_board[e][i] == 0 and current_board[e][i + 1] == 0 and current_board[e][i + 2] == 0 and \ + current_board[e][i + 3] == 0: + return i - LINE_COL + elif mse(stone, DOT_FULL): + # Dot + for e in range(19, 20, 1): + for i in range(0, 19, 1): + if current_board[e][i] == 0: + return i - DOT_COL def move_stone(self, col_movement): - keyboard.press('down') - cv.waitKey(100) + keyboard.press('Down') + cv.waitKey(500) if col_movement < 0: for i in range(0, col_movement, - 1): - keyboard.press('left') - cv.waitKey(100) + keyboard.press('Left') + cv.waitKey(500) else: for i in range(0, col_movement, 1): - keyboard.press('right') - cv.waitKey(100) + keyboard.press('Right') + cv.waitKey(500) def point_in_rect(self, point): for e in range(0, 20, 1):