added async stone detection thread
This commit is contained in:
61
litris.py
61
litris.py
@@ -5,6 +5,7 @@ from pynput.keyboard import Key, Controller
|
||||
from field import Field
|
||||
from tetromino import Tetromino
|
||||
from optimizer import Optimizer
|
||||
from litris_stone_id_thread import NewStoneID
|
||||
|
||||
O_FULL = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
|
||||
D_FULL = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
|
||||
@@ -42,6 +43,8 @@ class Litris(GameBase):
|
||||
|
||||
self.needles = {1: cv.imread("litris/blue_needle.jpg", cv.IMREAD_UNCHANGED)}
|
||||
|
||||
self.stone_id_thread = NewStoneID()
|
||||
|
||||
|
||||
def reset_field(self):
|
||||
self.state = [[' ' for cols in range(Field.WIDTH)]
|
||||
@@ -62,26 +65,42 @@ class Litris(GameBase):
|
||||
self.stone_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim]
|
||||
|
||||
def assess_playfield_and_make_move(self):
|
||||
current_letter = self.new_stone_detection_and_identification()
|
||||
#current_letter = self.stone_id()
|
||||
#current_letter = 'D'
|
||||
print("current_letter: ", current_letter)
|
||||
if current_letter is None:
|
||||
cv.waitKey(50)
|
||||
return
|
||||
|
||||
current_tetromino = Tetromino.create(current_letter)
|
||||
opt = Optimizer.get_optimal_drop(self.field, current_tetromino)
|
||||
rotation = opt['tetromino_rotation']
|
||||
column = opt['tetromino_column']
|
||||
print("Rota:", rotation)
|
||||
print("column:", column)
|
||||
current_tetromino.rotate(rotation)
|
||||
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)
|
||||
print(self.field)
|
||||
|
||||
while True:
|
||||
if self.stone_id_thread.get_pick_up_status() == False:
|
||||
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
|
||||
return
|
||||
cv.waitKey(50)
|
||||
continue
|
||||
|
||||
#current_letter = self.new_stone_detection_and_identification()
|
||||
#current_letter = self.stone_id()
|
||||
#current_letter = 'D'
|
||||
current_letter = self.stone_id_thread.get_actual_letter()
|
||||
#self.stone_id_thread.set_pick_up_status(False)
|
||||
print("current_letter: ", current_letter)
|
||||
#if current_letter is None:
|
||||
# cv.waitKey(50)
|
||||
# return
|
||||
|
||||
current_tetromino = Tetromino.create(current_letter)
|
||||
opt = Optimizer.get_optimal_drop(self.field, current_tetromino)
|
||||
rotation = opt['tetromino_rotation']
|
||||
column = opt['tetromino_column']
|
||||
print("Rota:", rotation)
|
||||
print("column:", column)
|
||||
current_tetromino.rotate(rotation)
|
||||
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.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)
|
||||
|
||||
def get_current_board_state(self):
|
||||
# get an updated image of the game
|
||||
@@ -151,7 +170,7 @@ class Litris(GameBase):
|
||||
|
||||
|
||||
def new_stone_detection_and_identification(self):
|
||||
stone_coords = np.zeros((2, 4), dtype=object)
|
||||
stone_coords = np.zeros((4, 4), dtype=object)
|
||||
fail_counter = 0
|
||||
while True:
|
||||
screenshot = self.capture_window.get_screenshot()
|
||||
|
||||
Reference in New Issue
Block a user