added first draft litris
This commit is contained in:
71
litris.py
71
litris.py
@@ -73,6 +73,18 @@ class Litris(GameBase):
|
|||||||
# 2: cv.imread("sodoku/2.jpg", cv.IMREAD_COLOR),
|
# 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):
|
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
|
||||||
@@ -94,7 +106,7 @@ class Litris(GameBase):
|
|||||||
# cv.waitKey(2000)
|
# cv.waitKey(2000)
|
||||||
current_stone = self.new_stone_detection_and_identification()
|
current_stone = self.new_stone_detection_and_identification()
|
||||||
new_observation, new_screenshot = self.get_current_board_state()
|
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)
|
self.move_stone(col)
|
||||||
|
|
||||||
|
|
||||||
@@ -103,8 +115,8 @@ class Litris(GameBase):
|
|||||||
|
|
||||||
def get_current_board_state(self):
|
def get_current_board_state(self):
|
||||||
# get an updated image of the game
|
# get an updated image of the game
|
||||||
#screenshot = self.capture_window.get_screenshot()
|
screenshot = self.capture_window.get_screenshot()
|
||||||
screenshot = cv.imread("litris/main_playfield.jpg")
|
#screenshot = cv.imread("litris/main_playfield.jpg")
|
||||||
|
|
||||||
# gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY)
|
# gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY)
|
||||||
# thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
|
# 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))
|
# self.change_value(x, y, int(needle_key))
|
||||||
# print(field.data_value_grid)
|
# print(field.data_value_grid)
|
||||||
#cv.circle(screenshot, points[0], 7, (0, 255, 0), -1)
|
#cv.circle(screenshot, points[0], 7, (0, 255, 0), -1)
|
||||||
output_image = self.vision_stun.draw_rectangles(screenshot, rectangles)
|
#output_image = self.vision_stun.draw_rectangles(screenshot, rectangles)
|
||||||
cv.imshow("output_image", output_image)
|
#cv.imshow("output_image", output_image)
|
||||||
cv.waitKey(150)
|
#cv.waitKey(150)
|
||||||
return data_coords, screenshot
|
return data_coords, screenshot
|
||||||
|
|
||||||
def new_stone_detection_and_identification(self):
|
def new_stone_detection_and_identification(self):
|
||||||
stone_coords = np.zeros((4, 4), dtype=object)
|
stone_coords = np.zeros((4, 4), dtype=object)
|
||||||
while True:
|
while True:
|
||||||
# screenshot = self.capture_window.get_screenshot()
|
screenshot = self.capture_window.get_screenshot()
|
||||||
screenshot = cv.imread("litris/main_playfield.jpg")
|
#screenshot = cv.imread("litris/main_playfield.jpg")
|
||||||
# 1148 1412 580 845
|
# 1148 1412 580 845
|
||||||
screenshot = screenshot[580:845, 1148:1412]
|
screenshot = screenshot[580:845, 1148:1412]
|
||||||
#cv.imshow("screenshot", screenshot)
|
#cv.imshow("screenshot", screenshot)
|
||||||
@@ -170,25 +182,46 @@ class Litris(GameBase):
|
|||||||
return stone_coords
|
return stone_coords
|
||||||
|
|
||||||
def find_place_for_stone(self, stone, current_board):
|
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):
|
def move_stone(self, col_movement):
|
||||||
keyboard.press('down')
|
keyboard.press('Down')
|
||||||
cv.waitKey(100)
|
cv.waitKey(500)
|
||||||
if col_movement < 0:
|
if col_movement < 0:
|
||||||
for i in range(0, col_movement, - 1):
|
for i in range(0, col_movement, - 1):
|
||||||
keyboard.press('left')
|
keyboard.press('Left')
|
||||||
cv.waitKey(100)
|
cv.waitKey(500)
|
||||||
else:
|
else:
|
||||||
for i in range(0, col_movement, 1):
|
for i in range(0, col_movement, 1):
|
||||||
keyboard.press('right')
|
keyboard.press('Right')
|
||||||
cv.waitKey(100)
|
cv.waitKey(500)
|
||||||
|
|
||||||
def point_in_rect(self, point):
|
def point_in_rect(self, point):
|
||||||
for e in range(0, 20, 1):
|
for e in range(0, 20, 1):
|
||||||
|
|||||||
Reference in New Issue
Block a user