From 69f063634b639728e296fadafa1ac65f0237b379 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sun, 10 Nov 2019 00:34:37 -0600 Subject: [PATCH] now the pieces fall asynchronously --- afutc/game.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/afutc/game.py b/afutc/game.py index 5fb32b6..8c454ad 100644 --- a/afutc/game.py +++ b/afutc/game.py @@ -1,6 +1,8 @@ import curses from _curses import error as CursesException from random import choice +from threading import Thread +from time import sleep import numpy as np @@ -30,6 +32,7 @@ class Afutc: self.print = lambda x, color=0: stdscr.addstr(x, curses.color_pair(color)) self.input = stdscr.getkey + self.is_paused = False self.board = np.zeros(shape=(HEIGHT, WIDTH)) self.current_move = 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.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): scr = self.stdscr input = self.input 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: - scr.timeout(1000 - self.score) scr.refresh() char = catch(input, "") scr.clear() - if not char: - self.move("down") - elif char == "q": + if char == "q": self.running = False elif char == "C": # Right self.move("right") @@ -236,6 +248,7 @@ class Afutc: self.draw_board() def pause(self): + self.is_paused = True self.stdscr.refresh() self.stdscr.clear() self.print("PAUSE\n") @@ -243,6 +256,7 @@ class Afutc: while True: char = catch(self.input, "") if char == " ": + self.is_paused = False break elif char == "q": self.running = False