Remove class Color
This commit is contained in:
parent
a445955399
commit
27439076b0
@ -9,6 +9,8 @@ except ImportError:
|
|||||||
You can install it on Windows with:
|
You can install it on Windows with:
|
||||||
pip install --user windows-curses"""
|
pip install --user windows-curses"""
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
curses.COLOR_ORANGE = 8
|
||||||
import random
|
import random
|
||||||
import sched
|
import sched
|
||||||
import time
|
import time
|
||||||
@ -44,18 +46,6 @@ class Rotation:
|
|||||||
COUNTERCLOCKWISE = -1
|
COUNTERCLOCKWISE = -1
|
||||||
|
|
||||||
|
|
||||||
class Color:
|
|
||||||
BLACK = 0
|
|
||||||
WHITE = 1
|
|
||||||
YELLOW = 2
|
|
||||||
RED = 3
|
|
||||||
GREEN = 4
|
|
||||||
BLUE = 5
|
|
||||||
MAGENTA = 6
|
|
||||||
CYAN = 7
|
|
||||||
ORANGE = 8
|
|
||||||
|
|
||||||
|
|
||||||
class Point:
|
class Point:
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
self.x = x
|
self.x = x
|
||||||
@ -73,9 +63,11 @@ class Movement:
|
|||||||
|
|
||||||
|
|
||||||
class Mino:
|
class Mino:
|
||||||
|
color_pairs = [None for _ in range(9)]
|
||||||
|
|
||||||
def __init__(self, position, color):
|
def __init__(self, position, color):
|
||||||
self.position = position
|
self.position = position
|
||||||
self.color = color
|
self.color_pair = self.color_pairs[color]
|
||||||
|
|
||||||
|
|
||||||
class Tetromino:
|
class Tetromino:
|
||||||
@ -196,7 +188,7 @@ 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 = curses.COLOR_YELLOW
|
||||||
|
|
||||||
def rotate(self, direction):
|
def rotate(self, direction):
|
||||||
return False
|
return False
|
||||||
@ -221,11 +213,11 @@ class I(Tetromino):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
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 = curses.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 = curses.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):
|
||||||
@ -242,19 +234,19 @@ class T(Tetromino):
|
|||||||
|
|
||||||
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 = curses.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 = curses.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 = curses.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 = curses.COLOR_RED
|
||||||
|
|
||||||
|
|
||||||
class Window:
|
class Window:
|
||||||
@ -270,22 +262,22 @@ class Window:
|
|||||||
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)
|
||||||
|
|
||||||
def draw_piece(self):
|
def draw_piece(self):
|
||||||
if self.piece:
|
if self.piece:
|
||||||
if self.piece.lock_timer:
|
if self.piece.lock_timer:
|
||||||
attr = curses.A_BLINK | curses.A_REVERSE | curses.A_BOLD
|
attr = Mino.color_pairs[self.piece.COLOR] | curses.A_BLINK | curses.A_REVERSE
|
||||||
else:
|
else:
|
||||||
attr = curses.A_BOLD
|
attr = Mino.color_pairs[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, self.piece.COLOR, attr)
|
self.draw_mino(position.x, position.y, attr)
|
||||||
|
|
||||||
def draw_mino(self, x, y, color=Color.WHITE, attr=curses.A_BOLD):
|
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)|attr)
|
self.window.addstr(y, x*2+1, "██", color)
|
||||||
else:
|
else:
|
||||||
self.window.addstr(y, x*2+1, "██")
|
self.window.addstr(y, x*2+1, "██")
|
||||||
|
|
||||||
@ -303,7 +295,7 @@ class Matrix(Window):
|
|||||||
begin_y += (game.HEIGHT - self.HEIGHT) // 2
|
begin_y += (game.HEIGHT - self.HEIGHT) // 2
|
||||||
self.game = game
|
self.game = game
|
||||||
self.cells = [
|
self.cells = [
|
||||||
[Color.BLACK for x in range(self.NB_COLS)]
|
[curses.COLOR_BLACK for x in range(self.NB_COLS)]
|
||||||
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)
|
||||||
@ -331,7 +323,7 @@ class Matrix(Window):
|
|||||||
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.color
|
self.cells[position.y][position.x] = mino.color_pair
|
||||||
else:
|
else:
|
||||||
self.game.over()
|
self.game.over()
|
||||||
return
|
return
|
||||||
@ -340,7 +332,7 @@ class Matrix(Window):
|
|||||||
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):
|
||||||
self.cells.pop(y)
|
self.cells.pop(y)
|
||||||
self.cells.insert(0, [Color.BLACK for x in range(self.NB_COLS)])
|
self.cells.insert(0, [curses.COLOR_BLACK for x in range(self.NB_COLS)])
|
||||||
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()
|
||||||
@ -560,7 +552,7 @@ class ControlsParser(configparser.SafeConfigParser):
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
subprocess.call(["notepad.exe", self.FILE_PATH])
|
subprocess.call(["notepad.exe", self.FILE_PATH])
|
||||||
else:
|
else:
|
||||||
os.system("${EDITOR:-vi}"+" "+self.FILE_PATH)
|
os.system("${EDITOR:-nano}"+" "+self.FILE_PATH)
|
||||||
|
|
||||||
|
|
||||||
class ControlsWindow(Window, ControlsParser):
|
class ControlsWindow(Window, ControlsParser):
|
||||||
@ -590,51 +582,25 @@ class Game:
|
|||||||
WIDTH = 80
|
WIDTH = 80
|
||||||
HEIGHT = Matrix.HEIGHT
|
HEIGHT = Matrix.HEIGHT
|
||||||
AUTOREPEAT_DELAY = 0.02
|
AUTOREPEAT_DELAY = 0.02
|
||||||
COLOR_PAIRS = {
|
|
||||||
16: {
|
|
||||||
Color.ORANGE: (curses.COLOR_YELLOW, curses.COLOR_WHITE),
|
|
||||||
Color.RED: (curses.COLOR_RED+8, curses.COLOR_WHITE),
|
|
||||||
Color.GREEN: (curses.COLOR_GREEN+8, curses.COLOR_WHITE),
|
|
||||||
Color.YELLOW: (curses.COLOR_YELLOW+8, curses.COLOR_WHITE),
|
|
||||||
Color.BLUE: (curses.COLOR_BLUE+8, curses.COLOR_WHITE),
|
|
||||||
Color.MAGENTA: (curses.COLOR_MAGENTA+8, curses.COLOR_WHITE),
|
|
||||||
Color.CYAN: (curses.COLOR_CYAN+8, curses.COLOR_WHITE),
|
|
||||||
Color.WHITE: (curses.COLOR_WHITE+8, curses.COLOR_WHITE)
|
|
||||||
},
|
|
||||||
8: {
|
|
||||||
Color.ORANGE: (curses.COLOR_YELLOW, curses.COLOR_WHITE),
|
|
||||||
Color.RED: (curses.COLOR_RED, curses.COLOR_WHITE),
|
|
||||||
Color.GREEN: (curses.COLOR_GREEN, curses.COLOR_WHITE),
|
|
||||||
Color.YELLOW: (curses.COLOR_WHITE, curses.COLOR_WHITE),
|
|
||||||
Color.BLUE: (curses.COLOR_BLUE, curses.COLOR_WHITE),
|
|
||||||
Color.MAGENTA: (curses.COLOR_MAGENTA, curses.COLOR_WHITE),
|
|
||||||
Color.CYAN: (curses.COLOR_CYAN, curses.COLOR_WHITE),
|
|
||||||
Color.WHITE: (curses.COLOR_WHITE, curses.COLOR_WHITE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, scr):
|
def __init__(self, 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()
|
||||||
if curses.COLORS >= 16:
|
Mino.color_pairs[curses.COLOR_BLACK] = curses.color_pair(curses.COLOR_BLACK)
|
||||||
nb_colors = 16
|
for color in range(1, 8):
|
||||||
if curses.can_change_color():
|
curses.init_pair(color, color, curses.COLOR_WHITE)
|
||||||
curses.init_color(curses.COLOR_YELLOW, 1000, 500, 0)
|
Mino.color_pairs[color] = curses.color_pair(color)|curses.A_BOLD
|
||||||
else:
|
if curses.can_change_color():
|
||||||
nb_colors = 8
|
curses.init_color(curses.COLOR_YELLOW, 1000, 500, 0)
|
||||||
for color, (fg, bg) in self.COLOR_PAIRS[nb_colors].items():
|
Mino.color_pairs[curses.COLOR_ORANGE] = curses.color_pair(curses.COLOR_YELLOW)
|
||||||
curses.init_pair(color, fg, bg)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
except curses.error:
|
except curses.error:
|
||||||
pass
|
pass
|
||||||
|
scr.timeout(0)
|
||||||
self.scr.timeout(0)
|
scr.getch()
|
||||||
self.scr.getch()
|
self.scr = scr
|
||||||
|
|
||||||
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
|
||||||
@ -742,10 +708,8 @@ class Game:
|
|||||||
for y, word in enumerate((("GA", "ME") ,("OV", "ER")), start=Matrix.NB_LINES//2):
|
for y, word in enumerate((("GA", "ME") ,("OV", "ER")), start=Matrix.NB_LINES//2):
|
||||||
for x, char in enumerate(word, start=Matrix.NB_COLS//2-1):
|
for x, char in enumerate(word, start=Matrix.NB_COLS//2-1):
|
||||||
color = self.matrix.cells[y][x]
|
color = self.matrix.cells[y][x]
|
||||||
if color == Color.BLACK:
|
if color != Mino.color_pairs[curses.COLOR_BLACK]:
|
||||||
color = curses.color_pair(Color.BLACK) | curses.A_BOLD
|
color |= curses.A_REVERSE
|
||||||
else:
|
|
||||||
color = curses.color_pair(color) | curses.A_REVERSE | curses.A_BOLD
|
|
||||||
self.matrix.window.addstr(y, x*2+1, char, color)
|
self.matrix.window.addstr(y, x*2+1, char, color)
|
||||||
self.matrix.window.refresh()
|
self.matrix.window.refresh()
|
||||||
curses.beep()
|
curses.beep()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user