From c9081bd3717d3cbea696149ad36f771297d6474d Mon Sep 17 00:00:00 2001 From: Thaloria Date: Sat, 29 Jul 2023 20:12:51 +0200 Subject: [PATCH] update litris with board detection --- litris.py | 22 +++--------------- litris_stone_id_thread.py | 49 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/litris.py b/litris.py index 91a979a..8f1acd7 100644 --- a/litris.py +++ b/litris.py @@ -15,18 +15,10 @@ class Litris(GameBase): self.keyboard = Controller() - self.data_coordinates = np.zeros((20, 20), dtype=object) - self.observation = np.zeros((20, 20), dtype=int) - - self.colors = [1, 2, 3, 4, 5, 6, 7, 8, 9] - self.offset_left = 610 self.offset_down = 40 - self.fill_data_coordinates() - self.field = Field() - self.field_mem = Field() self.litris_reset_board = cv.imread("control_elements/sodoku_reset_button.jpg", cv.IMREAD_COLOR) @@ -34,17 +26,6 @@ class Litris(GameBase): self.move_mode = 1 - def fill_data_coordinates(self): - # 610 to 1950 = 1340 - 76 / 20 = 63 - # 40 to 1380 = 1340 - 76 / 20 = 63 - # spacing 19 * 4 - dim = 63 - e_spacing = 4 - i_spacing = 4 - for e in range(0, 20, 1): - for i in range(0, 20, 1): - self.data_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim] - def assess_playfield_and_make_move(self): last_letter_received = time() while True: @@ -92,6 +73,7 @@ class Litris(GameBase): if self.field.get_line_count() >= 6 and self.field.height() <= 2: if self.field.predict_gaps_in_next_rotation() <= 5: self.update_move_mode() + self.field = self.stone_id_thread.get_current_board_state() self.field.rotate_state() #self.update_move_mode() self.field.reset_half_field() @@ -105,6 +87,8 @@ class Litris(GameBase): self.stone_id_thread.set_pick_up_status(False) + + def drop_down(self): if self.move_mode == 1: down = Key.down diff --git a/litris_stone_id_thread.py b/litris_stone_id_thread.py index a196977..4a6cab7 100644 --- a/litris_stone_id_thread.py +++ b/litris_stone_id_thread.py @@ -26,6 +26,7 @@ class NewStoneID(threading.Thread): self.vision_stun = Vision() self.stone_coordinates = np.zeros((4, 4), dtype=object) + self.data_coordinates = np.zeros((20, 20), dtype=object) self.fill_data_coordinates() self.needles = {1: cv.imread("litris/blue_needle.jpg", cv.IMREAD_UNCHANGED)} @@ -133,6 +134,49 @@ class NewStoneID(threading.Thread): return e, i return None, None + 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 = screenshot[40:1380, 610:1950] # 1000,1000 + + # cv.imshow("screenshot", screenshot) + # cv.waitKey(150) + # continue + data_coords = np.zeros((20, 20), dtype=object) + #field = Pickaxe_Field() + for needle_key in self.needles.keys(): + #gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY) + #thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] + rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.8, 100) + if len(rectangles) == 0: + continue + points = self.vision_stun.get_click_points(rectangles) + + for point in points: + x, y = self.point_in_rect(point) + if x is not None and y is not None: + data_coords[x][y] = int(needle_key) + #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) + return data_coords.tolist() + + def point_in_rect(self, point): + for e in range(0, 20, 1): + for i in range(0, 20, 1): + x1, y1, w, h = self.data_coordinates[e][i] + x2, y2 = x1 + w, y1 + h + x, y = point + if x1 < x and x < x2: + if y1 < y and y < y2: + return e, i + return None, None + def fill_data_coordinates(self): # 610 to 1950 = 1340 - 76 / 20 = 63 # 40 to 1380 = 1340 - 76 / 20 = 63 @@ -142,4 +186,7 @@ class NewStoneID(threading.Thread): i_spacing = 4 for e in range(0, 4, 1): for i in range(0, 4, 1): - self.stone_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim] \ No newline at end of file + self.stone_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim] + for e in range(0, 20, 1): + for i in range(0, 20, 1): + self.data_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim] \ No newline at end of file