Update terminis.py
This commit is contained in:
parent
71037c6182
commit
e6d18deb9b
@ -20,20 +20,20 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
DIR_NAME = "Terminis"
|
DIR_NAME = "Terminis"
|
||||||
|
|
||||||
|
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
if locale.getpreferredencoding() == 'UTF-8':
|
if locale.getpreferredencoding() == 'UTF-8':
|
||||||
os.environ["NCURSES_NO_UTF8_ACS"] = "1"
|
os.environ["NCURSES_NO_UTF8_ACS"] = "1"
|
||||||
|
|
||||||
scheduler = sched.scheduler(time.time, lambda delay: curses.napms(int(delay*1000)))
|
scheduler = sched.scheduler(time.time, lambda delay: curses.napms(int(delay*1000)))
|
||||||
|
|
||||||
|
|
||||||
class Rotation:
|
class Rotation:
|
||||||
CLOCKWISE = 1
|
CLOCKWISE = 1
|
||||||
COUNTERCLOCKWISE = -1
|
COUNTERCLOCKWISE = -1
|
||||||
|
|
||||||
|
|
||||||
class Color:
|
class Color:
|
||||||
BLACK = 0
|
BLACK = 0
|
||||||
WHITE = 1
|
WHITE = 1
|
||||||
@ -45,68 +45,21 @@ class Color:
|
|||||||
CYAN = 7
|
CYAN = 7
|
||||||
ORANGE = 8
|
ORANGE = 8
|
||||||
|
|
||||||
|
|
||||||
class Point:
|
class Point:
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
return Point(self.x+other.x, self.y+other.y)
|
return Point(self.x+other.x, self.y+other.y)
|
||||||
|
|
||||||
|
|
||||||
class Movement:
|
class Movement:
|
||||||
LEFT = Point(-1, 0)
|
LEFT = Point(-1, 0)
|
||||||
RIGHT = Point(1, 0)
|
RIGHT = Point(1, 0)
|
||||||
DOWN = Point(0, 1)
|
DOWN = Point(0, 1)
|
||||||
STILL = Point(0, 0)
|
STILL = Point(0, 0)
|
||||||
|
|
||||||
|
|
||||||
class Screen:
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
self.scr = curses.initscr()
|
|
||||||
curses.def_shell_mode()
|
|
||||||
if curses.has_colors():
|
|
||||||
self.init_colors()
|
|
||||||
curses.noecho()
|
|
||||||
curses.cbreak()
|
|
||||||
self.scr.keypad(True)
|
|
||||||
curses.curs_set(0)
|
|
||||||
self.scr.clear()
|
|
||||||
self.scr.nodelay(True)
|
|
||||||
self.scr.leaveok(True)
|
|
||||||
self.scr.getch()
|
|
||||||
return self.scr
|
|
||||||
if curses.has_colors():
|
|
||||||
self.init_colors()
|
|
||||||
|
|
||||||
def init_colors(self):
|
|
||||||
curses.start_color()
|
|
||||||
if curses.COLORS >= 16:
|
|
||||||
if curses.can_change_color():
|
|
||||||
curses.init_color(curses.COLOR_YELLOW, 1000, 500, 0)
|
|
||||||
curses.init_pair(Color.ORANGE, curses.COLOR_YELLOW, curses.COLOR_YELLOW)
|
|
||||||
curses.init_pair(Color.RED, curses.COLOR_RED+8, curses.COLOR_RED+8)
|
|
||||||
curses.init_pair(Color.GREEN, curses.COLOR_GREEN+8, curses.COLOR_GREEN+8)
|
|
||||||
curses.init_pair(Color.YELLOW, curses.COLOR_YELLOW+8, curses.COLOR_YELLOW+8)
|
|
||||||
curses.init_pair(Color.BLUE, curses.COLOR_BLUE+8, curses.COLOR_BLUE+8)
|
|
||||||
curses.init_pair(Color.MAGENTA, curses.COLOR_MAGENTA+8, curses.COLOR_MAGENTA+8)
|
|
||||||
curses.init_pair(Color.CYAN, curses.COLOR_CYAN+8, curses.COLOR_CYAN+8)
|
|
||||||
curses.init_pair(Color.WHITE, curses.COLOR_WHITE+8, curses.COLOR_WHITE+8)
|
|
||||||
else:
|
|
||||||
curses.init_pair(Color.ORANGE, curses.COLOR_YELLOW, curses.COLOR_YELLOW)
|
|
||||||
curses.init_pair(Color.RED, curses.COLOR_RED, curses.COLOR_RED)
|
|
||||||
curses.init_pair(Color.GREEN, curses.COLOR_GREEN, curses.COLOR_GREEN)
|
|
||||||
curses.init_pair(Color.YELLOW, curses.COLOR_WHITE, curses.COLOR_WHITE)
|
|
||||||
curses.init_pair(Color.BLUE, curses.COLOR_BLUE, curses.COLOR_BLUE)
|
|
||||||
curses.init_pair(Color.MAGENTA, curses.COLOR_MAGENTA, curses.COLOR_MAGENTA)
|
|
||||||
curses.init_pair(Color.CYAN, curses.COLOR_CYAN, curses.COLOR_CYAN)
|
|
||||||
curses.init_pair(Color.WHITE, curses.COLOR_WHITE, curses.COLOR_WHITE)
|
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
|
||||||
curses.reset_shell_mode()
|
|
||||||
curses.endwin()
|
|
||||||
|
|
||||||
|
|
||||||
class Mino:
|
class Mino:
|
||||||
@ -136,7 +89,7 @@ class Tetromino:
|
|||||||
)
|
)
|
||||||
lock_delay = 0.5
|
lock_delay = 0.5
|
||||||
fall_delay = 1
|
fall_delay = 1
|
||||||
|
|
||||||
def __init__(self, matrix, position):
|
def __init__(self, matrix, position):
|
||||||
self.matrix = matrix
|
self.matrix = matrix
|
||||||
self.position = position
|
self.position = position
|
||||||
@ -150,7 +103,7 @@ class Tetromino:
|
|||||||
self.lock_timer = None
|
self.lock_timer = None
|
||||||
self.fall_timer = None
|
self.fall_timer = None
|
||||||
self.hold_enabled = True
|
self.hold_enabled = True
|
||||||
|
|
||||||
def move(self, movement, lock=True):
|
def move(self, movement, lock=True):
|
||||||
potential_position = self.position + movement
|
potential_position = self.position + movement
|
||||||
if all(
|
if all(
|
||||||
@ -166,11 +119,11 @@ class Tetromino:
|
|||||||
if lock and movement == Movement.DOWN:
|
if lock and movement == Movement.DOWN:
|
||||||
self.locking()
|
self.locking()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def soft_drop(self):
|
def soft_drop(self):
|
||||||
if self.move(Movement.DOWN):
|
if self.move(Movement.DOWN):
|
||||||
self.matrix.game.stats.piece_dropped(1)
|
self.matrix.game.stats.piece_dropped(1)
|
||||||
|
|
||||||
def hard_drop(self):
|
def hard_drop(self):
|
||||||
if self.lock_timer:
|
if self.lock_timer:
|
||||||
scheduler.cancel(self.lock_timer)
|
scheduler.cancel(self.lock_timer)
|
||||||
@ -180,7 +133,7 @@ class Tetromino:
|
|||||||
lines += 2
|
lines += 2
|
||||||
self.matrix.game.stats.piece_dropped(lines)
|
self.matrix.game.stats.piece_dropped(lines)
|
||||||
self.lock()
|
self.lock()
|
||||||
|
|
||||||
def rotate(self, direction):
|
def rotate(self, direction):
|
||||||
potential_minoes_positions = tuple(
|
potential_minoes_positions = tuple(
|
||||||
Point(-direction*mino.position.y, direction*mino.position.x)
|
Point(-direction*mino.position.y, direction*mino.position.x)
|
||||||
@ -204,21 +157,21 @@ class Tetromino:
|
|||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def fall(self):
|
def fall(self):
|
||||||
self.fall_timer = scheduler.enter(self.fall_delay, 2, self.fall, tuple())
|
self.fall_timer = scheduler.enter(self.fall_delay, 2, self.fall, tuple())
|
||||||
self.move(Movement.DOWN)
|
self.move(Movement.DOWN)
|
||||||
|
|
||||||
def locking(self):
|
def locking(self):
|
||||||
if not self.lock_timer:
|
if not self.lock_timer:
|
||||||
self.lock_timer = scheduler.enter(self.lock_delay, 1, self.lock, tuple())
|
self.lock_timer = scheduler.enter(self.lock_delay, 1, self.lock, tuple())
|
||||||
self.matrix.refresh()
|
self.matrix.refresh()
|
||||||
|
|
||||||
def postpone_lock(self):
|
def postpone_lock(self):
|
||||||
if self.lock_timer:
|
if self.lock_timer:
|
||||||
scheduler.cancel(self.lock_timer)
|
scheduler.cancel(self.lock_timer)
|
||||||
self.lock_timer = scheduler.enter(self.lock_delay, 1, self.lock, tuple())
|
self.lock_timer = scheduler.enter(self.lock_delay, 1, self.lock, tuple())
|
||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
self.lock_timer = None
|
self.lock_timer = None
|
||||||
if not self.move(Movement.DOWN, lock=False):
|
if not self.move(Movement.DOWN, lock=False):
|
||||||
@ -229,7 +182,7 @@ class Tetromino:
|
|||||||
self.matrix.game.over()
|
self.matrix.game.over()
|
||||||
else:
|
else:
|
||||||
self.matrix.lock(self.t_spin())
|
self.matrix.lock(self.t_spin())
|
||||||
|
|
||||||
def t_spin(self):
|
def t_spin(self):
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@ -237,61 +190,61 @@ class Tetromino:
|
|||||||
class O(Tetromino):
|
class O(Tetromino):
|
||||||
MINOES_POSITIONS = (Point(0, 0), Point(1, 0), Point(0, -1), Point(1, -1))
|
MINOES_POSITIONS = (Point(0, 0), Point(1, 0), Point(0, -1), Point(1, -1))
|
||||||
COLOR = Color.YELLOW
|
COLOR = Color.YELLOW
|
||||||
|
|
||||||
def rotate(self, direction):
|
def rotate(self, direction):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
class I(Tetromino):
|
class I(Tetromino):
|
||||||
SUPER_ROTATION_SYSTEM = (
|
SUPER_ROTATION_SYSTEM = (
|
||||||
{
|
{
|
||||||
Rotation.COUNTERCLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
|
Rotation.COUNTERCLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
|
||||||
Rotation.CLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
|
Rotation.CLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Rotation.COUNTERCLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
|
Rotation.COUNTERCLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
|
||||||
Rotation.CLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
|
Rotation.CLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Rotation.COUNTERCLOCKWISE: (Point(0, -1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
|
Rotation.COUNTERCLOCKWISE: (Point(0, -1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
|
||||||
Rotation.CLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
|
Rotation.CLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Rotation.COUNTERCLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
|
Rotation.COUNTERCLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
|
||||||
Rotation.CLOCKWISE: (Point(0, 1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
|
Rotation.CLOCKWISE: (Point(0, 1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(2, 0))
|
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(2, 0))
|
||||||
COLOR = Color.CYAN
|
COLOR = Color.CYAN
|
||||||
|
|
||||||
class T(Tetromino):
|
class T(Tetromino):
|
||||||
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, 0))
|
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, 0))
|
||||||
COLOR = Color.MAGENTA
|
COLOR = Color.MAGENTA
|
||||||
T_SLOT = (Point(-1, -1), Point(1, -1), Point(1, 1), Point(-1, 1))
|
T_SLOT = (Point(-1, -1), Point(1, -1), Point(1, 1), Point(-1, 1))
|
||||||
|
|
||||||
def t_spin(self):
|
def t_spin(self):
|
||||||
if self.rotated_last:
|
if self.rotated_last:
|
||||||
a = not self.matrix.is_free_cell(self.position + self.T_SLOT[self.orientation])
|
a = not self.matrix.is_free_cell(self.position + self.T_SLOT[self.orientation])
|
||||||
b = not self.matrix.is_free_cell(self.position + self.T_SLOT[(1+self.orientation)%4])
|
b = not self.matrix.is_free_cell(self.position + self.T_SLOT[(1+self.orientation)%4])
|
||||||
c = not self.matrix.is_free_cell(self.position + self.T_SLOT[(3+self.orientation)%4])
|
c = not self.matrix.is_free_cell(self.position + self.T_SLOT[(3+self.orientation)%4])
|
||||||
d = not self.matrix.is_free_cell(self.position + self.T_SLOT[(2+self.orientation)%4])
|
d = not self.matrix.is_free_cell(self.position + self.T_SLOT[(2+self.orientation)%4])
|
||||||
if self.rotation_point_5_used or (a and b and (c or d)):
|
if self.rotation_point_5_used or (a and b and (c or d)):
|
||||||
return "T-SPIN"
|
return "T-SPIN"
|
||||||
elif c and d and (a or b):
|
elif c and d and (a or b):
|
||||||
return "MINI T-SPIN"
|
return "MINI T-SPIN"
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
class L(Tetromino):
|
class L(Tetromino):
|
||||||
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(1, -1))
|
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(1, -1))
|
||||||
COLOR = Color.ORANGE
|
COLOR = Color.ORANGE
|
||||||
|
|
||||||
class J(Tetromino):
|
class J(Tetromino):
|
||||||
MINOES_POSITIONS = (Point(-1, -1), Point(-1, 0), Point(0, 0), Point(1, 0))
|
MINOES_POSITIONS = (Point(-1, -1), Point(-1, 0), Point(0, 0), Point(1, 0))
|
||||||
COLOR = Color.BLUE
|
COLOR = Color.BLUE
|
||||||
|
|
||||||
class S(Tetromino):
|
class S(Tetromino):
|
||||||
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, -1))
|
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, -1))
|
||||||
COLOR = Color.GREEN
|
COLOR = Color.GREEN
|
||||||
|
|
||||||
class Z(Tetromino):
|
class Z(Tetromino):
|
||||||
MINOES_POSITIONS = (Point(-1, -1), Point(0, -1), Point(0, 0), Point(1, 0))
|
MINOES_POSITIONS = (Point(-1, -1), Point(0, -1), Point(0, 0), Point(1, 0))
|
||||||
COLOR = Color.RED
|
COLOR = Color.RED
|
||||||
@ -305,27 +258,27 @@ class Window:
|
|||||||
self.title_begin_x = (width-len(self.TITLE)) // 2 + 1
|
self.title_begin_x = (width-len(self.TITLE)) // 2 + 1
|
||||||
self.piece = None
|
self.piece = None
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def draw_border(self):
|
def draw_border(self):
|
||||||
self.window.erase()
|
self.window.erase()
|
||||||
self.window.border()
|
self.window.border()
|
||||||
if self.TITLE:
|
if self.TITLE:
|
||||||
self.window.addstr(0, self.title_begin_x, self.TITLE, curses.A_BOLD)
|
self.window.addstr(0, self.title_begin_x, self.TITLE, curses.A_BOLD)
|
||||||
|
|
||||||
def draw_piece(self):
|
def draw_piece(self):
|
||||||
if self.piece:
|
if self.piece:
|
||||||
color = Color.WHITE|curses.A_BLINK if self.piece.lock_timer else self.piece.COLOR
|
color = Color.WHITE|curses.A_BLINK if self.piece.lock_timer else self.piece.COLOR
|
||||||
for mino in self.piece.minoes:
|
for mino in self.piece.minoes:
|
||||||
position = mino.position + self.piece.position
|
position = mino.position + self.piece.position
|
||||||
self.draw_mino(position.x, position.y, color)
|
self.draw_mino(position.x, position.y, color)
|
||||||
|
|
||||||
def draw_mino(self, x, y, color):
|
def draw_mino(self, x, y, color):
|
||||||
if y >= 0:
|
if y >= 0:
|
||||||
if self.has_colors:
|
if self.has_colors:
|
||||||
self.window.addstr(y, x*2+1, "██", curses.color_pair(color))
|
self.window.addstr(y, x*2+1, "██", curses.color_pair(color))
|
||||||
else:
|
else:
|
||||||
self.window.addstr(y, x*2+1, "██")
|
self.window.addstr(y, x*2+1, "██")
|
||||||
|
|
||||||
|
|
||||||
class Matrix(Window):
|
class Matrix(Window):
|
||||||
NB_COLS = 10
|
NB_COLS = 10
|
||||||
@ -334,7 +287,7 @@ class Matrix(Window):
|
|||||||
HEIGHT = NB_LINES+1
|
HEIGHT = NB_LINES+1
|
||||||
PIECE_POSITION = Point(4, 0)
|
PIECE_POSITION = Point(4, 0)
|
||||||
TITLE = ""
|
TITLE = ""
|
||||||
|
|
||||||
def __init__(self, game, begin_x, begin_y):
|
def __init__(self, game, begin_x, begin_y):
|
||||||
begin_x += (game.WIDTH - self.WIDTH) // 2
|
begin_x += (game.WIDTH - self.WIDTH) // 2
|
||||||
begin_y += (game.HEIGHT - self.HEIGHT) // 2
|
begin_y += (game.HEIGHT - self.HEIGHT) // 2
|
||||||
@ -344,31 +297,31 @@ class Matrix(Window):
|
|||||||
for y in range(self.NB_LINES)
|
for y in range(self.NB_LINES)
|
||||||
]
|
]
|
||||||
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y)
|
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y)
|
||||||
|
|
||||||
def refresh(self, paused=False):
|
def refresh(self, paused=False):
|
||||||
self.draw_border()
|
self.draw_border()
|
||||||
if paused:
|
if paused:
|
||||||
self.window.addstr(11, 9, "PAUSE", curses.A_BOLD)
|
self.window.addstr(11, 9, "PAUSE", curses.A_BOLD)
|
||||||
else:
|
else:
|
||||||
for y, line in enumerate(self.cells):
|
for y, line in enumerate(self.cells):
|
||||||
for x, mino in enumerate(line):
|
for x, color in enumerate(line):
|
||||||
if mino:
|
if color:
|
||||||
self.draw_mino(x, y, mino.color)
|
self.draw_mino(x, y, color)
|
||||||
self.draw_piece()
|
self.draw_piece()
|
||||||
self.window.refresh()
|
self.window.refresh()
|
||||||
|
|
||||||
def is_free_cell(self, position):
|
def is_free_cell(self, position):
|
||||||
return (
|
return (
|
||||||
0 <= position.x < self.NB_COLS
|
0 <= position.x < self.NB_COLS
|
||||||
and position.y < self.NB_LINES
|
and position.y < self.NB_LINES
|
||||||
and not (position.y >= 0 and self.cells[position.y][position.x])
|
and not (position.y >= 0 and self.cells[position.y][position.x])
|
||||||
)
|
)
|
||||||
|
|
||||||
def lock(self, t_spin):
|
def lock(self, t_spin):
|
||||||
for mino in self.piece.minoes:
|
for mino in self.piece.minoes:
|
||||||
position = mino.position + self.piece.position
|
position = mino.position + self.piece.position
|
||||||
if position.y >= 0:
|
if position.y >= 0:
|
||||||
self.cells[position.y][position.x] = mino
|
self.cells[position.y][position.x] = mino.color
|
||||||
nb_lines_cleared = 0
|
nb_lines_cleared = 0
|
||||||
for y, line in enumerate(self.cells):
|
for y, line in enumerate(self.cells):
|
||||||
if all(mino for mino in line):
|
if all(mino for mino in line):
|
||||||
@ -377,37 +330,37 @@ class Matrix(Window):
|
|||||||
nb_lines_cleared += 1
|
nb_lines_cleared += 1
|
||||||
self.game.stats.piece_locked(nb_lines_cleared, t_spin)
|
self.game.stats.piece_locked(nb_lines_cleared, t_spin)
|
||||||
self.game.new_piece()
|
self.game.new_piece()
|
||||||
|
|
||||||
|
|
||||||
class HoldNext(Window):
|
class HoldNext(Window):
|
||||||
HEIGHT = 6
|
HEIGHT = 6
|
||||||
PIECE_POSITION = Point(6, 3)
|
PIECE_POSITION = Point(6, 3)
|
||||||
|
|
||||||
def __init__(self, width, begin_x, begin_y):
|
def __init__(self, width, begin_x, begin_y):
|
||||||
Window.__init__(self, width, self.HEIGHT, begin_x, begin_y)
|
Window.__init__(self, width, self.HEIGHT, begin_x, begin_y)
|
||||||
|
|
||||||
def refresh(self, paused=False):
|
def refresh(self, paused=False):
|
||||||
self.draw_border()
|
self.draw_border()
|
||||||
if not paused:
|
if not paused:
|
||||||
self.draw_piece()
|
self.draw_piece()
|
||||||
self.window.refresh()
|
self.window.refresh()
|
||||||
|
|
||||||
|
|
||||||
class Hold(HoldNext):
|
class Hold(HoldNext):
|
||||||
TITLE = "HOLD"
|
TITLE = "HOLD"
|
||||||
|
|
||||||
|
|
||||||
class Next(HoldNext):
|
class Next(HoldNext):
|
||||||
TITLE = "NEXT"
|
TITLE = "NEXT"
|
||||||
|
|
||||||
|
|
||||||
class Stats(Window):
|
class Stats(Window):
|
||||||
SCORES = (
|
SCORES = (
|
||||||
{"": 0, "MINI T-SPIN": 1, "T-SPIN": 4},
|
{"": 0, "MINI T-SPIN": 1, "T-SPIN": 4},
|
||||||
{"": 1, "MINI T-SPIN": 2, "T-SPIN": 8},
|
{"": 1, "MINI T-SPIN": 2, "T-SPIN": 8},
|
||||||
{"": 3, "T-SPIN": 12},
|
{"": 3, "T-SPIN": 12},
|
||||||
{"": 5, "T-SPIN": 16},
|
{"": 5, "T-SPIN": 16},
|
||||||
{"": 8}
|
{"": 8}
|
||||||
)
|
)
|
||||||
LINES_CLEARED_NAMES = ("", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS")
|
LINES_CLEARED_NAMES = ("", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS")
|
||||||
TITLE = "STATS"
|
TITLE = "STATS"
|
||||||
@ -418,7 +371,7 @@ class Stats(Window):
|
|||||||
DIR_PATH = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
|
DIR_PATH = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
|
||||||
DIR_PATH = os.path.join(DIR_PATH, DIR_NAME)
|
DIR_PATH = os.path.join(DIR_PATH, DIR_NAME)
|
||||||
FILE_PATH = os.path.join(DIR_PATH, FILE_NAME)
|
FILE_PATH = os.path.join(DIR_PATH, FILE_NAME)
|
||||||
|
|
||||||
def __init__(self, game, width, height, begin_x, begin_y):
|
def __init__(self, game, width, height, begin_x, begin_y):
|
||||||
if len(sys.argv) >= 2:
|
if len(sys.argv) >= 2:
|
||||||
try:
|
try:
|
||||||
@ -434,7 +387,7 @@ class Stats(Window):
|
|||||||
self.level -= 1
|
self.level -= 1
|
||||||
else:
|
else:
|
||||||
self.level = 0
|
self.level = 0
|
||||||
|
|
||||||
self.game = game
|
self.game = game
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
@ -452,7 +405,7 @@ class Stats(Window):
|
|||||||
self.strings = []
|
self.strings = []
|
||||||
Window.__init__(self, width, height, begin_x, begin_y)
|
Window.__init__(self, width, height, begin_x, begin_y)
|
||||||
self.new_level()
|
self.new_level()
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
self.draw_border()
|
self.draw_border()
|
||||||
self.window.addstr(2, 2, "SCORE\t{:n}".format(self.score))
|
self.window.addstr(2, 2, "SCORE\t{:n}".format(self.score))
|
||||||
@ -466,15 +419,15 @@ class Stats(Window):
|
|||||||
self.window.addstr(6, 2, "GOAL\t%d" % self.goal)
|
self.window.addstr(6, 2, "GOAL\t%d" % self.goal)
|
||||||
self.window.addstr(7, 2, "LINES\t%d" % self.lines_cleared)
|
self.window.addstr(7, 2, "LINES\t%d" % self.lines_cleared)
|
||||||
start_y = self.height - len(self.strings) - 2
|
start_y = self.height - len(self.strings) - 2
|
||||||
for y, string in enumerate(self.strings, start=start_y):
|
for y, string in enumerate(self.strings, start=start_y):
|
||||||
x = (self.width-len(string)) // 2 + 1
|
x = (self.width-len(string)) // 2 + 1
|
||||||
self.window.addstr(y, x, string)
|
self.window.addstr(y, x, string)
|
||||||
self.window.refresh()
|
self.window.refresh()
|
||||||
|
|
||||||
def clock(self):
|
def clock(self):
|
||||||
self.clock_timer = scheduler.enter(1, 3, self.clock, tuple())
|
self.clock_timer = scheduler.enter(1, 3, self.clock, tuple())
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def new_level(self):
|
def new_level(self):
|
||||||
self.level += 1
|
self.level += 1
|
||||||
Tetromino.fall_delay = pow(0.8 - ((self.level-1)*0.007), self.level-1)
|
Tetromino.fall_delay = pow(0.8 - ((self.level-1)*0.007), self.level-1)
|
||||||
@ -482,13 +435,13 @@ class Stats(Window):
|
|||||||
Tetromino.lock_delay = 0.5 * pow(0.9, self.level-15)
|
Tetromino.lock_delay = 0.5 * pow(0.9, self.level-15)
|
||||||
self.goal += 5 * self.level
|
self.goal += 5 * self.level
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def piece_dropped(self, lines):
|
def piece_dropped(self, lines):
|
||||||
self.score += lines
|
self.score += lines
|
||||||
if self.score > self.high_score:
|
if self.score > self.high_score:
|
||||||
self.high_score = self.score
|
self.high_score = self.score
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def piece_locked(self, nb_lines, t_spin):
|
def piece_locked(self, nb_lines, t_spin):
|
||||||
self.strings = []
|
self.strings = []
|
||||||
if t_spin:
|
if t_spin:
|
||||||
@ -518,8 +471,8 @@ class Stats(Window):
|
|||||||
self.new_level()
|
self.new_level()
|
||||||
else:
|
else:
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
if not os.path.exists(self.DIR_PATH):
|
if not os.path.exists(self.DIR_PATH):
|
||||||
os.mkdir(self.DIR_PATH)
|
os.mkdir(self.DIR_PATH)
|
||||||
try:
|
try:
|
||||||
@ -528,8 +481,8 @@ class Stats(Window):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("High score could not be saved:")
|
print("High score could not be saved:")
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
class Controls(Window, configparser.SafeConfigParser):
|
class Controls(Window, configparser.SafeConfigParser):
|
||||||
TITLE = "CONTROLS"
|
TITLE = "CONTROLS"
|
||||||
FILE_NAME = "config.cfg"
|
FILE_NAME = "config.cfg"
|
||||||
@ -539,7 +492,7 @@ class Controls(Window, configparser.SafeConfigParser):
|
|||||||
DIR_PATH = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
|
DIR_PATH = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
|
||||||
DIR_PATH = os.path.join(DIR_PATH, DIR_NAME)
|
DIR_PATH = os.path.join(DIR_PATH, DIR_NAME)
|
||||||
FILE_PATH = os.path.join(DIR_PATH, FILE_NAME)
|
FILE_PATH = os.path.join(DIR_PATH, FILE_NAME)
|
||||||
|
|
||||||
def __init__(self, width, height, begin_x, begin_y):
|
def __init__(self, width, height, begin_x, begin_y):
|
||||||
configparser.SafeConfigParser.__init__(self)
|
configparser.SafeConfigParser.__init__(self)
|
||||||
self.optionxform = str
|
self.optionxform = str
|
||||||
@ -582,14 +535,14 @@ class Controls(Window, configparser.SafeConfigParser):
|
|||||||
self.set("CONTROLS", action, "\n")
|
self.set("CONTROLS", action, "\n")
|
||||||
elif key == "TAB":
|
elif key == "TAB":
|
||||||
self.set("CONTROLS", action, "\t")
|
self.set("CONTROLS", action, "\t")
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
self.draw_border()
|
self.draw_border()
|
||||||
for y, (action, key) in enumerate(self.items("CONTROLS"), start=2):
|
for y, (action, key) in enumerate(self.items("CONTROLS"), start=2):
|
||||||
key = key.replace("KEY_", "").upper()
|
key = key.replace("KEY_", "").upper()
|
||||||
self.window.addstr(y, 2, "%s\t%s" % (key, action.upper()))
|
self.window.addstr(y, 2, "%s\t%s" % (key, action.upper()))
|
||||||
self.window.refresh()
|
self.window.refresh()
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
return self.get("CONTROLS", key)
|
return self.get("CONTROLS", key)
|
||||||
|
|
||||||
@ -597,10 +550,10 @@ class Game:
|
|||||||
WIDTH = 80
|
WIDTH = 80
|
||||||
HEIGHT = Matrix.HEIGHT
|
HEIGHT = Matrix.HEIGHT
|
||||||
AUTOREPEAT_DELAY = 0.02
|
AUTOREPEAT_DELAY = 0.02
|
||||||
|
|
||||||
def __init__(self, scr):
|
def __init__(self, scr):
|
||||||
self.scr = scr
|
self.scr = scr
|
||||||
|
|
||||||
if curses.has_colors():
|
if curses.has_colors():
|
||||||
curses.use_default_colors()
|
curses.use_default_colors()
|
||||||
curses.start_color()
|
curses.start_color()
|
||||||
@ -624,28 +577,28 @@ class Game:
|
|||||||
curses.init_pair(Color.MAGENTA, curses.COLOR_MAGENTA, curses.COLOR_MAGENTA)
|
curses.init_pair(Color.MAGENTA, curses.COLOR_MAGENTA, curses.COLOR_MAGENTA)
|
||||||
curses.init_pair(Color.CYAN, curses.COLOR_CYAN, curses.COLOR_CYAN)
|
curses.init_pair(Color.CYAN, curses.COLOR_CYAN, curses.COLOR_CYAN)
|
||||||
curses.init_pair(Color.WHITE, curses.COLOR_WHITE, curses.COLOR_WHITE)
|
curses.init_pair(Color.WHITE, curses.COLOR_WHITE, curses.COLOR_WHITE)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
except curses.error:
|
except curses.error:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.scr.timeout(0)
|
self.scr.timeout(0)
|
||||||
self.scr.getch()
|
self.scr.getch()
|
||||||
|
|
||||||
left_x = (curses.COLS-self.WIDTH) // 2
|
left_x = (curses.COLS-self.WIDTH) // 2
|
||||||
top_y = (curses.LINES-self.HEIGHT) // 2
|
top_y = (curses.LINES-self.HEIGHT) // 2
|
||||||
side_width = (self.WIDTH - Matrix.WIDTH) // 2
|
side_width = (self.WIDTH - Matrix.WIDTH) // 2
|
||||||
side_height = self.HEIGHT - Hold.HEIGHT
|
side_height = self.HEIGHT - Hold.HEIGHT
|
||||||
right_x = left_x + Matrix.WIDTH + side_width
|
right_x = left_x + Matrix.WIDTH + side_width
|
||||||
bottom_y = top_y + Hold.HEIGHT
|
bottom_y = top_y + Hold.HEIGHT
|
||||||
|
|
||||||
self.matrix = Matrix(self, left_x, top_y)
|
self.matrix = Matrix(self, left_x, top_y)
|
||||||
self.hold = Hold(side_width, left_x, top_y)
|
self.hold = Hold(side_width, left_x, top_y)
|
||||||
self.next = Next(side_width, right_x, top_y)
|
self.next = Next(side_width, right_x, top_y)
|
||||||
self.stats = Stats(self, side_width, side_height, left_x, bottom_y)
|
self.stats = Stats(self, side_width, side_height, left_x, bottom_y)
|
||||||
self.controls = Controls(side_width, side_height, right_x, bottom_y)
|
self.controls = Controls(side_width, side_height, right_x, bottom_y)
|
||||||
|
|
||||||
self.actions = {
|
self.actions = {
|
||||||
self.controls["QUIT"]: self.quit,
|
self.controls["QUIT"]: self.quit,
|
||||||
self.controls["PAUSE"]: self.pause,
|
self.controls["PAUSE"]: self.pause,
|
||||||
@ -657,7 +610,7 @@ class Game:
|
|||||||
self.controls["ROTATE CLOCKWISE"]: lambda: self.matrix.piece.rotate(Rotation.CLOCKWISE),
|
self.controls["ROTATE CLOCKWISE"]: lambda: self.matrix.piece.rotate(Rotation.CLOCKWISE),
|
||||||
self.controls["HARD DROP"]: lambda: self.matrix.piece.hard_drop()
|
self.controls["HARD DROP"]: lambda: self.matrix.piece.hard_drop()
|
||||||
}
|
}
|
||||||
|
|
||||||
self.playing = True
|
self.playing = True
|
||||||
self.paused = False
|
self.paused = False
|
||||||
self.stats.time = time.time()
|
self.stats.time = time.time()
|
||||||
@ -666,18 +619,18 @@ class Game:
|
|||||||
self.next.piece = self.random_piece()(self.matrix, Next.PIECE_POSITION)
|
self.next.piece = self.random_piece()(self.matrix, Next.PIECE_POSITION)
|
||||||
self.new_piece()
|
self.new_piece()
|
||||||
self.input_timer = scheduler.enter(self.AUTOREPEAT_DELAY, 2, self.process_input, tuple())
|
self.input_timer = scheduler.enter(self.AUTOREPEAT_DELAY, 2, self.process_input, tuple())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
scheduler.run()
|
scheduler.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
def random_piece(self):
|
def random_piece(self):
|
||||||
if not self.random_bag:
|
if not self.random_bag:
|
||||||
self.random_bag = [O, I, T, L, J, S, Z]
|
self.random_bag = [O, I, T, L, J, S, Z]
|
||||||
random.shuffle(self.random_bag)
|
random.shuffle(self.random_bag)
|
||||||
return self.random_bag.pop()
|
return self.random_bag.pop()
|
||||||
|
|
||||||
def new_piece(self, held_piece=None):
|
def new_piece(self, held_piece=None):
|
||||||
if not held_piece:
|
if not held_piece:
|
||||||
self.matrix.piece = self.next.piece
|
self.matrix.piece = self.next.piece
|
||||||
@ -688,7 +641,7 @@ class Game:
|
|||||||
self.matrix.piece.fall_timer = scheduler.enter(Tetromino.fall_delay, 2, self.matrix.piece.fall, tuple())
|
self.matrix.piece.fall_timer = scheduler.enter(Tetromino.fall_delay, 2, self.matrix.piece.fall, tuple())
|
||||||
else:
|
else:
|
||||||
self.over()
|
self.over()
|
||||||
|
|
||||||
def process_input(self):
|
def process_input(self):
|
||||||
self.input_timer = scheduler.enter(self.AUTOREPEAT_DELAY, 2, self.process_input, tuple())
|
self.input_timer = scheduler.enter(self.AUTOREPEAT_DELAY, 2, self.process_input, tuple())
|
||||||
try:
|
try:
|
||||||
@ -697,7 +650,7 @@ class Game:
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
action()
|
action()
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
self.stats.time = time.time() - self.stats.time
|
self.stats.time = time.time() - self.stats.time
|
||||||
self.paused = True
|
self.paused = True
|
||||||
@ -717,7 +670,7 @@ class Game:
|
|||||||
self.next.refresh()
|
self.next.refresh()
|
||||||
self.stats.time = time.time() - self.stats.time
|
self.stats.time = time.time() - self.stats.time
|
||||||
break
|
break
|
||||||
|
|
||||||
def swap(self):
|
def swap(self):
|
||||||
if self.matrix.piece.hold_enabled:
|
if self.matrix.piece.hold_enabled:
|
||||||
if self.matrix.piece.fall_timer:
|
if self.matrix.piece.fall_timer:
|
||||||
@ -733,7 +686,7 @@ class Game:
|
|||||||
self.hold.piece.hold_enabled = False
|
self.hold.piece.hold_enabled = False
|
||||||
self.hold.refresh()
|
self.hold.refresh()
|
||||||
self.new_piece(self.matrix.piece)
|
self.new_piece(self.matrix.piece)
|
||||||
|
|
||||||
def over(self):
|
def over(self):
|
||||||
self.matrix.refresh()
|
self.matrix.refresh()
|
||||||
self.matrix.window.addstr(10, 9, "GAME", curses.A_BOLD)
|
self.matrix.window.addstr(10, 9, "GAME", curses.A_BOLD)
|
||||||
@ -743,7 +696,7 @@ class Game:
|
|||||||
while self.scr.getkey() != self.controls["QUIT"]:
|
while self.scr.getkey() != self.controls["QUIT"]:
|
||||||
pass
|
pass
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
self.playing = False
|
self.playing = False
|
||||||
if self.matrix.piece.fall_timer:
|
if self.matrix.piece.fall_timer:
|
||||||
@ -759,11 +712,11 @@ class Game:
|
|||||||
scheduler.cancel(self.input_timer)
|
scheduler.cancel(self.input_timer)
|
||||||
self.input_timer = None
|
self.input_timer = None
|
||||||
self.stats.save()
|
self.stats.save()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
curses.wrapper(Game)
|
curses.wrapper(Game)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user