199 lines
6.5 KiB
Python
199 lines
6.5 KiB
Python
import cv2 as cv
|
|
import numpy as np
|
|
from time import time
|
|
from game_base_class import GameBase
|
|
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
|
|
|
|
class Litris(GameBase):
|
|
|
|
def __init__(self, overlay):
|
|
super().__init__(overlay)
|
|
|
|
self.keyboard = Controller()
|
|
|
|
self.offset_left = 610
|
|
self.offset_down = 40
|
|
|
|
self.field = Field()
|
|
|
|
self.litris_reset_board = cv.imread("control_elements/sodoku_reset_button.jpg", cv.IMREAD_COLOR)
|
|
|
|
self.stone_id_thread = NewStoneID()
|
|
|
|
self.move_mode = 1
|
|
|
|
def assess_playfield_and_make_move(self):
|
|
last_letter_received = time()
|
|
while True:
|
|
if self.stone_id_thread.get_pick_up_status() == False:
|
|
if (time() - last_letter_received) >= 5:
|
|
self.field.reset_field()
|
|
self.field.cleared_rows = 1
|
|
last_letter_received = time()
|
|
self.dig_point(1500, 980, 100)
|
|
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
|
|
return
|
|
continue
|
|
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
|
|
return
|
|
cv.waitKey(50)
|
|
continue
|
|
|
|
|
|
current_letter = self.stone_id_thread.get_actual_letter()
|
|
print("current_letter: ", current_letter)
|
|
last_letter_received = time()
|
|
|
|
|
|
current_tetromino = Tetromino.create(current_letter)
|
|
if self.move_mode == 2:
|
|
current_tetromino.rotate(3)
|
|
elif self.move_mode == 3:
|
|
current_tetromino.rotate(2)
|
|
elif self.move_mode == 4:
|
|
current_tetromino.rotate(1)
|
|
|
|
|
|
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, self.move_mode)
|
|
print("offset column:", offset_col)
|
|
self.field.drop(current_tetromino, column)
|
|
self.move_stone(column - offset_col, rotation)
|
|
self.drop_down()
|
|
print(self.field)
|
|
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()
|
|
cv.waitKey(200)
|
|
self.field.state = self.stone_id_thread.get_current_board_state()
|
|
self.field.rotate_state()
|
|
#self.update_move_mode()
|
|
self.field.reset_half_field()
|
|
#field_mem = copy(self.field)
|
|
#self.field = copy(self.field_mem)
|
|
#self.field_mem = copy(field_mem)
|
|
|
|
self.field.cleared_rows = 1
|
|
|
|
#cv.waitKey(200)
|
|
self.stone_id_thread.set_pick_up_status(False)
|
|
|
|
|
|
|
|
|
|
def drop_down(self):
|
|
if self.move_mode == 1:
|
|
down = Key.down
|
|
left = Key.left
|
|
right = Key.right
|
|
elif self.move_mode == 2:
|
|
down = Key.left
|
|
left = Key.up
|
|
right = Key.down
|
|
elif self.move_mode == 3:
|
|
down = Key.up
|
|
left = Key.right
|
|
right = Key.left
|
|
elif self.move_mode == 4:
|
|
down = Key.right
|
|
left = Key.down
|
|
right = Key.up
|
|
for i in range(1, 10, 1):
|
|
self.keyboard.press(down)
|
|
self.keyboard.release(down)
|
|
print("drop down pressed:", down)
|
|
cv.waitKey(50)
|
|
|
|
def update_move_mode(self):
|
|
if self.move_mode <=3:
|
|
self.move_mode = self.move_mode + 1
|
|
elif self.move_mode == 4:
|
|
self.move_mode = 1
|
|
|
|
def move_stone(self, col_movement, rotation):
|
|
if col_movement is None:
|
|
return
|
|
|
|
if self.move_mode == 1:
|
|
down = Key.down
|
|
left = Key.left
|
|
right = Key.right
|
|
elif self.move_mode == 2:
|
|
down = Key.left
|
|
left = Key.up
|
|
right = Key.down
|
|
elif self.move_mode == 3:
|
|
down = Key.up
|
|
left = Key.right
|
|
right = Key.left
|
|
elif self.move_mode == 4:
|
|
down = Key.right
|
|
left = Key.down
|
|
right = Key.up
|
|
|
|
# Press and release space
|
|
self.keyboard.press(down)
|
|
self.keyboard.release(down)
|
|
print("direction pressed: ", down)
|
|
cv.waitKey(120)
|
|
|
|
if rotation == 3:
|
|
self.keyboard.press('e')
|
|
self.keyboard.release('e')
|
|
print("rotation 1 pressed: e")
|
|
cv.waitKey(40)
|
|
elif rotation == 2:
|
|
self.keyboard.press('e')
|
|
self.keyboard.release('e')
|
|
print("rotation 2 pressed: e 1")
|
|
cv.waitKey(30)
|
|
self.keyboard.press('e')
|
|
self.keyboard.release('e')
|
|
print("rotation 2 pressed: e 2")
|
|
cv.waitKey(30)
|
|
elif rotation == 1:
|
|
self.keyboard.press('e')
|
|
self.keyboard.release('e')
|
|
print("rotation 3 pressed: e 1")
|
|
cv.waitKey(20)
|
|
self.keyboard.press('e')
|
|
self.keyboard.release('e')
|
|
print("rotation 3 pressed: e 2")
|
|
cv.waitKey(20)
|
|
self.keyboard.press('e')
|
|
self.keyboard.release('e')
|
|
print("rotation 3 pressed: e 3")
|
|
cv.waitKey(20)
|
|
|
|
if col_movement < 0:
|
|
for i in range(0, col_movement, - 1):
|
|
self.keyboard.press(left)
|
|
self.keyboard.release(left)
|
|
print("move left 3 pressed:", left)
|
|
cv.waitKey(40)
|
|
else:
|
|
for i in range(0, col_movement, 1):
|
|
self.keyboard.press(right)
|
|
self.keyboard.release(right)
|
|
print("move right 3 pressed:", right)
|
|
cv.waitKey(40)
|
|
|
|
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 |