def draw_blocks(self): # Draw the main board and whatever elements are there ansi.position(self.b_board.pos + (1, 1)) for row in self.dim: for cell in row: ansi.raw(*Field._dim_mapping[cell]) ansi.raw(Box.V_LINE) ansi.ln(self.b_board.width * 2 + 1) # +1 is for the last character we actually wrote # Draw the current tetromino falling if self.t_current is not None: # Get the current position of the current tetromino t_current_pos = self.b_board.pos + self.t_current.pos.map( lambda x: 1 + (x * 2), lambda y: 1 + y) ansi.position(t_current_pos) self.t_current._print() # Redraw the board for the next tetromino self.b_next._print() # Draw the next tetromino (aka spare) if self.t_next is not None: ansi.position(self.b_next.pos + (0, 1)) self.t_next._print(self.b_next.width) ansi.position(1, 1)
def run(self): # Here we'll have 2 threads, one counting time and moving the block down # and the main one waiting for input for c in ansi.read(): if c == 3: # If Ctrl + C break with ansi.get_movement(c) as m: if m in Field._move_mapping: Field._move_mapping[m](self) self.draw_blocks() ansi.clear_line() ansi.raw(str(c))
def start(self): # draw the main board ansi.clear() self.b_board._print() self.b_next._print() # Add level and score ansi.position(30, 9) ansi.raw("Level:") ansi.position(30, 10) ansi.raw("Score:") self.draw_blocks() ansi.position(1, 1)
def _print(self, in_size=None): if in_size is not None: # calculate center in given size padding = Tetromino.center(in_size, self.width) ansi.move(padding, 'C') ansi.raw(self._color()) for row in self.matrix: for col in row: if col == '.': ansi.raw('##') # ansi.move(2, 'C') else: ansi.raw(Tetromino.BLOCK) ansi.ln(self.width * 2) ansi.raw(Color.RESET)
def _print(self): ansi.position(self.pos()) ansi.raw(Box.TL_CORNER, Box.H_LINE * self.width, Box.TR_CORNER) ansi.ln(self.width * 2 + 2) for i in range(self.height): ansi.raw(Box.V_LINE, Box.SPACE * self.width, Box.V_LINE) ansi.ln(self.width * 2 + 2) ansi.raw(Box.BL_CORNER, Box.H_LINE * self.width, Box.BR_CORNER)