now the pieces fall asynchronously

master
kirbylife 2019-11-10 00:34:37 -06:00
parent 3f68c12ff6
commit 69f063634b
1 changed files with 18 additions and 4 deletions

View File

@ -1,6 +1,8 @@
import curses import curses
from _curses import error as CursesException from _curses import error as CursesException
from random import choice from random import choice
from threading import Thread
from time import sleep
import numpy as np import numpy as np
@ -30,6 +32,7 @@ class Afutc:
self.print = lambda x, color=0: stdscr.addstr(x, self.print = lambda x, color=0: stdscr.addstr(x,
curses.color_pair(color)) curses.color_pair(color))
self.input = stdscr.getkey self.input = stdscr.getkey
self.is_paused = False
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))
self.ghost = np.zeros(shape=(HEIGHT, WIDTH)) self.ghost = np.zeros(shape=(HEIGHT, WIDTH))
@ -207,20 +210,29 @@ class Afutc:
self.current_piece = temp_piece.copy() self.current_piece = temp_piece.copy()
self.recalculate_ghost() self.recalculate_ghost()
def __move_to_down(self):
while self.running:
sleep((1000 - self.score) / 1000)
if self.is_paused:
continue
self.move("down")
def start(self): def start(self):
scr = self.stdscr scr = self.stdscr
input = self.input input = self.input
self.new_random_piece() self.new_random_piece()
down_thread = Thread(target=self.__move_to_down)
down_thread.setDaemon(True)
down_thread.start()
scr.timeout(1000)
while self.running: while self.running:
scr.timeout(1000 - self.score)
scr.refresh() scr.refresh()
char = catch(input, "") char = catch(input, "")
scr.clear() scr.clear()
if not char: if char == "q":
self.move("down")
elif char == "q":
self.running = False self.running = False
elif char == "C": # Right elif char == "C": # Right
self.move("right") self.move("right")
@ -236,6 +248,7 @@ class Afutc:
self.draw_board() self.draw_board()
def pause(self): def pause(self):
self.is_paused = True
self.stdscr.refresh() self.stdscr.refresh()
self.stdscr.clear() self.stdscr.clear()
self.print("PAUSE\n") self.print("PAUSE\n")
@ -243,6 +256,7 @@ class Afutc:
while True: while True:
char = catch(self.input, "") char = catch(self.input, "")
if char == " ": if char == " ":
self.is_paused = False
break break
elif char == "q": elif char == "q":
self.running = False self.running = False