update litris with board detection

This commit is contained in:
2023-07-29 20:12:51 +02:00
parent a9b76cde97
commit c9081bd371
2 changed files with 51 additions and 20 deletions

View File

@@ -15,18 +15,10 @@ class Litris(GameBase):
self.keyboard = Controller() 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_left = 610
self.offset_down = 40 self.offset_down = 40
self.fill_data_coordinates()
self.field = Field() self.field = Field()
self.field_mem = Field()
self.litris_reset_board = cv.imread("control_elements/sodoku_reset_button.jpg", cv.IMREAD_COLOR) 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 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): def assess_playfield_and_make_move(self):
last_letter_received = time() last_letter_received = time()
while True: while True:
@@ -92,6 +73,7 @@ class Litris(GameBase):
if self.field.get_line_count() >= 6 and self.field.height() <= 2: if self.field.get_line_count() >= 6 and self.field.height() <= 2:
if self.field.predict_gaps_in_next_rotation() <= 5: if self.field.predict_gaps_in_next_rotation() <= 5:
self.update_move_mode() self.update_move_mode()
self.field = self.stone_id_thread.get_current_board_state()
self.field.rotate_state() self.field.rotate_state()
#self.update_move_mode() #self.update_move_mode()
self.field.reset_half_field() self.field.reset_half_field()
@@ -105,6 +87,8 @@ class Litris(GameBase):
self.stone_id_thread.set_pick_up_status(False) self.stone_id_thread.set_pick_up_status(False)
def drop_down(self): def drop_down(self):
if self.move_mode == 1: if self.move_mode == 1:
down = Key.down down = Key.down

View File

@@ -26,6 +26,7 @@ class NewStoneID(threading.Thread):
self.vision_stun = Vision() self.vision_stun = Vision()
self.stone_coordinates = np.zeros((4, 4), dtype=object) self.stone_coordinates = np.zeros((4, 4), dtype=object)
self.data_coordinates = np.zeros((20, 20), dtype=object)
self.fill_data_coordinates() self.fill_data_coordinates()
self.needles = {1: cv.imread("litris/blue_needle.jpg", cv.IMREAD_UNCHANGED)} self.needles = {1: cv.imread("litris/blue_needle.jpg", cv.IMREAD_UNCHANGED)}
@@ -133,6 +134,49 @@ class NewStoneID(threading.Thread):
return e, i return e, i
return None, None 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): def fill_data_coordinates(self):
# 610 to 1950 = 1340 - 76 / 20 = 63 # 610 to 1950 = 1340 - 76 / 20 = 63
# 40 to 1380 = 1340 - 76 / 20 = 63 # 40 to 1380 = 1340 - 76 / 20 = 63
@@ -143,3 +187,6 @@ class NewStoneID(threading.Thread):
for e in range(0, 4, 1): for e in range(0, 4, 1):
for i 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] 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]