added async stone detection thread
This commit is contained in:
9
field.py
9
field.py
@@ -13,6 +13,7 @@ class Field():
|
|||||||
else:
|
else:
|
||||||
self.state = [[' ' for cols in range(Field.WIDTH)]
|
self.state = [[' ' for cols in range(Field.WIDTH)]
|
||||||
for rows in range(Field.HEIGHT)]
|
for rows in range(Field.HEIGHT)]
|
||||||
|
self.cleared_rows = 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
BAR = ' ' + '-' * (Field.WIDTH * 2 + 1) + '\n ' + \
|
BAR = ' ' + '-' * (Field.WIDTH * 2 + 1) + '\n ' + \
|
||||||
@@ -21,6 +22,13 @@ class Field():
|
|||||||
'{:2d} |'.format(i) + ' '.join(row) + '|'
|
'{:2d} |'.format(i) + ' '.join(row) + '|'
|
||||||
for i, row in enumerate(self.state)]) + '\n' + BAR
|
for i, row in enumerate(self.state)]) + '\n' + BAR
|
||||||
|
|
||||||
|
|
||||||
|
def rotate_state(self):
|
||||||
|
self.state = list(zip(*self.state[::-1]))
|
||||||
|
|
||||||
|
def get_line_count(self):
|
||||||
|
return self.cleared_rows
|
||||||
|
|
||||||
def _test_tetromino(self, tetromino, row, column):
|
def _test_tetromino(self, tetromino, row, column):
|
||||||
"""
|
"""
|
||||||
Tests to see if a tetromino can be placed at the specified row and
|
Tests to see if a tetromino can be placed at the specified row and
|
||||||
@@ -80,6 +88,7 @@ class Field():
|
|||||||
self.state = list(filter(lambda row: row.count(' ') != 0, self.state))
|
self.state = list(filter(lambda row: row.count(' ') != 0, self.state))
|
||||||
while len(self.state) < Field.HEIGHT:
|
while len(self.state) < Field.HEIGHT:
|
||||||
self.state.insert(0, [' ' for col in range(Field.WIDTH)])
|
self.state.insert(0, [' ' for col in range(Field.WIDTH)])
|
||||||
|
self.cleared_rows = self.cleared_rows + 1
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
48
litris.py
48
litris.py
@@ -45,6 +45,8 @@ class Litris(GameBase):
|
|||||||
|
|
||||||
self.stone_id_thread = NewStoneID()
|
self.stone_id_thread = NewStoneID()
|
||||||
|
|
||||||
|
self.move_mode = 1
|
||||||
|
|
||||||
|
|
||||||
def reset_field(self):
|
def reset_field(self):
|
||||||
self.state = [[' ' for cols in range(Field.WIDTH)]
|
self.state = [[' ' for cols in range(Field.WIDTH)]
|
||||||
@@ -93,14 +95,24 @@ class Litris(GameBase):
|
|||||||
offset_col = current_tetromino.get_offset_column(rotation)
|
offset_col = current_tetromino.get_offset_column(rotation)
|
||||||
print("offset column:", offset_col)
|
print("offset column:", offset_col)
|
||||||
self.field.drop(current_tetromino, column)
|
self.field.drop(current_tetromino, column)
|
||||||
#self.move_stone(column - offset_col, rotation)
|
self.move_stone(column - offset_col, rotation)
|
||||||
for i in range(1,5,1):
|
for i in range(1,8,1):
|
||||||
self.keyboard.press(Key.down)
|
self.keyboard.press(Key.down)
|
||||||
self.keyboard.release(Key.down)
|
self.keyboard.release(Key.down)
|
||||||
print("direction pressed: drop down")
|
print("direction pressed: drop down")
|
||||||
cv.waitKey(50)
|
cv.waitKey(50)
|
||||||
self.stone_id_thread.set_pick_up_status(False)
|
self.stone_id_thread.set_pick_up_status(False)
|
||||||
print(self.field)
|
print(self.field)
|
||||||
|
if self.field.get_line_count() % 5 == 0:
|
||||||
|
self.field.rotate_state()
|
||||||
|
self.update_move_mode()
|
||||||
|
|
||||||
|
|
||||||
|
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 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
|
||||||
@@ -241,12 +253,30 @@ class Litris(GameBase):
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def move_stone(self, col_movement, rotation):
|
def move_stone(self, col_movement, rotation, move_mode):
|
||||||
if col_movement is None:
|
if col_movement is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if move_mode == 1:
|
||||||
|
down = Key.down
|
||||||
|
left = Key.left
|
||||||
|
right = Key.right
|
||||||
|
elif move_mode == 2:
|
||||||
|
down = Key.left
|
||||||
|
left = Key.up
|
||||||
|
right = Key.down
|
||||||
|
elif move_mode == 3:
|
||||||
|
down = Key.up
|
||||||
|
left = Key.right
|
||||||
|
right = Key.left
|
||||||
|
elif move_mode == 4:
|
||||||
|
down = Key.right
|
||||||
|
left = Key.down
|
||||||
|
right = Key.up
|
||||||
|
|
||||||
# Press and release space
|
# Press and release space
|
||||||
self.keyboard.press(Key.down)
|
self.keyboard.press(down)
|
||||||
self.keyboard.release(Key.down)
|
self.keyboard.release(down)
|
||||||
print("direction pressed: down")
|
print("direction pressed: down")
|
||||||
cv.waitKey(120)
|
cv.waitKey(120)
|
||||||
|
|
||||||
@@ -280,14 +310,14 @@ class Litris(GameBase):
|
|||||||
|
|
||||||
if col_movement < 0:
|
if col_movement < 0:
|
||||||
for i in range(0, col_movement, - 1):
|
for i in range(0, col_movement, - 1):
|
||||||
self.keyboard.press(Key.left)
|
self.keyboard.press(left)
|
||||||
self.keyboard.release(Key.left)
|
self.keyboard.release(left)
|
||||||
print("move left 3 pressed")
|
print("move left 3 pressed")
|
||||||
cv.waitKey(40)
|
cv.waitKey(40)
|
||||||
else:
|
else:
|
||||||
for i in range(0, col_movement, 1):
|
for i in range(0, col_movement, 1):
|
||||||
self.keyboard.press(Key.right)
|
self.keyboard.press(right)
|
||||||
self.keyboard.release(Key.right)
|
self.keyboard.release(right)
|
||||||
print("move right 3 pressed")
|
print("move right 3 pressed")
|
||||||
cv.waitKey(40)
|
cv.waitKey(40)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user