movement and delete de last row complete

master
kirbylife 2019-11-03 22:15:21 -06:00
parent 46e529ad6b
commit 64229b3c67
1 changed files with 78 additions and 16 deletions

View File

@ -1,8 +1,14 @@
import curses
from _curses import error as CursesException
from random import choice
import numpy as np
from afutc.pieces import PIECES
WIDTH = 20
HEIGHT = 25
def catch(func, default=..., args=[], kwargs={}):
try:
@ -23,34 +29,86 @@ class Afutc:
self.stdscr = stdscr
self.print = stdscr.addstr
self.input = stdscr.getkey
self.board = np.zeros(shape=(20, 10))
self.current_move = np.zeros(shape=(20, 10))
self.board = np.zeros(shape=(HEIGHT, WIDTH))
self.current_move = np.zeros(shape=(HEIGHT, WIDTH))
self.board_center = WIDTH // 2
self.current_piece = None
self.char = " "
def get_size(self):
return self.stdscr.getmaxyx()
def draw_board(self):
_, heigth = self.get_size()
heigth_start = (heigth // 2) - (self.board.shape[1] // 2)
print = lambda x: self.print((" " * heigth_start) + x + "\n")
_, height = self.get_size()
height_start = (height // 2) - (WIDTH // 2)
try:
print = lambda x: self.print((" " * height_start) + x + "\n")
row = "" * (self.board.shape[1])
print("ANOTHER F TETRIS CLONE")
print(f"{row}")
for n, row in enumerate(self.board):
for n, row in enumerate(self.board + self.current_move):
row = "".join("" if field else " " for field in row)
print(f"{row}")
row = "" * (self.board.shape[1])
print(f"{row}")
# print(f"╚{row}╝")
print(str(self.pivot))
except CursesException:
self.pause()
def move(self):
pass
def new_random_piece(self):
if all(self.board[-1:, :].flat):
new_row = np.zeros(shape=(1, WIDTH))
self.board = np.concatenate((new_row, self.board[:-1, :]))
self.current_move.fill(0.)
new_piece = np.array(choice(PIECES))
self.current_piece = new_piece.copy()
position = self.board_center - (new_piece.shape[1] // 2)
for i, row in enumerate(new_piece):
for j, value in enumerate(row):
self.current_move[i, position + j] = value
self.pivot = [0, position]
def move(self, direction):
if direction == "down":
if self.pivot[0] == HEIGHT - self.current_piece.shape[0]:
self.board = self.board + self.current_move
self.new_random_piece()
return
temp_board = self.current_move.copy()
temp_board = temp_board[:-1, :]
new_row = np.zeros(shape=(1, WIDTH))
temp_board = np.concatenate((new_row, temp_board))
if any(filter(lambda x: x == 2, (temp_board + self.board).flat)):
self.board = self.board + self.current_move
self.new_random_piece()
else:
self.current_move = temp_board
self.pivot[0] += 1
elif direction == "left" and self.pivot[1] > 0:
temp_board = self.current_move.copy()
temp_board = temp_board[:, 1:]
new_column = np.zeros(shape=(HEIGHT, 1))
temp_board = np.concatenate((temp_board, new_column), axis=1)
if any(filter(lambda x: x == 2, (temp_board + self.board).flat)):
return
self.current_move = temp_board
self.pivot[1] -= 1
elif direction == "right" and self.pivot[1] < (
WIDTH - self.current_piece.shape[1]):
temp_board = self.current_move.copy()
temp_board = temp_board[:, :-1]
new_column = np.zeros(shape=(HEIGHT, 1))
temp_board = np.concatenate((new_column, temp_board), axis=1)
if any(filter(lambda x: x == 2, (temp_board + self.board).flat)):
return
self.current_move = temp_board
self.pivot[1] += 1
def start(self):
scr = self.stdscr
input = self.input
self.new_random_piece()
scr.timeout(1000)
@ -60,15 +118,17 @@ class Afutc:
scr.clear()
if not char:
self.pivot[0] += 1
self.move("down")
elif char == "q":
self.running = False
elif char == "C": # Left
self.pivot[1] += 1
elif char == "D": # Rigth
self.pivot[1] -= 1
elif char == "C": # Right
self.char = char
self.move("right")
elif char == "D": # Left
self.char = char
self.move("left")
elif char == "B": # Down
self.pivot[0] += 1
self.move("down")
elif char == "A": # Rotate
pass
elif char == " ": # Rotate
@ -77,6 +137,8 @@ class Afutc:
self.draw_board()
def pause(self):
self.stdscr.refresh()
self.stdscr.clear()
self.print("PAUSE\n")
self.print("Press <SPACE>")
while True: