FIX T-SPIN DETECTION and clean up

This commit is contained in:
adrienmalin 2019-09-29 14:06:31 +02:00
parent 22198de8d4
commit 6e14bc08b3
2 changed files with 50 additions and 42 deletions

View File

@ -8,11 +8,10 @@ try:
import arcade import arcade
except ImportError: except ImportError:
sys.exit( sys.exit(
"""This game require arcade library. """This game require arcade library.
You can install it with: You can install it with:
python -m pip install --user arcade python -m pip install --user arcade"""
""" )
)
from tetrislogic import TetrisLogic, State, NB_LINES from tetrislogic import TetrisLogic, State, NB_LINES
@ -44,9 +43,11 @@ HARD DROP SPACE
ROTATE CLOCKWISE ROTATE CLOCKWISE
ROTATE COUNTER Z ROTATE COUNTER Z
HOLD C HOLD C
PAUSE ESC""" PAUSE ESC
START_TEXT = TITLE_AND_CONTROL_TEXT + "\n\nPRESS [ENTER] TO START"
PAUSE_TEXT = TITLE_AND_CONTROL_TEXT + "\n\nPRESS [ESC] TO RESUME" """
START_TEXT = TITLE_AND_CONTROL_TEXT + "PRESS [ENTER] TO START"
PAUSE_TEXT = TITLE_AND_CONTROL_TEXT + "PRESS [ESC] TO RESUME"
STATS_TEXT = """SCORE STATS_TEXT = """SCORE
HIGH SCORE HIGH SCORE
@ -130,9 +131,9 @@ class TetrominoSprites(arcade.SpriteList):
class TetrArcade(TetrisLogic, arcade.Window): class TetrArcade(TetrisLogic, arcade.Window):
def __init__(self): def __init__(self):
super().__init__()
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
self.highlight_texts = []
self.tasks = {}
self.KEY_MAP = { self.KEY_MAP = {
State.STARTING: { State.STARTING: {
@ -170,8 +171,7 @@ class TetrArcade(TetrisLogic, arcade.Window):
} }
} }
self.highlight_texts = [] super().__init__()
arcade.Window.__init__( arcade.Window.__init__(
self, self,
width = WINDOW_WIDTH, width = WINDOW_WIDTH,
@ -205,15 +205,16 @@ class TetrArcade(TetrisLogic, arcade.Window):
anchor_x = 'right' anchor_x = 'right'
) )
self.tasks = {}
self.new_game()
def new_game(self): def new_game(self):
self.highlight_texts = [] self.highlight_texts = []
self.matrix_minoes_sprites = []
super().new_game() super().new_game()
self.on_draw() self.on_draw()
def new_matrix(self):
self.matrix_minoes_sprites = []
super().new_matrix()
def new_next_pieces(self): def new_next_pieces(self):
super().new_next_pieces() super().new_next_pieces()
self.next_pieces_sprites = [ self.next_pieces_sprites = [

View File

@ -12,6 +12,8 @@ FALL_DELAY = 1
AUTOREPEAT_DELAY = 0.200 # Official : 0.300 AUTOREPEAT_DELAY = 0.200 # Official : 0.300
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010 AUTOREPEAT_PERIOD = 0.010 # Official : 0.010
LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
class Coord: class Coord:
@ -56,8 +58,8 @@ class Rotation:
class T_Spin: class T_Spin:
NO_T_SPIN = "" NONE = ""
MINI_T_SPIN = "MINI\nT-SPIN" MINI = "MINI\nT-SPIN"
T_SPIN = "T-SPIN" T_SPIN = "T-SPIN"
@ -219,19 +221,23 @@ class TetrisLogic():
self.lock_delay = LOCK_DELAY self.lock_delay = LOCK_DELAY
self.fall_delay = FALL_DELAY self.fall_delay = FALL_DELAY
self.matrix = [] self.new_matrix()
for y in range(NB_LINES+3):
self.append_new_line_to_matrix()
self.new_next_pieces() self.new_next_pieces()
self.current_piece = None self.current_piece = None
self.held_piece = None self.held_piece = None
self.state = State.PLAYING self.state = State.PLAYING
self.start(self.update_time, 1) self.start(self.update_time, 1)
self.new_level() self.new_level()
def new_next_pieces(self): def new_next_pieces(self):
self.next_pieces = [Tetromino() for i in range(NB_NEXT_PIECES)] self.next_pieces = [Tetromino() for i in range(NB_NEXT_PIECES)]
def new_matrix(self):
self.matrix = []
for y in range(NB_LINES+3):
self.append_new_line_to_matrix()
def new_level(self): def new_level(self):
self.level += 1 self.level += 1
self.goal += 5 * self.level self.goal += 5 * self.level
@ -241,6 +247,7 @@ class TetrisLogic():
self.lock_delay = 0.5 * pow(0.9, self.level-15) self.lock_delay = 0.5 * pow(0.9, self.level-15)
self.show_text("LEVEL\n{:n}".format(self.level)) self.show_text("LEVEL\n{:n}".format(self.level))
self.restart(self.fall, self.fall_delay) self.restart(self.fall, self.fall_delay)
self.new_current_piece() self.new_current_piece()
def new_current_piece(self): def new_current_piece(self):
@ -251,6 +258,7 @@ class TetrisLogic():
self.next_pieces.append(Tetromino()) self.next_pieces.append(Tetromino())
for piece, coord in zip (self.next_pieces, NEXT_PIECES_COORDS): for piece, coord in zip (self.next_pieces, NEXT_PIECES_COORDS):
piece.coord = coord piece.coord = coord
if not self.can_move( if not self.can_move(
self.current_piece.coord, self.current_piece.coord,
self.current_piece.minoes_coords self.current_piece.minoes_coords
@ -337,11 +345,11 @@ class TetrisLogic():
return False return False
SCORES = ( SCORES = (
{"name": "", T_Spin.NO_T_SPIN: 0, T_Spin.MINI_T_SPIN: 1, T_Spin.T_SPIN: 4}, {LINES_CLEAR_NAME: "", T_Spin.NONE: 0, T_Spin.MINI: 1, T_Spin.T_SPIN: 4},
{"name": "SINGLE", T_Spin.NO_T_SPIN: 1, T_Spin.MINI_T_SPIN: 2, T_Spin.T_SPIN: 8}, {LINES_CLEAR_NAME: "SINGLE", T_Spin.NONE: 1, T_Spin.MINI: 2, T_Spin.T_SPIN: 8},
{"name": "DOUBLE", T_Spin.NO_T_SPIN: 3, T_Spin.MINI_T_SPIN: 12}, {LINES_CLEAR_NAME: "DOUBLE", T_Spin.NONE: 3, T_Spin.T_SPIN: 12},
{"name": "TRIPLE", T_Spin.NO_T_SPIN: 5, T_Spin.T_SPIN: 16}, {LINES_CLEAR_NAME: "TRIPLE", T_Spin.NONE: 5, T_Spin.T_SPIN: 16},
{"name": "TETRIS", T_Spin.NO_T_SPIN: 8} {LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8}
) )
def lock(self): def lock(self):
@ -364,18 +372,6 @@ class TetrisLogic():
self.game_over() self.game_over()
return return
self.enter_the_matrix()
# Clear complete lines
nb_lines_cleared = 0
for y, line in reversed(list(enumerate(self.matrix))):
if all(mino for mino in line):
nb_lines_cleared += 1
self.remove_line_of_matrix(y)
self.append_new_line_to_matrix()
if nb_lines_cleared:
self.nb_lines += nb_lines_cleared
# T-Spin # T-Spin
if ( if (
self.current_piece.CAN_SPIN self.current_piece.CAN_SPIN
@ -390,11 +386,23 @@ class TetrisLogic():
): ):
t_spin = T_Spin.T_SPIN t_spin = T_Spin.T_SPIN
elif c and d and (a or b): elif c and d and (a or b):
t_spin = T_Spin.MINI_T_SPIN t_spin = T_Spin.MINI
else: else:
t_spin = T_Spin.NO_T_SPIN t_spin = T_Spin.NONE
else: else:
t_spin = T_Spin.NO_T_SPIN t_spin = T_Spin.NONE
self.enter_the_matrix()
# Clear complete lines
nb_lines_cleared = 0
for y, line in reversed(list(enumerate(self.matrix))):
if all(mino for mino in line):
nb_lines_cleared += 1
self.remove_line_of_matrix(y)
self.append_new_line_to_matrix()
if nb_lines_cleared:
self.nb_lines += nb_lines_cleared
# Scoring # Scoring
lock_strings = [] lock_strings = []
@ -403,7 +411,7 @@ class TetrisLogic():
if t_spin: if t_spin:
lock_strings.append(t_spin) lock_strings.append(t_spin)
if nb_lines_cleared: if nb_lines_cleared:
lock_strings.append(self.SCORES[nb_lines_cleared]["name"]) lock_strings.append(self.SCORES[nb_lines_cleared][LINES_CLEAR_NAME])
self.combo += 1 self.combo += 1
else: else:
self.combo = -1 self.combo = -1
@ -416,7 +424,6 @@ class TetrisLogic():
lock_strings.append(str(ds)) lock_strings.append(str(ds))
self.show_text("\n".join(lock_strings)) self.show_text("\n".join(lock_strings))
if self.combo >= 1: if self.combo >= 1:
ds = (20 if nb_lines_cleared==1 else 50) * self.combo * self.level ds = (20 if nb_lines_cleared==1 else 50) * self.combo * self.level
lock_score += ds lock_score += ds