|
|
|
|
@ -1,9 +1,8 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
import random
|
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
|
|
from .utils import Coord, Movement, Rotation, T_Spin
|
|
|
|
|
from .tetromino import Tetromino, T_Tetrimino, I_Tetrimino
|
|
|
|
|
from .utils import Coord, Movement, Rotation, T_Spin, Phase
|
|
|
|
|
from .tetromino import Tetromino, T_Tetrimino
|
|
|
|
|
from .consts import (
|
|
|
|
|
LINES,
|
|
|
|
|
COLLUMNS,
|
|
|
|
|
@ -13,8 +12,6 @@ from .consts import (
|
|
|
|
|
AUTOREPEAT_DELAY,
|
|
|
|
|
AUTOREPEAT_PERIOD,
|
|
|
|
|
MATRIX_PIECE_COORD,
|
|
|
|
|
NEXT_PIECE_COORDS,
|
|
|
|
|
HELD_PIECE_COORD,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -22,16 +19,7 @@ LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
|
|
|
|
|
CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class State:
|
|
|
|
|
|
|
|
|
|
STARTING = "STARTING"
|
|
|
|
|
PLAYING = "PLAYING"
|
|
|
|
|
PAUSED = "PAUSED"
|
|
|
|
|
OVER = "OVER"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PieceContainer:
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.piece = None
|
|
|
|
|
|
|
|
|
|
@ -41,12 +29,12 @@ class HoldQueue(PieceContainer):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Matrix(list, PieceContainer):
|
|
|
|
|
|
|
|
|
|
def __init__(self, lines, collumns):
|
|
|
|
|
list.__init__(self)
|
|
|
|
|
PieceContainer.__init__(self)
|
|
|
|
|
self.lines = lines
|
|
|
|
|
self.collumns = collumns
|
|
|
|
|
self.ghost = None
|
|
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
|
self.clear()
|
|
|
|
|
@ -57,11 +45,12 @@ class Matrix(list, PieceContainer):
|
|
|
|
|
self.append([None for x in range(self.collumns)])
|
|
|
|
|
|
|
|
|
|
def cell_is_free(self, coord):
|
|
|
|
|
return 0 <= coord.x < self.collumns and 0 <= coord.y and not self[coord.y][coord.x]
|
|
|
|
|
return (
|
|
|
|
|
0 <= coord.x < self.collumns and 0 <= coord.y and not self[coord.y][coord.x]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NextQueue(PieceContainer):
|
|
|
|
|
|
|
|
|
|
def __init__(self, nb_pieces):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.nb_pieces = nb_pieces
|
|
|
|
|
@ -70,6 +59,14 @@ class NextQueue(PieceContainer):
|
|
|
|
|
|
|
|
|
|
class Stats:
|
|
|
|
|
|
|
|
|
|
SCORES = (
|
|
|
|
|
{LINES_CLEAR_NAME: "", T_Spin.NONE: 0, T_Spin.MINI: 1, T_Spin.T_SPIN: 4},
|
|
|
|
|
{LINES_CLEAR_NAME: "SINGLE", T_Spin.NONE: 1, T_Spin.MINI: 2, T_Spin.T_SPIN: 8},
|
|
|
|
|
{LINES_CLEAR_NAME: "DOUBLE", T_Spin.NONE: 3, T_Spin.T_SPIN: 12},
|
|
|
|
|
{LINES_CLEAR_NAME: "TRIPLE", T_Spin.NONE: 5, T_Spin.T_SPIN: 16},
|
|
|
|
|
{LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _get_score(self):
|
|
|
|
|
return self._score
|
|
|
|
|
|
|
|
|
|
@ -106,6 +103,32 @@ class Stats:
|
|
|
|
|
def update_time(self):
|
|
|
|
|
self.time += 1
|
|
|
|
|
|
|
|
|
|
def pattern_phase(self, t_spin, lines_cleared):
|
|
|
|
|
pattern_name = []
|
|
|
|
|
pattern_score = 0
|
|
|
|
|
combo_score = 0
|
|
|
|
|
|
|
|
|
|
if t_spin:
|
|
|
|
|
pattern_name.append(t_spin)
|
|
|
|
|
if lines_cleared:
|
|
|
|
|
pattern_name.append(self.SCORES[lines_cleared][LINES_CLEAR_NAME])
|
|
|
|
|
self.combo += 1
|
|
|
|
|
else:
|
|
|
|
|
self.combo = -1
|
|
|
|
|
|
|
|
|
|
if lines_cleared or t_spin:
|
|
|
|
|
pattern_score = self.SCORES[lines_cleared][t_spin]
|
|
|
|
|
self.goal -= pattern_score
|
|
|
|
|
pattern_score *= 100 * self.level
|
|
|
|
|
pattern_name = "\n".join(pattern_name)
|
|
|
|
|
|
|
|
|
|
if self.combo >= 1:
|
|
|
|
|
combo_score = (20 if lines_cleared == 1 else 50) * self.combo * self.level
|
|
|
|
|
|
|
|
|
|
self.score += pattern_score + combo_score
|
|
|
|
|
|
|
|
|
|
return pattern_name, pattern_score, self.combo, combo_score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TetrisLogic:
|
|
|
|
|
|
|
|
|
|
@ -115,22 +138,13 @@ class TetrisLogic:
|
|
|
|
|
AUTOREPEAT_DELAY = AUTOREPEAT_DELAY
|
|
|
|
|
AUTOREPEAT_PERIOD = AUTOREPEAT_PERIOD
|
|
|
|
|
MATRIX_PIECE_COORD = MATRIX_PIECE_COORD
|
|
|
|
|
NEXT_PIECE_COORDS = NEXT_PIECE_COORDS
|
|
|
|
|
HELD_PIECE_COORD = HELD_PIECE_COORD
|
|
|
|
|
random_bag = []
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
lines=LINES,
|
|
|
|
|
collumns=COLLUMNS,
|
|
|
|
|
next_pieces=NEXT_PIECES,
|
|
|
|
|
):
|
|
|
|
|
def __init__(self, lines=LINES, collumns=COLLUMNS, next_pieces=NEXT_PIECES):
|
|
|
|
|
self.stats = Stats()
|
|
|
|
|
self.load_high_score()
|
|
|
|
|
self.state = State.STARTING
|
|
|
|
|
self.phase = Phase.STARTING
|
|
|
|
|
self.held = HoldQueue()
|
|
|
|
|
self.matrix = Matrix(lines, collumns)
|
|
|
|
|
self.matrix.ghost = None
|
|
|
|
|
self.next = NextQueue(next_pieces)
|
|
|
|
|
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
|
|
|
|
self.pressed_actions = []
|
|
|
|
|
@ -142,35 +156,19 @@ class TetrisLogic:
|
|
|
|
|
self.auto_repeat = False
|
|
|
|
|
|
|
|
|
|
self.matrix.reset()
|
|
|
|
|
self.next.pieces = [self.new_piece() for n in range(self.next.nb_pieces)]
|
|
|
|
|
self.next.pieces = [Tetromino() for n in range(self.next.nb_pieces)]
|
|
|
|
|
self.held.piece = None
|
|
|
|
|
self.state = State.PLAYING
|
|
|
|
|
self.start(self.stats.update_time, 1)
|
|
|
|
|
|
|
|
|
|
self.on_new_game()
|
|
|
|
|
|
|
|
|
|
self.on_new_game(self.next.pieces)
|
|
|
|
|
self.new_level()
|
|
|
|
|
|
|
|
|
|
def on_new_game(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def new_piece(self):
|
|
|
|
|
if not self.random_bag:
|
|
|
|
|
self.random_bag = list(Tetromino.shapes)
|
|
|
|
|
random.shuffle(self.random_bag)
|
|
|
|
|
piece = self.random_bag.pop()()
|
|
|
|
|
self.on_new_piece(piece)
|
|
|
|
|
return piece
|
|
|
|
|
|
|
|
|
|
def on_new_piece(self, piece):
|
|
|
|
|
def on_new_game(self, next_pieces):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def new_level(self):
|
|
|
|
|
self.stats.new_level()
|
|
|
|
|
self.restart(self.fall, self.stats.fall_delay)
|
|
|
|
|
|
|
|
|
|
self.on_new_level(self.stats.level)
|
|
|
|
|
|
|
|
|
|
self.generation_phase()
|
|
|
|
|
|
|
|
|
|
def on_new_level(self, level):
|
|
|
|
|
@ -178,122 +176,133 @@ class TetrisLogic:
|
|
|
|
|
|
|
|
|
|
def generation_phase(self):
|
|
|
|
|
self.matrix.piece = self.next.pieces.pop(0)
|
|
|
|
|
self.next.pieces.append(Tetromino())
|
|
|
|
|
self.matrix.piece.coord = self.MATRIX_PIECE_COORD
|
|
|
|
|
self.matrix.ghost = self.matrix.piece.ghost()
|
|
|
|
|
self.move_ghost()
|
|
|
|
|
self.next.pieces.append(self.new_piece())
|
|
|
|
|
self.next.pieces[-1].coord = self.NEXT_PIECE_COORDS[-1]
|
|
|
|
|
for tetromino, coord in zip(self.next.pieces, self.NEXT_PIECE_COORDS):
|
|
|
|
|
tetromino.coord = coord
|
|
|
|
|
|
|
|
|
|
self.on_generation_phase(self.matrix.piece)
|
|
|
|
|
self.on_falling_phase()
|
|
|
|
|
|
|
|
|
|
if not self.can_move(self.matrix.piece.coord, (mino.coord for mino in self.matrix.piece)):
|
|
|
|
|
self.on_generation_phase(
|
|
|
|
|
self.matrix, self.matrix.piece, self.matrix.ghost, self.next.pieces
|
|
|
|
|
)
|
|
|
|
|
if not self.move(Movement.DOWN):
|
|
|
|
|
self.game_over()
|
|
|
|
|
else:
|
|
|
|
|
self.restart(self.fall, self.stats.fall_delay)
|
|
|
|
|
self.falling_phase()
|
|
|
|
|
|
|
|
|
|
def on_generation_phase(self, piece):
|
|
|
|
|
def on_generation_phase(self, matrix, falling_piece, ghost_piece, next_pieces):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def on_falling_phase(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def move_left(self):
|
|
|
|
|
self.move(Movement.LEFT)
|
|
|
|
|
|
|
|
|
|
def move_right(self):
|
|
|
|
|
self.move(Movement.RIGHT)
|
|
|
|
|
|
|
|
|
|
def rotate_clockwise(self):
|
|
|
|
|
self.rotate(Rotation.CLOCKWISE)
|
|
|
|
|
|
|
|
|
|
def rotate_counter(self):
|
|
|
|
|
self.rotate(Rotation.COUNTER)
|
|
|
|
|
|
|
|
|
|
def move_ghost(self):
|
|
|
|
|
def falling_phase(self):
|
|
|
|
|
self.phase = Phase.FALLING
|
|
|
|
|
self.matrix.ghost.coord = self.matrix.piece.coord
|
|
|
|
|
for ghost_mino, current_mino in zip(self.matrix.ghost, self.matrix.piece):
|
|
|
|
|
ghost_mino.coord = current_mino.coord
|
|
|
|
|
while self.can_move(self.matrix.ghost.coord + Movement.DOWN, (mino.coord for mino in self.matrix.ghost)):
|
|
|
|
|
while self.space_to_move(
|
|
|
|
|
self.matrix.ghost.coord + Movement.DOWN,
|
|
|
|
|
(mino.coord for mino in self.matrix.ghost),
|
|
|
|
|
):
|
|
|
|
|
self.matrix.ghost.coord += Movement.DOWN
|
|
|
|
|
|
|
|
|
|
def soft_drop(self):
|
|
|
|
|
moved = self.move(Movement.DOWN)
|
|
|
|
|
if moved:
|
|
|
|
|
self.stats.score += 1
|
|
|
|
|
return moved
|
|
|
|
|
self.on_falling_phase(self.matrix.piece, self.matrix.ghost)
|
|
|
|
|
|
|
|
|
|
def hard_drop(self):
|
|
|
|
|
while self.move(Movement.DOWN, prelock=False):
|
|
|
|
|
self.stats.score += 2
|
|
|
|
|
self.lock()
|
|
|
|
|
def on_falling_phase(self, falling_piece, ghost_piece):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def fall(self):
|
|
|
|
|
self.move(Movement.DOWN)
|
|
|
|
|
|
|
|
|
|
def move(self, movement, prelock=True):
|
|
|
|
|
def move(self, movement, rotated_coords=None, lock=True):
|
|
|
|
|
potential_coord = self.matrix.piece.coord + movement
|
|
|
|
|
if self.can_move(potential_coord, (mino.coord for mino in self.matrix.piece)):
|
|
|
|
|
if self.matrix.piece.prelocked:
|
|
|
|
|
self.restart(self.lock, self.stats.lock_delay)
|
|
|
|
|
if self.space_to_move(
|
|
|
|
|
potential_coord,
|
|
|
|
|
rotated_coords or (mino.coord for mino in self.matrix.piece),
|
|
|
|
|
):
|
|
|
|
|
self.matrix.piece.coord = potential_coord
|
|
|
|
|
if not movement == Movement.DOWN:
|
|
|
|
|
self.matrix.piece.last_rotation_point = None
|
|
|
|
|
self.move_ghost()
|
|
|
|
|
self.on_moved(movement)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
if prelock and not self.matrix.piece.prelocked and movement == Movement.DOWN:
|
|
|
|
|
self.matrix.piece.prelocked = True
|
|
|
|
|
self.start(self.lock, self.stats.lock_delay)
|
|
|
|
|
self.on_lock_phase()
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def on_moved(self, movement):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def rotate(self, rotation):
|
|
|
|
|
rotated_coords = tuple(Coord(rotation * mino.coord.y, -rotation * mino.coord.x) for mino in self.matrix.piece)
|
|
|
|
|
for rotation_point, liberty_degree in enumerate(self.matrix.piece.SRS[rotation][self.matrix.piece.orientation], start=1):
|
|
|
|
|
potential_coord = self.matrix.piece.coord + liberty_degree
|
|
|
|
|
if self.can_move(potential_coord, rotated_coords):
|
|
|
|
|
if self.matrix.piece.prelocked:
|
|
|
|
|
self.restart(self.lock, self.stats.lock_delay)
|
|
|
|
|
self.matrix.piece.coord = potential_coord
|
|
|
|
|
if rotated_coords:
|
|
|
|
|
for mino, coord in zip(self.matrix.piece, rotated_coords):
|
|
|
|
|
mino.coord = coord
|
|
|
|
|
self.matrix.piece.orientation = (self.matrix.piece.orientation + rotation) % 4
|
|
|
|
|
else:
|
|
|
|
|
if movement != Movement.DOWN:
|
|
|
|
|
self.matrix.piece.last_rotation_point = None
|
|
|
|
|
if self.phase == Phase.LOCK:
|
|
|
|
|
self.restart(self.pattern_phase, self.stats.lock_delay)
|
|
|
|
|
self.falling_phase()
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
if lock and self.phase != Phase.LOCK and movement == Movement.DOWN:
|
|
|
|
|
self.lock_phase()
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def lock_phase(self):
|
|
|
|
|
self.phase = Phase.LOCK
|
|
|
|
|
self.on_lock_phase(self.matrix.piece)
|
|
|
|
|
self.start(self.pattern_phase, self.stats.lock_delay)
|
|
|
|
|
|
|
|
|
|
def on_lock_phase(self, locked_piece):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def space_to_move(self, potential_coord, minoes_coord):
|
|
|
|
|
return all(
|
|
|
|
|
self.matrix.cell_is_free(potential_coord + mino_coord)
|
|
|
|
|
for mino_coord in minoes_coord
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def rotate(self, rotation):
|
|
|
|
|
rotated_coords = tuple(
|
|
|
|
|
Coord(rotation * mino.coord.y, -rotation * mino.coord.x)
|
|
|
|
|
for mino in self.matrix.piece
|
|
|
|
|
)
|
|
|
|
|
for rotation_point, liberty_degree in enumerate(
|
|
|
|
|
self.matrix.piece.SRS[rotation][self.matrix.piece.orientation], start=1
|
|
|
|
|
):
|
|
|
|
|
if self.move(liberty_degree, rotated_coords):
|
|
|
|
|
self.matrix.piece.orientation = (
|
|
|
|
|
self.matrix.piece.orientation + rotation
|
|
|
|
|
) % 4
|
|
|
|
|
self.matrix.piece.last_rotation_point = rotation_point
|
|
|
|
|
self.move_ghost()
|
|
|
|
|
self.on_rotated(rotation)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def on_rotated(self, direction):
|
|
|
|
|
def hold(self):
|
|
|
|
|
if not self.matrix.piece.hold_enabled:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.matrix.piece.hold_enabled = False
|
|
|
|
|
self.stop(self.pattern_phase)
|
|
|
|
|
self.stop(self.fall)
|
|
|
|
|
self.matrix.piece, self.held.piece = self.held.piece, self.matrix.piece
|
|
|
|
|
|
|
|
|
|
self.on_hold(self.held.piece)
|
|
|
|
|
|
|
|
|
|
if self.matrix.piece:
|
|
|
|
|
self.matrix.piece.coord = self.MATRIX_PIECE_COORD
|
|
|
|
|
self.matrix.ghost = self.matrix.piece.ghost()
|
|
|
|
|
self.falling_phase()
|
|
|
|
|
else:
|
|
|
|
|
self.generation_phase()
|
|
|
|
|
|
|
|
|
|
def on_hold(self, held_piece):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def on_lock_phase(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
SCORES = (
|
|
|
|
|
{LINES_CLEAR_NAME: "", T_Spin.NONE: 0, T_Spin.MINI: 1, T_Spin.T_SPIN: 4},
|
|
|
|
|
{LINES_CLEAR_NAME: "SINGLE", T_Spin.NONE: 1, T_Spin.MINI: 2, T_Spin.T_SPIN: 8},
|
|
|
|
|
{LINES_CLEAR_NAME: "DOUBLE", T_Spin.NONE: 3, T_Spin.T_SPIN: 12},
|
|
|
|
|
{LINES_CLEAR_NAME: "TRIPLE", T_Spin.NONE: 5, T_Spin.T_SPIN: 16},
|
|
|
|
|
{LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def lock(self):
|
|
|
|
|
def pattern_phase(self):
|
|
|
|
|
self.phase = Phase.PATTERN
|
|
|
|
|
self.matrix.piece.prelocked = False
|
|
|
|
|
self.stop(self.lock)
|
|
|
|
|
self.stop(self.pattern_phase)
|
|
|
|
|
self.stop(self.fall)
|
|
|
|
|
|
|
|
|
|
# Piece unlocked
|
|
|
|
|
if self.can_move(self.matrix.piece.coord + Movement.DOWN, (mino.coord for mino in self.matrix.piece)):
|
|
|
|
|
if self.space_to_move(
|
|
|
|
|
self.matrix.piece.coord + Movement.DOWN,
|
|
|
|
|
(mino.coord for mino in self.matrix.piece),
|
|
|
|
|
):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Game over
|
|
|
|
|
if all((mino.coord + self.matrix.piece.coord).y >= self.matrix.lines for mino in self.matrix.piece):
|
|
|
|
|
if all(
|
|
|
|
|
(mino.coord + self.matrix.piece.coord).y >= self.matrix.lines
|
|
|
|
|
for mino in self.matrix.piece
|
|
|
|
|
):
|
|
|
|
|
self.game_over()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
@ -305,13 +314,13 @@ class TetrisLogic:
|
|
|
|
|
coord = mino.coord + self.matrix.piece.coord
|
|
|
|
|
if coord.y <= self.matrix.lines + 3:
|
|
|
|
|
self.matrix[coord.y][coord.x] = mino
|
|
|
|
|
self.on_locked(self.matrix.piece)
|
|
|
|
|
self.on_locked(self.matrix, self.matrix.piece)
|
|
|
|
|
|
|
|
|
|
self.pattern_phase()
|
|
|
|
|
|
|
|
|
|
def pattern_phase(self):
|
|
|
|
|
# T-Spin
|
|
|
|
|
if type(self.matrix.piece) == T_Tetrimino and self.matrix.piece.last_rotation_point is not None:
|
|
|
|
|
if (
|
|
|
|
|
type(self.matrix.piece) == T_Tetrimino
|
|
|
|
|
and self.matrix.piece.last_rotation_point is not None
|
|
|
|
|
):
|
|
|
|
|
a = self.is_t_slot(0)
|
|
|
|
|
b = self.is_t_slot(1)
|
|
|
|
|
c = self.is_t_slot(3)
|
|
|
|
|
@ -330,38 +339,16 @@ class TetrisLogic:
|
|
|
|
|
for y, line in reversed(list(enumerate(self.matrix))):
|
|
|
|
|
if all(mino for mino in line):
|
|
|
|
|
lines_cleared += 1
|
|
|
|
|
self.on_line_remove(y)
|
|
|
|
|
self.on_line_remove(self.matrix, y)
|
|
|
|
|
self.matrix.pop(y)
|
|
|
|
|
self.matrix.append_new_line()
|
|
|
|
|
if lines_cleared:
|
|
|
|
|
self.stats.lines_cleared += lines_cleared
|
|
|
|
|
|
|
|
|
|
# Scoring
|
|
|
|
|
lock_strings = []
|
|
|
|
|
lock_score = 0
|
|
|
|
|
|
|
|
|
|
if t_spin:
|
|
|
|
|
lock_strings.append(t_spin)
|
|
|
|
|
if lines_cleared:
|
|
|
|
|
lock_strings.append(self.SCORES[lines_cleared][LINES_CLEAR_NAME])
|
|
|
|
|
self.stats.combo += 1
|
|
|
|
|
else:
|
|
|
|
|
self.stats.combo = -1
|
|
|
|
|
|
|
|
|
|
if lines_cleared or t_spin:
|
|
|
|
|
ds = self.SCORES[lines_cleared][t_spin]
|
|
|
|
|
self.stats.goal -= ds
|
|
|
|
|
ds *= 100 * self.stats.level
|
|
|
|
|
lock_score += ds
|
|
|
|
|
lock_strings.append(str(ds))
|
|
|
|
|
self.show_text("\n".join(lock_strings))
|
|
|
|
|
|
|
|
|
|
if self.stats.combo >= 1:
|
|
|
|
|
ds = (20 if lines_cleared == 1 else 50) * self.stats.combo * self.stats.level
|
|
|
|
|
lock_score += ds
|
|
|
|
|
self.show_text("COMBO x{:n}\n{:n}".format(self.stats.combo, ds))
|
|
|
|
|
|
|
|
|
|
self.stats.score += lock_score
|
|
|
|
|
pattern_name, pattern_score, nb_combo, combo_score = self.stats.pattern_phase(
|
|
|
|
|
t_spin, lines_cleared
|
|
|
|
|
)
|
|
|
|
|
self.on_pattern_phase(pattern_name, pattern_score, nb_combo, combo_score)
|
|
|
|
|
|
|
|
|
|
if self.stats.goal <= 0:
|
|
|
|
|
self.new_level()
|
|
|
|
|
@ -371,62 +358,73 @@ class TetrisLogic:
|
|
|
|
|
if self.pressed_actions:
|
|
|
|
|
self.start(self.repeat_action, self.AUTOREPEAT_DELAY)
|
|
|
|
|
|
|
|
|
|
def on_locked(piece):
|
|
|
|
|
def on_locked(self, matrix, locked_piece):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def on_line_remove(self, y):
|
|
|
|
|
def on_line_remove(self, matrix, y):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def can_move(self, potential_coord, minoes_coords):
|
|
|
|
|
return all(self.matrix.cell_is_free(potential_coord + mino_coord) for mino_coord in minoes_coords)
|
|
|
|
|
def on_pattern_phase(self, pattern_name, pattern_score, nb_combo, combo_score):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def move_left(self):
|
|
|
|
|
self.move(Movement.LEFT)
|
|
|
|
|
|
|
|
|
|
def move_right(self):
|
|
|
|
|
self.move(Movement.RIGHT)
|
|
|
|
|
|
|
|
|
|
def rotate_clockwise(self):
|
|
|
|
|
self.rotate(Rotation.CLOCKWISE)
|
|
|
|
|
|
|
|
|
|
def rotate_counter(self):
|
|
|
|
|
self.rotate(Rotation.COUNTER)
|
|
|
|
|
|
|
|
|
|
def soft_drop(self):
|
|
|
|
|
moved = self.move(Movement.DOWN)
|
|
|
|
|
if moved:
|
|
|
|
|
self.stats.score += 1
|
|
|
|
|
return moved
|
|
|
|
|
|
|
|
|
|
def hard_drop(self):
|
|
|
|
|
while self.move(Movement.DOWN, lock=False):
|
|
|
|
|
self.stats.score += 2
|
|
|
|
|
self.pattern_phase()
|
|
|
|
|
|
|
|
|
|
T_SLOT_COORDS = (Coord(-1, 1), Coord(1, 1), Coord(-1, 1), Coord(-1, -1))
|
|
|
|
|
|
|
|
|
|
def is_t_slot(self, n):
|
|
|
|
|
t_slot_coord = self.matrix.piece.coord + self.T_SLOT_COORDS[(self.matrix.piece.orientation + n) % 4]
|
|
|
|
|
t_slot_coord = (
|
|
|
|
|
self.matrix.piece.coord
|
|
|
|
|
+ self.T_SLOT_COORDS[(self.matrix.piece.orientation + n) % 4]
|
|
|
|
|
)
|
|
|
|
|
return not self.matrix.cell_is_free(t_slot_coord)
|
|
|
|
|
|
|
|
|
|
def swap(self):
|
|
|
|
|
if self.matrix.piece.hold_enabled:
|
|
|
|
|
self.matrix.piece.hold_enabled = False
|
|
|
|
|
self.matrix.piece.prelocked = False
|
|
|
|
|
self.stop(self.lock)
|
|
|
|
|
self.matrix.piece, self.held.piece = self.held.piece, self.matrix.piece
|
|
|
|
|
self.held.piece.coord = self.HELD_PIECE_COORD
|
|
|
|
|
if type(self.held.piece) == I_Tetrimino:
|
|
|
|
|
self.held.piece.coord += Movement.LEFT
|
|
|
|
|
for mino, coord in zip(self.held.piece, self.held.piece.MINOES_COORDS):
|
|
|
|
|
mino.coord = coord
|
|
|
|
|
|
|
|
|
|
if self.matrix.piece:
|
|
|
|
|
self.matrix.piece.coord = self.MATRIX_PIECE_COORD
|
|
|
|
|
self.matrix.ghost = self.matrix.piece.ghost()
|
|
|
|
|
self.move_ghost()
|
|
|
|
|
else:
|
|
|
|
|
self.generation_phase()
|
|
|
|
|
|
|
|
|
|
def pause(self):
|
|
|
|
|
self.state = State.PAUSED
|
|
|
|
|
self.phase = Phase.PAUSED
|
|
|
|
|
self.stop_all()
|
|
|
|
|
self.pressed_actions = []
|
|
|
|
|
self.auto_repeat = False
|
|
|
|
|
self.stop(self.repeat_action)
|
|
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
|
self.state = State.PLAYING
|
|
|
|
|
self.phase = Phase.FALLING
|
|
|
|
|
self.start(self.fall, self.stats.fall_delay)
|
|
|
|
|
if self.matrix.piece.prelocked:
|
|
|
|
|
self.start(self.lock, self.stats.lock_delay)
|
|
|
|
|
if self.phase == Phase.LOCK:
|
|
|
|
|
self.start(self.pattern_phase, self.stats.lock_delay)
|
|
|
|
|
self.start(self.stats.update_time, 1)
|
|
|
|
|
|
|
|
|
|
def game_over(self):
|
|
|
|
|
self.state = State.OVER
|
|
|
|
|
self.phase = Phase.OVER
|
|
|
|
|
self.stop_all()
|
|
|
|
|
self.save_high_score()
|
|
|
|
|
self.on_game_over()
|
|
|
|
|
|
|
|
|
|
def on_game_over(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def stop_all(self):
|
|
|
|
|
self.stop(self.fall)
|
|
|
|
|
self.stop(self.lock)
|
|
|
|
|
self.stop(self.pattern_phase)
|
|
|
|
|
self.stop(self.stats.update_time)
|
|
|
|
|
|
|
|
|
|
def do_action(self, action):
|
|
|
|
|
|