Compare commits
3 Commits
4edad3c1cf
...
59b21145f4
Author | SHA1 | Date | |
---|---|---|---|
|
59b21145f4 | ||
|
1437207c5d | ||
|
759c6f2054 |
@ -13,7 +13,7 @@ You can install it with:
|
|||||||
python -m pip install --user arcade"""
|
python -m pip install --user arcade"""
|
||||||
)
|
)
|
||||||
|
|
||||||
from tetrislogic import TetrisLogic, State
|
import tetrislogic
|
||||||
|
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
@ -21,7 +21,7 @@ from tetrislogic import TetrisLogic, State
|
|||||||
WINDOW_WIDTH = 800
|
WINDOW_WIDTH = 800
|
||||||
WINDOW_HEIGHT = 600
|
WINDOW_HEIGHT = 600
|
||||||
WINDOW_TITLE = "TETRARCADE"
|
WINDOW_TITLE = "TETRARCADE"
|
||||||
BG_COLOR = (7, 11, 21)
|
MINO_SIZE = 20
|
||||||
|
|
||||||
# Delays (seconds)
|
# Delays (seconds)
|
||||||
HIGHLIGHT_TEXT_DISPLAY_DELAY = 0.7
|
HIGHLIGHT_TEXT_DISPLAY_DELAY = 0.7
|
||||||
@ -30,9 +30,9 @@ HIGHLIGHT_TEXT_DISPLAY_DELAY = 0.7
|
|||||||
NORMAL_ALPHA = 200
|
NORMAL_ALPHA = 200
|
||||||
PRELOCKED_ALPHA = 100
|
PRELOCKED_ALPHA = 100
|
||||||
GHOST_ALPHA = 30
|
GHOST_ALPHA = 30
|
||||||
MATRIX_SPRITE_ALPHA = 100
|
MATRIX_BG_ALPHA = 100
|
||||||
|
|
||||||
# Paths
|
# Sprites
|
||||||
WINDOW_BG_PATH = "images/bg.jpg"
|
WINDOW_BG_PATH = "images/bg.jpg"
|
||||||
MATRIX_SPRITE_PATH = "images/matrix.png"
|
MATRIX_SPRITE_PATH = "images/matrix.png"
|
||||||
MINOES_SPRITES_PATHS = {
|
MINOES_SPRITES_PATHS = {
|
||||||
@ -44,6 +44,8 @@ MINOES_SPRITES_PATHS = {
|
|||||||
"red": "images/red_mino.png",
|
"red": "images/red_mino.png",
|
||||||
"magenta": "images/magenta_mino.png"
|
"magenta": "images/magenta_mino.png"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# User profile path
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
USER_PROFILE_DIR = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
|
USER_PROFILE_DIR = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
|
||||||
else:
|
else:
|
||||||
@ -58,7 +60,7 @@ FONT_NAME = "joystix monospace.ttf"
|
|||||||
TEXT_MARGIN = 40
|
TEXT_MARGIN = 40
|
||||||
FONT_SIZE = 16
|
FONT_SIZE = 16
|
||||||
TEXT_HEIGHT = 20.8
|
TEXT_HEIGHT = 20.8
|
||||||
HIGHLIGHT_TEXT_SIZE = 20
|
HIGHLIGHT_TEXT_FONT_SIZE = 20
|
||||||
|
|
||||||
CONTROL_TEXT = """
|
CONTROL_TEXT = """
|
||||||
|
|
||||||
@ -72,9 +74,7 @@ HARD DROP SPACE
|
|||||||
ROTATE CLOCKWISE ↑
|
ROTATE CLOCKWISE ↑
|
||||||
ROTATE COUNTER Z
|
ROTATE COUNTER Z
|
||||||
HOLD C
|
HOLD C
|
||||||
FULLSCREEN F11
|
|
||||||
PAUSE ESC
|
PAUSE ESC
|
||||||
QUIT ALT+F4
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -101,70 +101,56 @@ TO PLAY
|
|||||||
AGAIN"""
|
AGAIN"""
|
||||||
|
|
||||||
|
|
||||||
class ResizableSprite(arcade.Sprite):
|
class MinoSprite(arcade.Sprite):
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, mino, matrix_bg, alpha):
|
||||||
super().__init__(path)
|
super().__init__(MINOES_SPRITES_PATHS[mino.color])
|
||||||
self.init_width = self.width
|
self.alpha = alpha
|
||||||
self.init_height = self.height
|
self.matrix_bg = matrix_bg
|
||||||
|
|
||||||
def resize(self, ratio):
|
def set_position(self, x, y):
|
||||||
self.width = ratio * self.init_width
|
self.left = self.matrix_bg.left + x*MINO_SIZE
|
||||||
self.height = ratio * self.init_height
|
self.bottom = self.matrix_bg.bottom + y*MINO_SIZE
|
||||||
|
|
||||||
|
|
||||||
class MinoSprites(arcade.SpriteList):
|
class TetrominoSprites(arcade.SpriteList):
|
||||||
|
|
||||||
|
def __init__(self, tetromino, matrix_bg, alpha=NORMAL_ALPHA):
|
||||||
|
super().__init__()
|
||||||
|
self.tetromino = tetromino
|
||||||
|
self.matrix_bg = matrix_bg
|
||||||
|
for mino in tetromino:
|
||||||
|
mino.sprite = MinoSprite(mino, matrix_bg, alpha)
|
||||||
|
self.append(mino.sprite)
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
for mino in self.tetromino:
|
||||||
|
coord = mino.coord + self.tetromino.coord
|
||||||
|
mino.sprite.set_position(coord.x, coord.y)
|
||||||
|
super().update()
|
||||||
|
|
||||||
|
def set_alpha(self, alpha):
|
||||||
|
for sprite in self:
|
||||||
|
sprite.alpha = alpha
|
||||||
|
|
||||||
|
|
||||||
|
class MatrixSprites(arcade.SpriteList):
|
||||||
|
|
||||||
def __init__(self, matrix):
|
def __init__(self, matrix):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.matrix = matrix
|
self.matrix = matrix
|
||||||
|
self.update()
|
||||||
def update_mino(self, mino, x, y, alpha):
|
|
||||||
mino.sprite.left = self.matrix.sprite.left + x*(mino.sprite.width-1)
|
|
||||||
mino.sprite.bottom = self.matrix.sprite.bottom + y*(mino.sprite.height-1)
|
|
||||||
mino.sprite.alpha = alpha
|
|
||||||
|
|
||||||
def resize(self, ratio):
|
|
||||||
for sprite in self:
|
|
||||||
sprite.resize(ratio)
|
|
||||||
self.update(sprite)
|
|
||||||
|
|
||||||
|
|
||||||
class MatrixSprites(MinoSprites):
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
for sprite in self.matrix.sprites:
|
|
||||||
self.matrix.sprite.remove(sprite)
|
|
||||||
for y, line in enumerate(self.matrix):
|
for y, line in enumerate(self.matrix):
|
||||||
for x, mino in enumerate(line):
|
for x, mino in enumerate(line):
|
||||||
if mino:
|
if mino:
|
||||||
self.update_mino(mino, x, y, NORMAL_ALPHA)
|
mino.sprite.set_position(x, y)
|
||||||
self.append(mino.sprite)
|
self.append(mino.sprite)
|
||||||
|
super().update()
|
||||||
|
|
||||||
|
|
||||||
class TetrominoSprites(MinoSprites):
|
class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
||||||
|
|
||||||
def __init__(self, tetromino, matrix, alpha=NORMAL_ALPHA):
|
|
||||||
super().__init__(matrix)
|
|
||||||
self.tetromino = tetromino
|
|
||||||
path = MINOES_SPRITES_PATHS[tetromino.MINOES_COLOR]
|
|
||||||
self.alpha = alpha
|
|
||||||
for mino in tetromino:
|
|
||||||
mino.sprite = ResizableSprite(path)
|
|
||||||
self.append(mino.sprite)
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
alpha = (
|
|
||||||
PRELOCKED_ALPHA
|
|
||||||
if self.tetromino.prelocked
|
|
||||||
else self.alpha
|
|
||||||
)
|
|
||||||
for mino in self.tetromino:
|
|
||||||
coord = mino.coord + self.tetromino.coord
|
|
||||||
self.update_mino(mino, coord.x, coord.y, alpha)
|
|
||||||
|
|
||||||
|
|
||||||
class TetrArcade(TetrisLogic, arcade.Window):
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
@ -172,11 +158,10 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
self.tasks = {}
|
self.tasks = {}
|
||||||
|
|
||||||
self.KEY_MAP = {
|
self.KEY_MAP = {
|
||||||
State.STARTING: {
|
tetrislogic.State.STARTING: {
|
||||||
arcade.key.ENTER: self.new_game,
|
arcade.key.ENTER: self.new_game
|
||||||
arcade.key.F11: self.toogle_fullscreen
|
|
||||||
},
|
},
|
||||||
State.PLAYING: {
|
tetrislogic.State.PLAYING: {
|
||||||
arcade.key.LEFT: self.move_left,
|
arcade.key.LEFT: self.move_left,
|
||||||
arcade.key.NUM_4: self.move_left,
|
arcade.key.NUM_4: self.move_left,
|
||||||
arcade.key.RIGHT: self.move_right,
|
arcade.key.RIGHT: self.move_right,
|
||||||
@ -198,16 +183,13 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
arcade.key.NUM_0: self.swap,
|
arcade.key.NUM_0: self.swap,
|
||||||
arcade.key.ESCAPE: self.pause,
|
arcade.key.ESCAPE: self.pause,
|
||||||
arcade.key.F1: self.pause,
|
arcade.key.F1: self.pause,
|
||||||
arcade.key.F11: self.toogle_fullscreen
|
|
||||||
},
|
},
|
||||||
State.PAUSED: {
|
tetrislogic.State.PAUSED: {
|
||||||
arcade.key.ESCAPE: self.resume,
|
arcade.key.ESCAPE: self.resume,
|
||||||
arcade.key.F1: self.resume,
|
arcade.key.F1: self.resume
|
||||||
arcade.key.F11: self.toogle_fullscreen
|
|
||||||
},
|
},
|
||||||
State.OVER: {
|
tetrislogic.State.OVER: {
|
||||||
arcade.key.ENTER: self.new_game,
|
arcade.key.ENTER: self.new_game
|
||||||
arcade.key.F11: self.toogle_fullscreen
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,15 +200,21 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
width = WINDOW_WIDTH,
|
width = WINDOW_WIDTH,
|
||||||
height = WINDOW_HEIGHT,
|
height = WINDOW_HEIGHT,
|
||||||
title = WINDOW_TITLE,
|
title = WINDOW_TITLE,
|
||||||
resizable = True,
|
resizable = False,
|
||||||
antialiasing = False
|
antialiasing = False
|
||||||
)
|
)
|
||||||
|
|
||||||
self.init_width = self.width
|
center_x = WINDOW_WIDTH / 2
|
||||||
self.init_height = self.height
|
center_y = WINDOW_HEIGHT / 2
|
||||||
self.sprite = ResizableSprite(WINDOW_BG_PATH)
|
self.bg = arcade.Sprite(WINDOW_BG_PATH)
|
||||||
self.matrix.sprite = ResizableSprite(MATRIX_SPRITE_PATH)
|
self.bg.center_x = center_x
|
||||||
self.matrix.sprite.alpha = MATRIX_SPRITE_ALPHA
|
self.bg.center_y = center_y
|
||||||
|
self.matrix_bg = arcade.Sprite(MATRIX_SPRITE_PATH)
|
||||||
|
self.matrix_bg.alpha = MATRIX_BG_ALPHA
|
||||||
|
self.matrix_bg.center_x = center_x
|
||||||
|
self.matrix_bg.center_y = center_y
|
||||||
|
self.matrix_bg.left = int(self.matrix_bg.left)
|
||||||
|
self.matrix_bg.top = int(self.matrix_bg.top)
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
self.stats_text = arcade.create_text(
|
self.stats_text = arcade.create_text(
|
||||||
text = STATS_TEXT,
|
text = STATS_TEXT,
|
||||||
@ -235,63 +223,33 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
font_name = FONT_NAME,
|
font_name = FONT_NAME,
|
||||||
anchor_x = 'right'
|
anchor_x = 'right'
|
||||||
)
|
)
|
||||||
self.highlight_text_size = HIGHLIGHT_TEXT_SIZE
|
|
||||||
arcade.set_background_color(BG_COLOR)
|
|
||||||
|
|
||||||
def on_resize(self, width, height):
|
|
||||||
super().on_resize(width, height)
|
|
||||||
center_x = width / 2
|
|
||||||
center_y = height / 2
|
|
||||||
self.sprite.center_x = center_x
|
|
||||||
self.sprite.center_y = center_y
|
|
||||||
self.matrix.sprite.center_x = center_x
|
|
||||||
self.matrix.sprite.center_y = center_y
|
|
||||||
self.matrix.sprite.left = int(self.matrix.sprite.left)
|
|
||||||
self.matrix.sprite.top = int(self.matrix.sprite.top)
|
|
||||||
ratio = min(
|
|
||||||
width / self.init_width,
|
|
||||||
height / self.init_height
|
|
||||||
)
|
|
||||||
for sprite in [
|
|
||||||
self.sprite,
|
|
||||||
self.matrix.sprite
|
|
||||||
]:
|
|
||||||
sprite.resize(ratio)
|
|
||||||
for minoes in [self.matrix, self.held, self.current, self.ghost] + self.next:
|
|
||||||
minoes.sprites.resize(ratio)
|
|
||||||
self.font_size = FONT_SIZE * ratio
|
|
||||||
self.stats_text = arcade.create_text(
|
|
||||||
text = STATS_TEXT,
|
|
||||||
color = TEXT_COLOR,
|
|
||||||
font_size = FONT_SIZE,
|
|
||||||
font_name = FONT_NAME,
|
|
||||||
anchor_x = 'right'
|
|
||||||
)
|
|
||||||
self.highlight_text_size = HIGHLIGHT_TEXT_SIZE * ratio
|
|
||||||
|
|
||||||
def toogle_fullscreen(self):
|
|
||||||
self.fullscreen = not self.fullscreen
|
|
||||||
|
|
||||||
def new_game(self):
|
def new_game(self):
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
|
||||||
super().new_game()
|
super().new_game()
|
||||||
|
|
||||||
def new_next(self):
|
def new_tetromino(self):
|
||||||
super().new_next()
|
tetromino = super().new_tetromino()
|
||||||
self.next[-1].sprites = TetrominoSprites(self.next[-1], self.matrix)
|
tetromino.sprites = TetrominoSprites(tetromino, self.matrix_bg)
|
||||||
|
return tetromino
|
||||||
|
|
||||||
def new_current(self):
|
def new_current(self):
|
||||||
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
super().new_current()
|
super().new_current()
|
||||||
self.ghost.sprites = TetrominoSprites(self.ghost, self.matrix, GHOST_ALPHA)
|
self.ghost.sprites = TetrominoSprites(self.ghost, self.matrix_bg, GHOST_ALPHA)
|
||||||
for tetromino in [self.current, self.ghost] + self.next:
|
for tetromino in [self.current, self.ghost] + self.next:
|
||||||
tetromino.sprites.update()
|
tetromino.sprites.update()
|
||||||
|
|
||||||
def move(self, movement, prelock=True):
|
def move(self, movement, prelock=True):
|
||||||
moved = super().move(movement, prelock)
|
moved = super().move(movement, prelock)
|
||||||
if moved or self.current.prelocked:
|
if self.current.prelocked:
|
||||||
for tetromino in (self.current, self.ghost):
|
self.current.sprites.set_alpha(PRELOCKED_ALPHA)
|
||||||
tetromino.sprites.update()
|
if moved:
|
||||||
|
change_x = movement.x * MINO_SIZE
|
||||||
|
change_y = movement.y * MINO_SIZE
|
||||||
|
self.current.sprites.move(change_x, change_y)
|
||||||
|
if movement in (tetrislogic.Movement.LEFT, tetrislogic.Movement.RIGHT):
|
||||||
|
self.ghost.sprites.update()
|
||||||
return moved
|
return moved
|
||||||
|
|
||||||
def rotate(self, rotation):
|
def rotate(self, rotation):
|
||||||
@ -303,7 +261,7 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
def swap(self):
|
def swap(self):
|
||||||
super().swap()
|
super().swap()
|
||||||
self.ghost.sprites = TetrominoSprites(self.ghost, self.matrix, GHOST_ALPHA)
|
self.ghost = TetrominoSprites(self.ghost, self.matrix_bg, GHOST_ALPHA)
|
||||||
for tetromino in [self.held, self.current, self.ghost]:
|
for tetromino in [self.held, self.current, self.ghost]:
|
||||||
if tetromino:
|
if tetromino:
|
||||||
tetromino.sprites.update()
|
tetromino.sprites.update()
|
||||||
@ -311,10 +269,6 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
def lock(self):
|
def lock(self):
|
||||||
self.current.sprites.update()
|
self.current.sprites.update()
|
||||||
super().lock()
|
super().lock()
|
||||||
self.matrix.sprites.update()
|
|
||||||
|
|
||||||
def game_over(self):
|
|
||||||
super().game_over()
|
|
||||||
|
|
||||||
def on_key_press(self, key, modifiers):
|
def on_key_press(self, key, modifiers):
|
||||||
for key_or_modifier in (key, modifiers):
|
for key_or_modifier in (key, modifiers):
|
||||||
@ -346,19 +300,20 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
def on_draw(self):
|
def on_draw(self):
|
||||||
arcade.start_render()
|
arcade.start_render()
|
||||||
self.sprite.draw()
|
self.bg.draw()
|
||||||
|
|
||||||
if self.state in (State.PLAYING, State.OVER):
|
if self.state in (tetrislogic.State.PLAYING, tetrislogic.State.OVER):
|
||||||
self.matrix.sprite.draw()
|
self.matrix_bg.draw()
|
||||||
self.matrix.sprites.draw()
|
self.matrix.sprites.draw()
|
||||||
|
|
||||||
for tetromino in [self.held, self.current, self.ghost] + self.next:
|
for tetromino in [self.held, self.current, self.ghost] + self.next:
|
||||||
if tetromino:
|
if tetromino:
|
||||||
tetromino.sprites.draw()
|
tetromino.sprites.draw()
|
||||||
|
|
||||||
arcade.render_text(
|
arcade.render_text(
|
||||||
self.stats_text,
|
self.stats_text,
|
||||||
self.matrix.sprite.left - TEXT_MARGIN,
|
self.matrix_bg.left - TEXT_MARGIN,
|
||||||
self.matrix.sprite.bottom
|
self.matrix_bg.bottom
|
||||||
)
|
)
|
||||||
t = time.localtime(self.time)
|
t = time.localtime(self.time)
|
||||||
for y, text in enumerate(
|
for y, text in enumerate(
|
||||||
@ -376,8 +331,8 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
):
|
):
|
||||||
arcade.draw_text(
|
arcade.draw_text(
|
||||||
text = text,
|
text = text,
|
||||||
start_x = self.matrix.sprite.left - TEXT_MARGIN,
|
start_x = self.matrix_bg.left - TEXT_MARGIN,
|
||||||
start_y = self.matrix.sprite.bottom + 2*y*TEXT_HEIGHT,
|
start_y = self.matrix_bg.bottom + 2*y*TEXT_HEIGHT,
|
||||||
color = TEXT_COLOR,
|
color = TEXT_COLOR,
|
||||||
font_size = FONT_SIZE,
|
font_size = FONT_SIZE,
|
||||||
align = 'right',
|
align = 'right',
|
||||||
@ -386,18 +341,18 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
)
|
)
|
||||||
|
|
||||||
highlight_text = {
|
highlight_text = {
|
||||||
State.STARTING: START_TEXT,
|
tetrislogic.State.STARTING: START_TEXT,
|
||||||
State.PLAYING: self.highlight_texts[0] if self.highlight_texts else "",
|
tetrislogic.State.PLAYING: self.highlight_texts[0] if self.highlight_texts else "",
|
||||||
State.PAUSED: PAUSE_TEXT,
|
tetrislogic.State.PAUSED: PAUSE_TEXT,
|
||||||
State.OVER: GAME_OVER_TEXT
|
tetrislogic.State.OVER: GAME_OVER_TEXT
|
||||||
}.get(self.state, "")
|
}.get(self.state, "")
|
||||||
if highlight_text:
|
if highlight_text:
|
||||||
arcade.draw_text(
|
arcade.draw_text(
|
||||||
text = highlight_text,
|
text = highlight_text,
|
||||||
start_x = self.matrix.sprite.center_x,
|
start_x = self.matrix_bg.center_x,
|
||||||
start_y = self.matrix.sprite.center_y,
|
start_y = self.matrix_bg.center_y,
|
||||||
color = HIGHLIGHT_TEXT_COLOR,
|
color = HIGHLIGHT_TEXT_COLOR,
|
||||||
font_size = self.highlight_text_size,
|
font_size = HIGHLIGHT_TEXT_FONT_SIZE,
|
||||||
align = 'center',
|
align = 'center',
|
||||||
font_name = FONT_NAME,
|
font_name = FONT_NAME,
|
||||||
anchor_x = 'center',
|
anchor_x = 'center',
|
@ -1,3 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from .consts import NB_LINES, NB_COLS, NB_NEXT
|
from .consts import NB_LINES, NB_COLS, NB_NEXT
|
||||||
from .tetrislogic import TetrisLogic, State
|
from .utils import Movement, Rotation
|
||||||
|
from .tetromino import Mino, Tetromino
|
||||||
|
from .tetrislogic import TetrisLogic, State, Matrix
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from .utils import Coord, Movement, Rotation, T_Spin, Line
|
from .utils import Coord, Movement, Rotation, T_Spin, Line
|
||||||
from .tetromino import Tetromino
|
from .tetromino import Tetromino, T
|
||||||
from .consts import (
|
from .consts import (
|
||||||
NB_LINES, NB_COLS, NB_NEXT,
|
NB_LINES, NB_COLS, NB_NEXT,
|
||||||
LOCK_DELAY, FALL_DELAY,
|
LOCK_DELAY, FALL_DELAY,
|
||||||
@ -71,17 +71,15 @@ class TetrisLogic():
|
|||||||
self.matrix.clear()
|
self.matrix.clear()
|
||||||
for y in range(NB_LINES+3):
|
for y in range(NB_LINES+3):
|
||||||
self.append_new_line_to_matrix()
|
self.append_new_line_to_matrix()
|
||||||
self.next = []
|
self.next = [self.new_tetromino() for n in range(NB_NEXT)]
|
||||||
for n in range(NB_NEXT):
|
|
||||||
self.new_next()
|
|
||||||
self.held = None
|
self.held = None
|
||||||
self.state = State.PLAYING
|
self.state = State.PLAYING
|
||||||
self.start(self.update_time, 1)
|
self.start(self.update_time, 1)
|
||||||
|
|
||||||
self.new_level()
|
self.new_level()
|
||||||
|
|
||||||
def new_next(self):
|
def new_tetromino(self):
|
||||||
self.next.append(Tetromino())
|
return Tetromino()
|
||||||
|
|
||||||
def append_new_line_to_matrix(self):
|
def append_new_line_to_matrix(self):
|
||||||
self.matrix.append(Line(None for x in range(NB_COLS)))
|
self.matrix.append(Line(None for x in range(NB_COLS)))
|
||||||
@ -103,7 +101,7 @@ class TetrisLogic():
|
|||||||
self.current.coord = CURRENT_COORD
|
self.current.coord = CURRENT_COORD
|
||||||
self.ghost = self.current.ghost()
|
self.ghost = self.current.ghost()
|
||||||
self.move_ghost()
|
self.move_ghost()
|
||||||
self.new_next()
|
self.next.append(self.new_tetromino())
|
||||||
self.next[-1].coord = NEXT_COORDS[-1]
|
self.next[-1].coord = NEXT_COORDS[-1]
|
||||||
for tetromino, coord in zip (self.next, NEXT_COORDS):
|
for tetromino, coord in zip (self.next, NEXT_COORDS):
|
||||||
tetromino.coord = coord
|
tetromino.coord = coord
|
||||||
@ -211,7 +209,6 @@ class TetrisLogic():
|
|||||||
self.current.coord + Movement.DOWN,
|
self.current.coord + Movement.DOWN,
|
||||||
(mino.coord for mino in self.current)
|
(mino.coord for mino in self.current)
|
||||||
):
|
):
|
||||||
self.restart(self.lock, self.lock_delay)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Start lock
|
# Start lock
|
||||||
@ -231,7 +228,7 @@ class TetrisLogic():
|
|||||||
|
|
||||||
# T-Spin
|
# T-Spin
|
||||||
if (
|
if (
|
||||||
self.current.__class__ == Tetromino.T
|
type(self.current) == T
|
||||||
and self.current.last_rotation_point is not None
|
and self.current.last_rotation_point is not None
|
||||||
):
|
):
|
||||||
a = self.is_t_slot(0)
|
a = self.is_t_slot(0)
|
||||||
|
@ -10,115 +10,114 @@ class Mino:
|
|||||||
self.coord = coord
|
self.coord = coord
|
||||||
|
|
||||||
|
|
||||||
|
class MetaTetromino(type):
|
||||||
|
|
||||||
|
def __init__(cls, name, bases, dct):
|
||||||
|
super().__init__(name, bases, dct)
|
||||||
|
Tetromino.shapes.append(cls)
|
||||||
|
|
||||||
|
|
||||||
class Tetromino:
|
class Tetromino:
|
||||||
|
|
||||||
|
shapes = []
|
||||||
random_bag = []
|
random_bag = []
|
||||||
|
|
||||||
|
|
||||||
class MetaTetromino(type):
|
|
||||||
|
|
||||||
def __init__(cls, name, bases, dico):
|
|
||||||
super().__init__(name, bases, dico)
|
|
||||||
cls.classes.append(cls)
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractTetromino(list):
|
|
||||||
|
|
||||||
# Super rotation system
|
|
||||||
SRS = {
|
|
||||||
Rotation.CLOCKWISE: (
|
|
||||||
(Coord(0, 0), Coord(-1, 0), Coord(-1, 1), Coord(0, -2), Coord(-1, -2)),
|
|
||||||
(Coord(0, 0), Coord(1, 0), Coord(1, -1), Coord(0, 2), Coord(1, 2)),
|
|
||||||
(Coord(0, 0), Coord(1, 0), Coord(1, 1), Coord(0, -2), Coord(1, -2)),
|
|
||||||
(Coord(0, 0), Coord(-1, 0), Coord(-1, -1), Coord(0, -2), Coord(-1, 2)),
|
|
||||||
),
|
|
||||||
Rotation.COUNTER: (
|
|
||||||
(Coord(0, 0), Coord(1, 0), Coord(1, 1), Coord(0, -2), Coord(1, -2)),
|
|
||||||
(Coord(0, 0), Coord(1, 0), Coord(1, -1), Coord(0, 2), Coord(1, 2)),
|
|
||||||
(Coord(0, 0), Coord(-1, 0), Coord(-1, 1), Coord(0, -2), Coord(-1, -2)),
|
|
||||||
(Coord(0, 0), Coord(-1, 0), Coord(-1, -1), Coord(0, 2), Coord(-1, 2)),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
classes = []
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__(
|
|
||||||
Mino(self.MINOES_COLOR, coord)
|
|
||||||
for coord in self.MINOES_COORDS
|
|
||||||
)
|
|
||||||
self.orientation = 0
|
|
||||||
self.last_rotation_point = None
|
|
||||||
self.hold_enabled = True
|
|
||||||
self.prelocked = False
|
|
||||||
|
|
||||||
def ghost(self):
|
|
||||||
return self.__class__()
|
|
||||||
|
|
||||||
class O(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
SRS = {
|
|
||||||
Rotation.CLOCKWISE: (tuple(), tuple(), tuple(), tuple()),
|
|
||||||
Rotation.COUNTER: (tuple(), tuple(), tuple(), tuple()),
|
|
||||||
}
|
|
||||||
MINOES_COORDS = (Coord(0, 0), Coord(1, 0), Coord(0, 1), Coord(1, 1))
|
|
||||||
MINOES_COLOR = "yellow"
|
|
||||||
|
|
||||||
def rotate(self, direction):
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class I(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
SRS = {
|
|
||||||
Rotation.CLOCKWISE: (
|
|
||||||
(Coord(1, 0), Coord(-1, 0), Coord(2, 0), Coord(-1, -1), Coord(2, 2)),
|
|
||||||
(Coord(0, -1), Coord(-1, -1), Coord(2, -1), Coord(-1, 1), Coord(2, -2)),
|
|
||||||
(Coord(-1, 0), Coord(1, 0), Coord(-2, 0), Coord(1, 1), Coord(-2, -2)),
|
|
||||||
(Coord(0, -1), Coord(1, 1), Coord(-2, 1), Coord(1, -1), Coord(-2, 2)),
|
|
||||||
),
|
|
||||||
Rotation.COUNTER: (
|
|
||||||
(Coord(0, -1), Coord(-1, -1), Coord(2, -1), Coord(-1, 1), Coord(2, -2)),
|
|
||||||
(Coord(-1, 0), Coord(1, 0), Coord(-2, 0), Coord(1, 1), Coord(-2, -2)),
|
|
||||||
(Coord(0, 1), Coord(1, 1), Coord(-2, 1), Coord(1, -1), Coord(-2, 2)),
|
|
||||||
(Coord(1, 0), Coord(-1, 0), Coord(2, 0), Coord(-1, -1), Coord(2, 2)),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(1, 0), Coord(2, 0))
|
|
||||||
MINOES_COLOR = "cyan"
|
|
||||||
|
|
||||||
|
|
||||||
class T(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(0, 1), Coord(1, 0))
|
|
||||||
MINOES_COLOR = "magenta"
|
|
||||||
|
|
||||||
|
|
||||||
class L(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(1, 0), Coord(1, 1))
|
|
||||||
MINOES_COLOR = "orange"
|
|
||||||
|
|
||||||
|
|
||||||
class J(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
MINOES_COORDS = (Coord(-1, 1), Coord(-1, 0), Coord(0, 0), Coord(1, 0))
|
|
||||||
MINOES_COLOR = "blue"
|
|
||||||
|
|
||||||
|
|
||||||
class S(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(0, 1), Coord(1, 1))
|
|
||||||
MINOES_COLOR = "green"
|
|
||||||
|
|
||||||
|
|
||||||
class Z(AbstractTetromino, metaclass=MetaTetromino):
|
|
||||||
|
|
||||||
MINOES_COORDS = (Coord(-1, 1), Coord(0, 1), Coord(0, 0), Coord(1, 0))
|
|
||||||
MINOES_COLOR = "red"
|
|
||||||
|
|
||||||
|
|
||||||
def __new__(cls):
|
def __new__(cls):
|
||||||
if not cls.random_bag:
|
if not cls.random_bag:
|
||||||
cls.random_bag = list(Tetromino.AbstractTetromino.classes)
|
cls.random_bag = list(cls.shapes)
|
||||||
random.shuffle(cls.random_bag)
|
random.shuffle(cls.random_bag)
|
||||||
return cls.random_bag.pop()()
|
return cls.random_bag.pop()()
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractTetromino(list):
|
||||||
|
|
||||||
|
# Super rotation system
|
||||||
|
SRS = {
|
||||||
|
Rotation.CLOCKWISE: (
|
||||||
|
(Coord(0, 0), Coord(-1, 0), Coord(-1, 1), Coord(0, -2), Coord(-1, -2)),
|
||||||
|
(Coord(0, 0), Coord(1, 0), Coord(1, -1), Coord(0, 2), Coord(1, 2)),
|
||||||
|
(Coord(0, 0), Coord(1, 0), Coord(1, 1), Coord(0, -2), Coord(1, -2)),
|
||||||
|
(Coord(0, 0), Coord(-1, 0), Coord(-1, -1), Coord(0, -2), Coord(-1, 2)),
|
||||||
|
),
|
||||||
|
Rotation.COUNTER: (
|
||||||
|
(Coord(0, 0), Coord(1, 0), Coord(1, 1), Coord(0, -2), Coord(1, -2)),
|
||||||
|
(Coord(0, 0), Coord(1, 0), Coord(1, -1), Coord(0, 2), Coord(1, 2)),
|
||||||
|
(Coord(0, 0), Coord(-1, 0), Coord(-1, 1), Coord(0, -2), Coord(-1, -2)),
|
||||||
|
(Coord(0, 0), Coord(-1, 0), Coord(-1, -1), Coord(0, 2), Coord(-1, 2)),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
|
Mino(self.MINOES_COLOR, coord)
|
||||||
|
for coord in self.MINOES_COORDS
|
||||||
|
)
|
||||||
|
self.orientation = 0
|
||||||
|
self.last_rotation_point = None
|
||||||
|
self.hold_enabled = True
|
||||||
|
self.prelocked = False
|
||||||
|
|
||||||
|
def ghost(self):
|
||||||
|
return self.__class__()
|
||||||
|
|
||||||
|
class O(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
SRS = {
|
||||||
|
Rotation.CLOCKWISE: (tuple(), tuple(), tuple(), tuple()),
|
||||||
|
Rotation.COUNTER: (tuple(), tuple(), tuple(), tuple()),
|
||||||
|
}
|
||||||
|
MINOES_COORDS = (Coord(0, 0), Coord(1, 0), Coord(0, 1), Coord(1, 1))
|
||||||
|
MINOES_COLOR = "yellow"
|
||||||
|
|
||||||
|
def rotate(self, direction):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class I(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
SRS = {
|
||||||
|
Rotation.CLOCKWISE: (
|
||||||
|
(Coord(1, 0), Coord(-1, 0), Coord(2, 0), Coord(-1, -1), Coord(2, 2)),
|
||||||
|
(Coord(0, -1), Coord(-1, -1), Coord(2, -1), Coord(-1, 1), Coord(2, -2)),
|
||||||
|
(Coord(-1, 0), Coord(1, 0), Coord(-2, 0), Coord(1, 1), Coord(-2, -2)),
|
||||||
|
(Coord(0, -1), Coord(1, 1), Coord(-2, 1), Coord(1, -1), Coord(-2, 2)),
|
||||||
|
),
|
||||||
|
Rotation.COUNTER: (
|
||||||
|
(Coord(0, -1), Coord(-1, -1), Coord(2, -1), Coord(-1, 1), Coord(2, -2)),
|
||||||
|
(Coord(-1, 0), Coord(1, 0), Coord(-2, 0), Coord(1, 1), Coord(-2, -2)),
|
||||||
|
(Coord(0, 1), Coord(1, 1), Coord(-2, 1), Coord(1, -1), Coord(-2, 2)),
|
||||||
|
(Coord(1, 0), Coord(-1, 0), Coord(2, 0), Coord(-1, -1), Coord(2, 2)),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(1, 0), Coord(2, 0))
|
||||||
|
MINOES_COLOR = "cyan"
|
||||||
|
|
||||||
|
|
||||||
|
class T(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(0, 1), Coord(1, 0))
|
||||||
|
MINOES_COLOR = "magenta"
|
||||||
|
|
||||||
|
|
||||||
|
class L(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(1, 0), Coord(1, 1))
|
||||||
|
MINOES_COLOR = "orange"
|
||||||
|
|
||||||
|
|
||||||
|
class J(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
MINOES_COORDS = (Coord(-1, 1), Coord(-1, 0), Coord(0, 0), Coord(1, 0))
|
||||||
|
MINOES_COLOR = "blue"
|
||||||
|
|
||||||
|
|
||||||
|
class S(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(0, 1), Coord(1, 1))
|
||||||
|
MINOES_COLOR = "green"
|
||||||
|
|
||||||
|
|
||||||
|
class Z(AbstractTetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
|
MINOES_COORDS = (Coord(-1, 1), Coord(0, 1), Coord(0, 0), Coord(1, 0))
|
||||||
|
MINOES_COLOR = "red"
|
Loading…
x
Reference in New Issue
Block a user