now the pieces fall asynchronously
parent
3f68c12ff6
commit
69f063634b
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue