added colors to the pieces

master
kirbylife 2019-11-09 22:03:22 -06:00
parent 2e718315a9
commit bada0fc18e
2 changed files with 26 additions and 9 deletions

View File

@ -27,7 +27,8 @@ class Afutc:
def __init__(self, stdscr): def __init__(self, stdscr):
self.stdscr = stdscr self.stdscr = stdscr
self.print = stdscr.addstr self.print = lambda x, color=0: stdscr.addstr(x,
curses.color_pair(color))
self.input = stdscr.getkey self.input = stdscr.getkey
self.board = np.zeros(shape=(HEIGHT, WIDTH)) self.board = np.zeros(shape=(HEIGHT, WIDTH))
self.current_move = np.zeros(shape=(HEIGHT, WIDTH)) self.current_move = np.zeros(shape=(HEIGHT, WIDTH))
@ -41,7 +42,7 @@ class Afutc:
def is_valid(self, board): def is_valid(self, board):
for field in (board + self.board).flat: for field in (board + self.board).flat:
if field == 2: if field != 0 and field % 2 == 0:
return False return False
return True return True
@ -52,23 +53,26 @@ class Afutc:
_, height = self.get_size() _, height = self.get_size()
height_start = (height // 2) - (WIDTH // 2) height_start = (height // 2) - (WIDTH // 2)
try: try:
print = lambda x: self.print((" " * height_start) + x + "\n") print = lambda x, color=0, end="\n": self.print((
" " * height_start) + x + end, color)
iprint = lambda x, color=0: self.print(x, color)
row = str(self.score) row = str(self.score)
row = f"{row}" row = f"{row}"
row = row.center(self.board.shape[1], "") row = row.center(self.board.shape[1], "")
print("ANOTHER F TETRIS CLONE") print("A F U TETRIS CLONE")
print(f"{row}") print(f"{row}")
for i, row in enumerate(self.board + self.current_move): for i, row in enumerate(self.board + self.current_move):
row_str = "" row_str = ""
print("", end="")
for j, field in enumerate(row): for j, field in enumerate(row):
if self.ghost[i, j]: if self.ghost[i, j]:
row_str += "" iprint("", 0)
elif field: elif field:
row_str += self.char iprint(self.char, int(field))
else: else:
row_str += " " iprint(" ")
row_str = f"{row_str}" row_str += f""
piece = self.next_piece piece = self.next_piece
piece_height, piece_width = piece.shape piece_height, piece_width = piece.shape
@ -84,7 +88,7 @@ class Afutc:
if i == (piece_height + 2): if i == (piece_height + 2):
row_str += f"{'' * (piece_width + 2)}" row_str += f"{'' * (piece_width + 2)}"
print(row_str) iprint(row_str + "\n")
row = "" * (self.board.shape[1]) row = "" * (self.board.shape[1])
print(f"{row}") print(f"{row}")
@ -256,6 +260,10 @@ class Afutc:
def start(): def start():
stdscr = curses.initscr() stdscr = curses.initscr()
curses.noecho() curses.noecho()
curses.start_color()
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
curses.curs_set(False) curses.curs_set(False)
try: try:
game = Afutc(stdscr) game = Afutc(stdscr)

View File

@ -1,3 +1,12 @@
import numpy as np
PIECES = [[[0, 1], [0, 1], [1, 1]], [[0, 1, 0], [1, 1, 1]], [[1, 1], [1, 1]], PIECES = [[[0, 1], [0, 1], [1, 1]], [[0, 1, 0], [1, 1, 1]], [[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]], [[0, 1, 1], [1, 1, 0]], [[1], [1], [1], [1]], [[1, 1, 0], [0, 1, 1]], [[0, 1, 1], [1, 1, 0]], [[1], [1], [1], [1]],
[[0, 1, 0], [0, 1, 0], [1, 1, 1]]] [[0, 1, 0], [0, 1, 0], [1, 1, 1]]]
__colors = [5, 7, 9, 11, 13, 15, 35, 41, 123, 205, 219]
for n, piece in enumerate(PIECES):
piece = np.array(piece)
piece *= __colors[n]
PIECES[n] = piece.copy()