move next piece logic in NextQueue class + comments
This commit is contained in:
parent
2895570f6e
commit
a3dc434c88
@ -14,7 +14,7 @@ AUTOREPEAT_DELAY = 0.300 # Official : 0.300 s
|
|||||||
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010 s
|
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010 s
|
||||||
|
|
||||||
# Piece init coord
|
# Piece init coord
|
||||||
MATRIX_PIECE_COORD = Coord(4, LINES)
|
FALLING_PIECE_COORD = Coord(4, LINES)
|
||||||
|
|
||||||
# Scores
|
# Scores
|
||||||
LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
|
LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
|
||||||
|
@ -16,7 +16,7 @@ from .consts import (
|
|||||||
FALL_DELAY,
|
FALL_DELAY,
|
||||||
AUTOREPEAT_DELAY,
|
AUTOREPEAT_DELAY,
|
||||||
AUTOREPEAT_PERIOD,
|
AUTOREPEAT_PERIOD,
|
||||||
MATRIX_PIECE_COORD,
|
FALLING_PIECE_COORD,
|
||||||
SCORES,
|
SCORES,
|
||||||
LINES_CLEAR_NAME,
|
LINES_CLEAR_NAME,
|
||||||
)
|
)
|
||||||
@ -26,18 +26,19 @@ CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
|
|||||||
|
|
||||||
|
|
||||||
class AbstractScheduler:
|
class AbstractScheduler:
|
||||||
|
"""Scheduler class to be implemented"""
|
||||||
def postpone(task, delay):
|
def postpone(task, delay):
|
||||||
"""schedule callable task once after delay in second"""
|
"""schedule callable task once after delay in second"""
|
||||||
raise Warning("AbstractTimer.postpone is not implemented.")
|
raise Warning("AbstractScheduler.postpone is not implemented.")
|
||||||
|
|
||||||
def cancel(self, task):
|
def cancel(self, task):
|
||||||
"""cancel task if schedule of pass"""
|
"""cancel task if schedule of pass"""
|
||||||
raise Warning("AbstractTimer.stop is not implemented.")
|
raise Warning("AbstractScheduler.stop is not implemented.")
|
||||||
|
|
||||||
def reset(self, task, period):
|
def reset(self, task, delay):
|
||||||
"""cancel and reschedule task"""
|
"""cancel and reschedule task"""
|
||||||
self.timer.cancel(task)
|
self.timer.cancel(task)
|
||||||
self.timer.postpone(task, period)
|
self.timer.postpone(task, delay)
|
||||||
|
|
||||||
|
|
||||||
class AbstractPieceContainer:
|
class AbstractPieceContainer:
|
||||||
@ -46,10 +47,12 @@ class AbstractPieceContainer:
|
|||||||
|
|
||||||
|
|
||||||
class HoldQueue(AbstractPieceContainer):
|
class HoldQueue(AbstractPieceContainer):
|
||||||
|
"""the storage place where players can Hold any falling Tetrimino for use later"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Matrix(list, AbstractPieceContainer):
|
class Matrix(list, AbstractPieceContainer):
|
||||||
|
"""the rectangular arrangement of cells creating the active game area, usually 10 columns wide by 20 rows high."""
|
||||||
def __init__(self, lines, collumns):
|
def __init__(self, lines, collumns):
|
||||||
list.__init__(self)
|
list.__init__(self)
|
||||||
AbstractPieceContainer.__init__(self)
|
AbstractPieceContainer.__init__(self)
|
||||||
@ -57,7 +60,8 @@ class Matrix(list, AbstractPieceContainer):
|
|||||||
self.collumns = collumns
|
self.collumns = collumns
|
||||||
self.ghost = None
|
self.ghost = None
|
||||||
|
|
||||||
def reset(self):
|
def new_game(self):
|
||||||
|
"""Removes all minoes in matrix"""
|
||||||
self.clear()
|
self.clear()
|
||||||
for y in range(self.lines + 3):
|
for y in range(self.lines + 3):
|
||||||
self.append_new_line()
|
self.append_new_line()
|
||||||
@ -83,13 +87,22 @@ class Matrix(list, AbstractPieceContainer):
|
|||||||
|
|
||||||
|
|
||||||
class NextQueue(AbstractPieceContainer):
|
class NextQueue(AbstractPieceContainer):
|
||||||
|
"""Displays the Next Tetrimino(s) to be placed (generated) just above the Matrix"""
|
||||||
def __init__(self, nb_pieces):
|
def __init__(self, nb_pieces):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.nb_pieces = nb_pieces
|
self.nb_pieces = nb_pieces
|
||||||
self.pieces = []
|
self.pieces = []
|
||||||
|
|
||||||
|
def new_game(self):
|
||||||
|
self.pieces = [Tetromino() for n in range(self.nb_pieces)]
|
||||||
|
|
||||||
|
def generation_phase(self):
|
||||||
|
self.pieces.append(Tetromino())
|
||||||
|
return self.pieces.pop(0)
|
||||||
|
|
||||||
|
|
||||||
class Stats:
|
class Stats:
|
||||||
|
"""Game statistics"""
|
||||||
def _get_score(self):
|
def _get_score(self):
|
||||||
return self._score
|
return self._score
|
||||||
|
|
||||||
@ -160,7 +173,7 @@ class TetrisLogic:
|
|||||||
# These class attributes can be redefined on inheritance
|
# These class attributes can be redefined on inheritance
|
||||||
AUTOREPEAT_DELAY = AUTOREPEAT_DELAY
|
AUTOREPEAT_DELAY = AUTOREPEAT_DELAY
|
||||||
AUTOREPEAT_PERIOD = AUTOREPEAT_PERIOD
|
AUTOREPEAT_PERIOD = AUTOREPEAT_PERIOD
|
||||||
MATRIX_PIECE_COORD = MATRIX_PIECE_COORD
|
FALLING_PIECE_COORD = FALLING_PIECE_COORD
|
||||||
|
|
||||||
timer = AbstractScheduler()
|
timer = AbstractScheduler()
|
||||||
|
|
||||||
@ -181,8 +194,8 @@ class TetrisLogic:
|
|||||||
|
|
||||||
self.pressed_actions = []
|
self.pressed_actions = []
|
||||||
|
|
||||||
self.matrix.reset()
|
self.matrix.new_game()
|
||||||
self.next.pieces = [Tetromino() for n in range(self.next.nb_pieces)]
|
self.next.new_game()
|
||||||
self.held.piece = None
|
self.held.piece = None
|
||||||
self.timer.postpone(self.stats.update_time, 1)
|
self.timer.postpone(self.stats.update_time, 1)
|
||||||
|
|
||||||
@ -204,9 +217,8 @@ class TetrisLogic:
|
|||||||
|
|
||||||
def generation_phase(self, held_piece=None):
|
def generation_phase(self, held_piece=None):
|
||||||
if not held_piece:
|
if not held_piece:
|
||||||
self.matrix.piece = self.next.pieces.pop(0)
|
self.matrix.piece = self.next.generation_phase()
|
||||||
self.next.pieces.append(Tetromino())
|
self.matrix.piece.coord = self.FALLING_PIECE_COORD
|
||||||
self.matrix.piece.coord = self.MATRIX_PIECE_COORD
|
|
||||||
self.matrix.ghost = self.matrix.piece.ghost()
|
self.matrix.ghost = self.matrix.piece.ghost()
|
||||||
self.refresh_ghost()
|
self.refresh_ghost()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user