class TetrisPreviewWidget(DrawabaleTetrisBoard): """ A preview window for figures. Only shows a single figure, trying to center it vertically. Note: the success of this method depends on the actual figures that participate in the game. It works well for the default 7 Tetris figures. """ def __init__(self, parent, nrows, ncols, blocksize): super(TetrisPreviewWidget, self).__init__(parent, nrows, ncols, blocksize) self.showgrid = False self.figure = None def set_figure(self, figure): self.figure = figure self.board = TetrisBoard(self.nrows, self.ncols) self.board.spawn_figure(figure) self.board.move_figure_down() self.board.move_figure_down() self.update()
class MainTetrisWidget(DrawabaleTetrisBoard): """ The main tetris window type, with an active figure that can be moved around, dropped, etc. and a bunch of inactive blocks. Supports all the expected tetris operations, like removing completed rows and generating new figures. """ def __init__(self, parent, nrows, ncols, blocksize, startfigure): super(MainTetrisWidget, self).__init__(parent, nrows, ncols, blocksize) self.board.spawn_figure(startfigure) # Keeps track of the amount of rows the active figure # fell in the last "drop" command. This is used to # update the score. # self.last_drop_height = 0 def restart(self, startfigure): self.board = TetrisBoard(self.nrows, self.ncols) self.board.spawn_figure(startfigure) self.update() def keyPressEvent(self, event): if event.key() == Qt.Key_Up: self.board.rotate_figure() elif event.key() == Qt.Key_Down: self.board.move_figure_down() elif event.key() == Qt.Key_Left: self.board.move_figure_left() elif event.key() == Qt.Key_Right: self.board.move_figure_right() elif event.key() == Qt.Key_Space: for i in range(self.nrows): if not self.board.move_figure_down(): self.last_drop_height = i break else: return self.update() Result = namedtuple('Result', 'state completed_rows drop_height') def timer_tick(self, nextfigure): """ One timer tick for the tetris game. Advances the game by one step and returns a result as a namedtuple: result.state: The game state. running - The current figure was moved down by one cell successfully. newfigure - The current figure could no longer be moved down, so a new figure was created. gameover - The current figure could no longer be moved down, and a new figure could not be created. result.completed_rows: A list of row numbers that were completed with the current figure reaching bottom. It is applicable in the "newfigure" state result.drop_height: The amount of lines the figure was dropped in the last drop. """ state = 'running' completed_rows = [] drop_height = 0 if self.board.move_figure_down(): state = 'running' else: completed_rows = self.board.finish_fall() if self.board.spawn_figure(nextfigure): state = 'newfigure' else: state = 'gameover' drop_height = self.last_drop_height self.last_drop_height = 0 self.update() return self.Result(state, completed_rows, drop_height)
class MainTetrisWidget(DrawabaleTetrisBoard): """ The main tetris window type, with an active figure that can be moved around, dropped, etc. and a bunch of inactive blocks. Supports all the expected tetris operations, like removing completed rows and generating new figures. """ def __init__(self, parent, nrows, ncols, blocksize, startfigure): super(MainTetrisWidget, self).__init__(parent, nrows, ncols, blocksize) self.board.spawn_figure(startfigure) # Keeps track of the amount of rows the active figure # fell in the last "drop" command. This is used to # update the score. # self.last_drop_height = 0 def restart(self, startfigure): self.board = TetrisBoard(self.nrows, self.ncols) self.board.spawn_figure(startfigure) self.update() def keyPressEvent(self, event): if event.key() == Qt.Key_Up: self.board.rotate_figure() elif event.key() == Qt.Key_Down: self.board.move_figure_down() elif event.key() == Qt.Key_Left: self.board.move_figure_left() elif event.key() == Qt.Key_Right: self.board.move_figure_right() elif event.key() == Qt.Key_Space: for i in range(self.nrows): if not self.board.move_figure_down(): self.last_drop_height = i break else: return self.update() Result = namedtuple("Result", "state completed_rows drop_height") def timer_tick(self, nextfigure): """ One timer tick for the tetris game. Advances the game by one step and returns a result as a namedtuple: result.state: The game state. running - The current figure was moved down by one cell successfully. newfigure - The current figure could no longer be moved down, so a new figure was created. gameover - The current figure could no longer be moved down, and a new figure could not be created. result.completed_rows: A list of row numbers that were completed with the current figure reaching bottom. It is applicable in the "newfigure" state result.drop_height: The amount of lines the figure was dropped in the last drop. """ state = "running" completed_rows = [] drop_height = 0 if self.board.move_figure_down(): state = "running" else: completed_rows = self.board.finish_fall() if self.board.spawn_figure(nextfigure): state = "newfigure" else: state = "gameover" drop_height = self.last_drop_height self.last_drop_height = 0 self.update() return self.Result(state, completed_rows, drop_height)