Compare commits
90 Commits
59b21145f4
...
master
Author | SHA1 | Date | |
---|---|---|---|
eca466519b | |||
f5cfb2221f | |||
250e79c458 | |||
d85c63701c | |||
4df3c6c9ba | |||
5ed15da4ed | |||
a3dc434c88 | |||
2895570f6e | |||
0815409953 | |||
8e6359bfa3 | |||
0d7470fd51 | |||
fe93336bb9 | |||
578b126b3e | |||
6135e24eac | |||
ee7e6fcdb9 | |||
e7f3146e9a | |||
deba1a2daf | |||
c5c21c5017 | |||
28a8ea0953 | |||
5367e77149 | |||
af005f72ca | |||
363a89a590 | |||
f9c1fe4688 | |||
a0a414db14 | |||
4522ac1d4b | |||
e3e05e87d7 | |||
82f2b74e68 | |||
504ebf8e51 | |||
4452eb821c | |||
e041a8118a | |||
0db5dd4d0d | |||
fe69557bc6 | |||
32bf60313c | |||
f013a061b2 | |||
f025ad5fd8 | |||
9a7aead918 | |||
b173b6ff73 | |||
ddf7ea0f4e | |||
d308618556 | |||
9c77096bfb | |||
093264c351 | |||
5b3e6ec931 | |||
0970e1f6df | |||
f48f1fc000 | |||
5f137a62ec | |||
ec42f17ca7 | |||
0d6386d22b | |||
abaeb3be9a | |||
dff4ae487a | |||
5c830fd828 | |||
a46c07af8b | |||
338371f443 | |||
a95463438e | |||
06fe72d8db | |||
4a69c12349 | |||
2bd75be892 | |||
3015a36984 | |||
7fc342e061 | |||
0e68ea4e51 | |||
b95478ea8d | |||
df8257c4da | |||
0b3dd847d3 | |||
2a7900586c | |||
be66bada11 | |||
02e4aa066d | |||
5b6cfd6512 | |||
f1ffb1a7c6 | |||
dda475a584 | |||
b8e20199af | |||
4c44089f41 | |||
f2bbafeb8b | |||
54f2554b2a | |||
5f10bd782e | |||
9aa461fe07 | |||
ca0a18a8c5 | |||
ecd2915b12 | |||
ab8afe6fdd | |||
fa9a323af1 | |||
c2ec677f3f | |||
11c6321f17 | |||
58a7736d53 | |||
578f33ae64 | |||
00e2adf60c | |||
b1cef21f00 | |||
2c4808312f | |||
9db4f4d122 | |||
850aad353e | |||
97b687c085 | |||
a654458610 | |||
f7a47efecf |
21
README.md
@ -2,9 +2,12 @@
|
||||
|
||||
Tetris clone made with Python and Arcade graphic library
|
||||
|
||||

|
||||
|
||||
## Requirements
|
||||
|
||||
* [Python](https://www.python.org/)
|
||||
* [Python 3.6 or later](https://www.python.org/)
|
||||
* [FFmpeg 4](http://ubuntuhandbook.org/index.php/2019/08/install-ffmpeg-4-2-ubuntu-18-04/)
|
||||
|
||||
## Install
|
||||
|
||||
@ -18,4 +21,18 @@ python -m pip install -r requirements.txt
|
||||
|
||||
```shell
|
||||
python tetrarcade.py
|
||||
```
|
||||
```
|
||||
|
||||
## Settings
|
||||
|
||||
* Windows: Edit `%appdata%\Tetrarcade\TetrArcade.ini`
|
||||
* Linux: Edit `~/.local/share/Tetrarcade/TetrArcade.ini`
|
||||
|
||||
Use key name from [arcade.key package](http://arcade.academy/arcade.key.html).
|
||||
|
||||
## Build
|
||||
|
||||
```shell
|
||||
python -m pip install -r build-requirements.txt
|
||||
python setup.py bdist
|
||||
```
|
||||
|
964
TetrArcade.py
2
build-requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
arcade
|
||||
cx-freeze
|
BIN
icon48.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 151 B |
Before Width: | Height: | Size: 167 B |
Before Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 143 B |
Before Width: | Height: | Size: 153 KiB After Width: | Height: | Size: 153 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
BIN
resources/images/minoes.png
Normal file
After Width: | Height: | Size: 389 B |
BIN
resources/musics/1-Song A.ogg
Normal file
BIN
resources/musics/2-!!!.ogg
Normal file
BIN
resources/musics/3-Boogie!.ogg
Normal file
BIN
resources/musics/4-Riff Blues.ogg
Normal file
37
setup.py
Normal file
@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
from cx_Freeze import setup, Executable
|
||||
|
||||
if sys.platform == "win32":
|
||||
base = "Win32GUI"
|
||||
icon = "icon.ico"
|
||||
else:
|
||||
base = None
|
||||
icon = None
|
||||
|
||||
excludes = ["tkinter", "PyQt4", "PyQt5", "PySide", "PySide2"]
|
||||
|
||||
executable = Executable(
|
||||
script="TetrArcade.py",
|
||||
icon=icon,
|
||||
base=base,
|
||||
shortcutName="TetrArcade",
|
||||
shortcutDir="DesktopFolder",
|
||||
)
|
||||
|
||||
options = {
|
||||
"build_exe": {
|
||||
"packages": ["arcade", "pyglet"],
|
||||
"excludes": excludes,
|
||||
"include_files": "resources",
|
||||
"silent": True,
|
||||
}
|
||||
}
|
||||
setup(
|
||||
name="TetrArcade",
|
||||
version="0.6",
|
||||
description="Tetris clone",
|
||||
author="AdrienMalin",
|
||||
executables=[executable],
|
||||
options=options,
|
||||
)
|
34
test.py
Normal file
@ -0,0 +1,34 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from TetrArcade import TetrArcade, MinoSprite, State
|
||||
from tetrislogic import Mino, Color, Coord
|
||||
|
||||
game = TetrArcade()
|
||||
game.new_game()
|
||||
for x in range(game.matrix.collumns):
|
||||
mino = Mino(Color.ORANGE, Coord(x, 0))
|
||||
mino.sprite = MinoSprite(mino, game, 200)
|
||||
game.matrix[0][x] = mino
|
||||
game.matrix.sprites.append(mino.sprite)
|
||||
game.move_left()
|
||||
game.pause()
|
||||
game.resume()
|
||||
game.move_right()
|
||||
game.hold()
|
||||
game.update(0)
|
||||
game.on_draw()
|
||||
game.rotate_clockwise()
|
||||
game.hold()
|
||||
game.update(0)
|
||||
game.on_draw()
|
||||
game.rotate_counter()
|
||||
for i in range(22):
|
||||
game.soft_drop()
|
||||
game.on_draw()
|
||||
game.lock_phase()
|
||||
game.hold()
|
||||
game.update(0)
|
||||
game.on_draw()
|
||||
while game.state != State.OVER:
|
||||
game.hard_drop()
|
||||
game.on_draw()
|
@ -1,5 +1,15 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from .consts import NB_LINES, NB_COLS, NB_NEXT
|
||||
from .utils import Movement, Rotation
|
||||
from .tetromino import Mino, Tetromino
|
||||
from .tetrislogic import TetrisLogic, State, Matrix
|
||||
from .consts import LINES, COLLUMNS, NEXT_PIECES
|
||||
from .utils import Movement, Spin, Color, Coord
|
||||
from .tetromino import (
|
||||
Mino,
|
||||
Tetromino,
|
||||
I_Tetrimino,
|
||||
J_Tetrimino,
|
||||
L_Tetrimino,
|
||||
O_Tetrimino,
|
||||
S_Tetrimino,
|
||||
T_Tetrimino,
|
||||
Z_Tetrimino,
|
||||
)
|
||||
from .tetrislogic import TetrisLogic, Matrix, AbstractScheduler
|
||||
|
@ -1,24 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from .utils import Coord
|
||||
from .utils import Coord, T_Spin
|
||||
|
||||
|
||||
# Matrix
|
||||
NB_LINES = 20
|
||||
NB_COLS = 10
|
||||
NB_NEXT = 5
|
||||
LINES = 20
|
||||
COLLUMNS = 10
|
||||
NEXT_PIECES = 5
|
||||
|
||||
# Delays (seconds)
|
||||
LOCK_DELAY = 0.5
|
||||
FALL_DELAY = 1
|
||||
AUTOREPEAT_DELAY = 0.200 # Official : 0.300
|
||||
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010
|
||||
|
||||
AUTOREPEAT_DELAY = 0.300 # Official : 0.300 s
|
||||
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010 s
|
||||
|
||||
# Piece init coord
|
||||
CURRENT_COORD = Coord(4, NB_LINES)
|
||||
NEXT_COORDS = [
|
||||
Coord(NB_COLS+6, NB_LINES-4*n-3)
|
||||
for n in range(NB_NEXT)
|
||||
]
|
||||
HELD_COORD = Coord(-7, NB_LINES-3)
|
||||
HELD_I_COORD = Coord(-7, NB_LINES-3)
|
||||
FALLING_PIECE_COORD = Coord(4, LINES)
|
||||
|
||||
# Scores
|
||||
LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
|
||||
SCORES = (
|
||||
{LINES_CLEAR_NAME: "", T_Spin.NONE: 0, T_Spin.MINI: 1, T_Spin.T_SPIN: 4},
|
||||
{LINES_CLEAR_NAME: "SINGLE", T_Spin.NONE: 1, T_Spin.MINI: 2, T_Spin.T_SPIN: 8},
|
||||
{LINES_CLEAR_NAME: "DOUBLE", T_Spin.NONE: 3, T_Spin.T_SPIN: 12},
|
||||
{LINES_CLEAR_NAME: "TRIPLE", T_Spin.NONE: 5, T_Spin.T_SPIN: 16},
|
||||
{LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8},
|
||||
)
|
||||
|
@ -1,116 +1,393 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from .utils import Coord, Movement, Rotation, T_Spin, Line
|
||||
from .tetromino import Tetromino, T
|
||||
"""Tetris game logic meant to be implemented with GUI
|
||||
Follows Tetris Guidelines 2009 (see https://tetris.fandom.com/wiki/Tetris_Guideline)
|
||||
"""
|
||||
|
||||
|
||||
import pickle
|
||||
|
||||
from .utils import Coord, Movement, Spin, T_Spin, T_Slot
|
||||
from .tetromino import Tetromino, T_Tetrimino
|
||||
from .consts import (
|
||||
NB_LINES, NB_COLS, NB_NEXT,
|
||||
LOCK_DELAY, FALL_DELAY,
|
||||
AUTOREPEAT_DELAY, AUTOREPEAT_PERIOD,
|
||||
CURRENT_COORD, NEXT_COORDS, HELD_COORD, HELD_I_COORD
|
||||
LINES,
|
||||
COLLUMNS,
|
||||
NEXT_PIECES,
|
||||
LOCK_DELAY,
|
||||
FALL_DELAY,
|
||||
AUTOREPEAT_DELAY,
|
||||
AUTOREPEAT_PERIOD,
|
||||
FALLING_PIECE_COORD,
|
||||
SCORES,
|
||||
LINES_CLEAR_NAME,
|
||||
)
|
||||
|
||||
|
||||
LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
|
||||
CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
|
||||
|
||||
|
||||
class State:
|
||||
class AbstractScheduler:
|
||||
"""Scheduler class to be implemented"""
|
||||
|
||||
STARTING = "STARTING"
|
||||
PLAYING = "PLAYING"
|
||||
PAUSED = "PAUSED"
|
||||
OVER = "OVER"
|
||||
def postpone(task, delay):
|
||||
"""schedule callable task once after delay in second"""
|
||||
raise Warning("AbstractScheduler.postpone is not implemented.")
|
||||
|
||||
def cancel(self, task):
|
||||
"""cancel task if schedule of pass"""
|
||||
raise Warning("AbstractScheduler.stop is not implemented.")
|
||||
|
||||
def reset(self, task, delay):
|
||||
"""cancel and reschedule task"""
|
||||
self.timer.cancel(task)
|
||||
self.timer.postpone(task, delay)
|
||||
|
||||
|
||||
class Matrix(list):
|
||||
class AbstractPieceContainer:
|
||||
def __init__(self):
|
||||
self.piece = None
|
||||
|
||||
|
||||
class HoldQueue(AbstractPieceContainer):
|
||||
"""the storage place where players can Hold any falling Tetrimino for use later"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class Matrix(list, AbstractPieceContainer):
|
||||
"""the rectangular arrangement of cells creating the active game area, usually 10 columns wide by 20 rows high."""
|
||||
|
||||
def __init__(self, lines, collumns):
|
||||
list.__init__(self)
|
||||
AbstractPieceContainer.__init__(self)
|
||||
self.lines = lines
|
||||
self.collumns = collumns
|
||||
self.ghost = None
|
||||
|
||||
def new_game(self):
|
||||
"""Removes all minoes in matrix"""
|
||||
self.clear()
|
||||
for y in range(self.lines + 3):
|
||||
self.append_new_line()
|
||||
|
||||
def append_new_line(self):
|
||||
self.append([None for x in range(self.collumns)])
|
||||
|
||||
def cell_is_free(self, coord):
|
||||
return (
|
||||
0 <= coord.x < NB_COLS
|
||||
and 0 <= coord.y
|
||||
and not self[coord.y][coord.x]
|
||||
0 <= coord.x < self.collumns and 0 <= coord.y and not self[coord.y][coord.x]
|
||||
)
|
||||
|
||||
def space_to_move(self, potential_coord, minoes_coord):
|
||||
return all(
|
||||
self.cell_is_free(potential_coord + mino_coord)
|
||||
for mino_coord in minoes_coord
|
||||
)
|
||||
|
||||
def space_to_fall(self):
|
||||
return self.space_to_move(
|
||||
self.piece.coord + Movement.DOWN, (mino.coord for mino in self.piece)
|
||||
)
|
||||
|
||||
|
||||
class TetrisLogic():
|
||||
class NextQueue(AbstractPieceContainer):
|
||||
"""Displays the Next Tetrimino(s) to be placed (generated) just above the Matrix"""
|
||||
|
||||
def __init__(self):
|
||||
self.load_high_score()
|
||||
self.state = State.STARTING
|
||||
self.matrix = Matrix()
|
||||
self.next = []
|
||||
self.current = None
|
||||
self.ghost = None
|
||||
self.held = None
|
||||
self.time = 0
|
||||
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
||||
self.pressed_actions = []
|
||||
self._score = 0
|
||||
def __init__(self, nb_pieces):
|
||||
super().__init__()
|
||||
self.nb_pieces = nb_pieces
|
||||
self.pieces = []
|
||||
|
||||
def get_score(self):
|
||||
def new_game(self):
|
||||
self.pieces = [Tetromino() for n in range(self.nb_pieces)]
|
||||
|
||||
def generation_phase(self):
|
||||
self.pieces.append(Tetromino())
|
||||
return self.pieces.pop(0)
|
||||
|
||||
|
||||
class Stats:
|
||||
"""Game statistics"""
|
||||
|
||||
def _get_score(self):
|
||||
return self._score
|
||||
|
||||
def set_score(self, new_score):
|
||||
def _set_score(self, new_score):
|
||||
self._score = new_score
|
||||
if self._score > self.high_score:
|
||||
self.high_score = self._score
|
||||
|
||||
score = property(get_score, set_score)
|
||||
score = property(_get_score, _set_score)
|
||||
|
||||
def new_game(self):
|
||||
self.level = 0
|
||||
self.score = 0
|
||||
self.nb_lines_cleared = 0
|
||||
self.goal = 0
|
||||
def __init__(self):
|
||||
self._score = 0
|
||||
self.high_score = 0
|
||||
self.time = 0
|
||||
|
||||
self.pressed_actions = []
|
||||
self.auto_repeat = False
|
||||
def new_game(self, level):
|
||||
self.level = level - 1
|
||||
self.score = 0
|
||||
self.lines_cleared = 0
|
||||
self.goal = 0
|
||||
self.time = 0
|
||||
self.combo = -1
|
||||
|
||||
self.lock_delay = LOCK_DELAY
|
||||
self.fall_delay = FALL_DELAY
|
||||
|
||||
self.matrix.clear()
|
||||
for y in range(NB_LINES+3):
|
||||
self.append_new_line_to_matrix()
|
||||
self.next = [self.new_tetromino() for n in range(NB_NEXT)]
|
||||
self.held = None
|
||||
self.state = State.PLAYING
|
||||
self.start(self.update_time, 1)
|
||||
|
||||
self.new_level()
|
||||
|
||||
def new_tetromino(self):
|
||||
return Tetromino()
|
||||
|
||||
def append_new_line_to_matrix(self):
|
||||
self.matrix.append(Line(None for x in range(NB_COLS)))
|
||||
|
||||
def new_level(self):
|
||||
self.level += 1
|
||||
self.goal += 5 * self.level
|
||||
if self.level <= 20:
|
||||
self.fall_delay = pow(0.8 - ((self.level-1)*0.007), self.level-1)
|
||||
self.fall_delay = pow(0.8 - ((self.level - 1) * 0.007), self.level - 1)
|
||||
if self.level > 15:
|
||||
self.lock_delay = 0.5 * pow(0.9, self.level-15)
|
||||
self.show_text("LEVEL\n{:n}".format(self.level))
|
||||
self.restart(self.fall, self.fall_delay)
|
||||
self.lock_delay = 0.5 * pow(0.9, self.level - 15)
|
||||
|
||||
self.new_current()
|
||||
def update_time(self):
|
||||
self.time += 1
|
||||
|
||||
def new_current(self):
|
||||
self.current = self.next.pop(0)
|
||||
self.current.coord = CURRENT_COORD
|
||||
self.ghost = self.current.ghost()
|
||||
self.move_ghost()
|
||||
self.next.append(self.new_tetromino())
|
||||
self.next[-1].coord = NEXT_COORDS[-1]
|
||||
for tetromino, coord in zip (self.next, NEXT_COORDS):
|
||||
tetromino.coord = coord
|
||||
def locks_down(self, t_spin, lines_cleared):
|
||||
pattern_name = []
|
||||
pattern_score = 0
|
||||
combo_score = 0
|
||||
|
||||
if not self.can_move(
|
||||
self.current.coord,
|
||||
(mino.coord for mino in self.current)
|
||||
if t_spin:
|
||||
pattern_name.append(t_spin)
|
||||
if lines_cleared:
|
||||
pattern_name.append(SCORES[lines_cleared][LINES_CLEAR_NAME])
|
||||
self.combo += 1
|
||||
else:
|
||||
self.combo = -1
|
||||
|
||||
if lines_cleared or t_spin:
|
||||
pattern_score = SCORES[lines_cleared][t_spin]
|
||||
self.goal -= pattern_score
|
||||
pattern_score *= 100 * self.level
|
||||
pattern_name = "\n".join(pattern_name)
|
||||
|
||||
if self.combo >= 1:
|
||||
combo_score = (20 if lines_cleared == 1 else 50) * self.combo * self.level
|
||||
|
||||
self.score += pattern_score + combo_score
|
||||
|
||||
return pattern_name, pattern_score, self.combo, combo_score
|
||||
|
||||
|
||||
class TetrisLogic:
|
||||
"""Tetris game logic"""
|
||||
|
||||
# These class attributes can be redefined on inheritance
|
||||
AUTOREPEAT_DELAY = AUTOREPEAT_DELAY
|
||||
AUTOREPEAT_PERIOD = AUTOREPEAT_PERIOD
|
||||
FALLING_PIECE_COORD = FALLING_PIECE_COORD
|
||||
|
||||
timer = AbstractScheduler()
|
||||
|
||||
def __init__(self, lines=LINES, collumns=COLLUMNS, nb_next_pieces=NEXT_PIECES):
|
||||
"""init game with a `lines`x`collumns` size matrix
|
||||
and `nb_next_pieces`"""
|
||||
self.stats = Stats()
|
||||
self.load_high_score()
|
||||
self.held = HoldQueue()
|
||||
self.matrix = Matrix(lines, collumns)
|
||||
self.next = NextQueue(nb_next_pieces)
|
||||
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
||||
self.pressed_actions = []
|
||||
|
||||
def new_game(self, level=1):
|
||||
"""start a new game at `level`"""
|
||||
self.stats.new_game(level)
|
||||
|
||||
self.pressed_actions = []
|
||||
|
||||
self.matrix.new_game()
|
||||
self.next.new_game()
|
||||
self.held.piece = None
|
||||
self.timer.postpone(self.stats.update_time, 1)
|
||||
|
||||
self.on_new_game(self.matrix, self.next.pieces)
|
||||
self.new_level()
|
||||
|
||||
def on_new_game(self, matrix, next_pieces):
|
||||
pass
|
||||
|
||||
def new_level(self):
|
||||
self.stats.new_level()
|
||||
self.on_new_level(self.stats.level)
|
||||
self.generation_phase()
|
||||
|
||||
def on_new_level(self, level):
|
||||
pass
|
||||
|
||||
# Tetris Engine
|
||||
|
||||
def generation_phase(self, held_piece=None):
|
||||
if not held_piece:
|
||||
self.matrix.piece = self.next.generation_phase()
|
||||
self.matrix.piece.coord = self.FALLING_PIECE_COORD
|
||||
self.matrix.ghost = self.matrix.piece.ghost()
|
||||
self.refresh_ghost()
|
||||
|
||||
self.on_generation_phase(
|
||||
self.matrix, self.matrix.piece, self.matrix.ghost, self.next.pieces
|
||||
)
|
||||
if self.matrix.space_to_move(
|
||||
self.matrix.piece.coord, (mino.coord for mino in self.matrix.piece)
|
||||
):
|
||||
self.falling_phase()
|
||||
else:
|
||||
self.game_over()
|
||||
|
||||
def refresh_ghost(self):
|
||||
self.matrix.ghost.coord = self.matrix.piece.coord
|
||||
for ghost_mino, current_mino in zip(self.matrix.ghost, self.matrix.piece):
|
||||
ghost_mino.coord = current_mino.coord
|
||||
while self.matrix.space_to_move(
|
||||
self.matrix.ghost.coord + Movement.DOWN,
|
||||
(mino.coord for mino in self.matrix.ghost),
|
||||
):
|
||||
self.matrix.ghost.coord += Movement.DOWN
|
||||
|
||||
def on_generation_phase(self, matrix, falling_piece, ghost_piece, next_pieces):
|
||||
pass
|
||||
|
||||
def falling_phase(self):
|
||||
self.timer.cancel(self.lock_phase)
|
||||
self.timer.cancel(self.locks_down)
|
||||
self.matrix.piece.locked = False
|
||||
self.timer.postpone(self.lock_phase, self.stats.fall_delay)
|
||||
self.on_falling_phase(self.matrix.piece, self.matrix.ghost)
|
||||
|
||||
def on_falling_phase(self, falling_piece, ghost_piece):
|
||||
pass
|
||||
|
||||
def lock_phase(self):
|
||||
self.move(Movement.DOWN)
|
||||
|
||||
def move(self, movement, rotated_coords=None, lock=True):
|
||||
potential_coord = self.matrix.piece.coord + movement
|
||||
potential_minoes_coords = rotated_coords or (
|
||||
mino.coord for mino in self.matrix.piece
|
||||
)
|
||||
if self.matrix.space_to_move(potential_coord, potential_minoes_coords):
|
||||
self.matrix.piece.coord = potential_coord
|
||||
if rotated_coords:
|
||||
for mino, coord in zip(self.matrix.piece, rotated_coords):
|
||||
mino.coord = coord
|
||||
self.refresh_ghost()
|
||||
if movement != Movement.DOWN:
|
||||
self.matrix.piece.rotated_last = False
|
||||
if self.matrix.space_to_fall():
|
||||
self.falling_phase()
|
||||
else:
|
||||
self.matrix.piece.locked = True
|
||||
self.on_locked(self.matrix.piece, self.matrix.ghost)
|
||||
self.timer.reset(self.locks_down, self.stats.lock_delay)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def on_locked(self, falling_piece, ghost_piece):
|
||||
pass
|
||||
|
||||
def rotate(self, spin):
|
||||
rotated_coords = tuple(mino.coord @ spin for mino in self.matrix.piece)
|
||||
for rotation_point, liberty_degree in enumerate(
|
||||
self.matrix.piece.SRS[spin][self.matrix.piece.orientation], start=1
|
||||
):
|
||||
if self.move(liberty_degree, rotated_coords, lock=False):
|
||||
self.matrix.piece.orientation = (
|
||||
self.matrix.piece.orientation + spin
|
||||
) % 4
|
||||
self.matrix.piece.rotated_last = True
|
||||
if rotation_point == 5:
|
||||
self.matrix.piece.rotation_point_5_used = True
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def locks_down(self):
|
||||
self.timer.cancel(self.lock_phase)
|
||||
|
||||
# Game over
|
||||
if all(
|
||||
(mino.coord + self.matrix.piece.coord).y >= self.matrix.lines
|
||||
for mino in self.matrix.piece
|
||||
):
|
||||
self.game_over()
|
||||
return
|
||||
|
||||
for mino in self.matrix.piece:
|
||||
coord = mino.coord + self.matrix.piece.coord
|
||||
if coord.y <= self.matrix.lines + 3:
|
||||
self.matrix[coord.y][coord.x] = mino
|
||||
|
||||
self.on_locks_down(self.matrix, self.matrix.piece)
|
||||
|
||||
# Pattern phase
|
||||
|
||||
# T-Spin
|
||||
if type(self.matrix.piece) == T_Tetrimino and self.matrix.piece.rotated_last:
|
||||
a = self.is_t_slot(T_Slot.A)
|
||||
b = self.is_t_slot(T_Slot.B)
|
||||
c = self.is_t_slot(T_Slot.C)
|
||||
d = self.is_t_slot(T_Slot.D)
|
||||
if a and b and (c or d):
|
||||
t_spin = T_Spin.T_SPIN
|
||||
elif c and d and (a or b):
|
||||
if self.matrix.piece.rotation_point_5_used:
|
||||
t_spin = T_Spin.T_SPIN
|
||||
else:
|
||||
t_spin = T_Spin.MINI
|
||||
else:
|
||||
t_spin = T_Spin.NONE
|
||||
else:
|
||||
t_spin = T_Spin.NONE
|
||||
|
||||
# Complete lines
|
||||
lines_to_remove = []
|
||||
for y, line in reversed(list(enumerate(self.matrix))):
|
||||
if all(mino for mino in line):
|
||||
lines_to_remove.append(y)
|
||||
|
||||
lines_cleared = len(lines_to_remove)
|
||||
if lines_cleared:
|
||||
self.stats.lines_cleared += lines_cleared
|
||||
|
||||
# Animate phase
|
||||
|
||||
self.on_animate_phase(self.matrix, lines_to_remove)
|
||||
|
||||
# Eliminate phase
|
||||
self.on_eliminate_phase(self.matrix, lines_to_remove)
|
||||
for y in lines_to_remove:
|
||||
self.matrix.pop(y)
|
||||
self.matrix.append_new_line()
|
||||
|
||||
# Completion phase
|
||||
|
||||
pattern_name, pattern_score, nb_combo, combo_score = self.stats.locks_down(
|
||||
t_spin, lines_cleared
|
||||
)
|
||||
self.on_completion_phase(pattern_name, pattern_score, nb_combo, combo_score)
|
||||
|
||||
if self.stats.goal <= 0:
|
||||
self.new_level()
|
||||
else:
|
||||
self.generation_phase()
|
||||
|
||||
def on_locks_down(self, matrix, falling_piece):
|
||||
pass
|
||||
|
||||
def on_animate_phase(self, matrix, lines_to_remove):
|
||||
pass
|
||||
|
||||
def on_eliminate_phase(self, matrix, lines_to_remove):
|
||||
pass
|
||||
|
||||
def on_completion_phase(self, pattern_name, pattern_score, nb_combo, combo_score):
|
||||
pass
|
||||
|
||||
# Actions
|
||||
|
||||
def move_left(self):
|
||||
self.move(Movement.LEFT)
|
||||
@ -119,263 +396,98 @@ class TetrisLogic():
|
||||
self.move(Movement.RIGHT)
|
||||
|
||||
def rotate_clockwise(self):
|
||||
self.rotate(Rotation.CLOCKWISE)
|
||||
self.rotate(Spin.CLOCKWISE)
|
||||
|
||||
def rotate_counter(self):
|
||||
self.rotate(Rotation.COUNTER)
|
||||
|
||||
def move_ghost(self):
|
||||
self.ghost.coord = self.current.coord
|
||||
for ghost_mino, current_mino in zip(self.ghost, self.current):
|
||||
ghost_mino.coord = current_mino.coord
|
||||
while self.can_move(
|
||||
self.ghost.coord + Movement.DOWN,
|
||||
(mino.coord for mino in self.ghost)
|
||||
):
|
||||
self.ghost.coord += Movement.DOWN
|
||||
self.rotate(Spin.COUNTER)
|
||||
|
||||
def soft_drop(self):
|
||||
moved = self.move(Movement.DOWN)
|
||||
if moved:
|
||||
self.score += 1
|
||||
self.stats.score += 1
|
||||
return moved
|
||||
|
||||
def hard_drop(self):
|
||||
while self.move(Movement.DOWN, prelock=False):
|
||||
self.score += 2
|
||||
self.lock()
|
||||
self.timer.cancel(self.lock_phase)
|
||||
self.timer.cancel(self.locks_down)
|
||||
while self.move(Movement.DOWN, lock=False):
|
||||
self.stats.score += 2
|
||||
self.locks_down()
|
||||
|
||||
def fall(self):
|
||||
self.move(Movement.DOWN)
|
||||
|
||||
def move(self, movement, prelock=True):
|
||||
potential_coord = self.current.coord + movement
|
||||
if self.can_move(
|
||||
potential_coord,
|
||||
(mino.coord for mino in self.current)
|
||||
):
|
||||
if self.current.prelocked:
|
||||
self.restart(self.lock, self.lock_delay)
|
||||
self.current.coord = potential_coord
|
||||
if not movement == Movement.DOWN:
|
||||
self.current.last_rotation_point = None
|
||||
self.move_ghost()
|
||||
return True
|
||||
else:
|
||||
if (
|
||||
prelock and not self.current.prelocked
|
||||
and movement == Movement.DOWN
|
||||
):
|
||||
self.current.prelocked = True
|
||||
self.start(self.lock, self.lock_delay)
|
||||
return False
|
||||
|
||||
def rotate(self, rotation):
|
||||
rotated_coords = tuple(
|
||||
Coord(rotation*mino.coord.y, -rotation*mino.coord.x)
|
||||
for mino in self.current
|
||||
)
|
||||
for rotation_point, liberty_degree in enumerate(
|
||||
self.current.SRS[rotation][self.current.orientation],
|
||||
start = 1
|
||||
):
|
||||
potential_coord = self.current.coord + liberty_degree
|
||||
if self.can_move(potential_coord, rotated_coords):
|
||||
if self.current.prelocked:
|
||||
self.restart(self.lock, self.lock_delay)
|
||||
self.current.coord = potential_coord
|
||||
for mino, coord in zip(self.current, rotated_coords):
|
||||
mino.coord = coord
|
||||
self.current.orientation = (
|
||||
(self.current.orientation + rotation) % 4
|
||||
)
|
||||
self.current.last_rotation_point = rotation_point
|
||||
self.move_ghost()
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
SCORES = (
|
||||
{LINES_CLEAR_NAME: "", T_Spin.NONE: 0, T_Spin.MINI: 1, T_Spin.T_SPIN: 4},
|
||||
{LINES_CLEAR_NAME: "SINGLE", T_Spin.NONE: 1, T_Spin.MINI: 2, T_Spin.T_SPIN: 8},
|
||||
{LINES_CLEAR_NAME: "DOUBLE", T_Spin.NONE: 3, T_Spin.T_SPIN: 12},
|
||||
{LINES_CLEAR_NAME: "TRIPLE", T_Spin.NONE: 5, T_Spin.T_SPIN: 16},
|
||||
{LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8}
|
||||
)
|
||||
|
||||
def lock(self):
|
||||
# Piece unlocked
|
||||
if self.can_move(
|
||||
self.current.coord + Movement.DOWN,
|
||||
(mino.coord for mino in self.current)
|
||||
):
|
||||
def hold(self):
|
||||
if not self.matrix.piece.hold_enabled:
|
||||
return
|
||||
|
||||
# Start lock
|
||||
self.current.prelocked = False
|
||||
self.stop(self.lock)
|
||||
if self.pressed_actions:
|
||||
self.auto_repeat = False
|
||||
self.restart(self.repeat_action, AUTOREPEAT_DELAY)
|
||||
self.matrix.piece.hold_enabled = False
|
||||
self.timer.cancel(self.lock_phase)
|
||||
self.matrix.piece, self.held.piece = self.held.piece, self.matrix.piece
|
||||
|
||||
# Game over
|
||||
if all(
|
||||
(mino.coord + self.current.coord).y >= NB_LINES
|
||||
for mino in self.current
|
||||
):
|
||||
self.game_over()
|
||||
return
|
||||
for mino, coord in zip(self.held.piece, self.held.piece.MINOES_COORDS):
|
||||
mino.coord = coord
|
||||
|
||||
# T-Spin
|
||||
if (
|
||||
type(self.current) == T
|
||||
and self.current.last_rotation_point is not None
|
||||
):
|
||||
a = self.is_t_slot(0)
|
||||
b = self.is_t_slot(1)
|
||||
c = self.is_t_slot(3)
|
||||
d = self.is_t_slot(2)
|
||||
if self.current.last_rotation_point == 5 or (
|
||||
a and b and (c or d)
|
||||
):
|
||||
t_spin = T_Spin.T_SPIN
|
||||
elif c and d and (a or b):
|
||||
t_spin = T_Spin.MINI
|
||||
else:
|
||||
t_spin = T_Spin.NONE
|
||||
else:
|
||||
t_spin = T_Spin.NONE
|
||||
self.on_hold(self.held.piece)
|
||||
self.generation_phase(self.matrix.piece)
|
||||
|
||||
for mino in self.current:
|
||||
coord = mino.coord + self.current.coord
|
||||
del(mino.coord)
|
||||
if coord.y <= NB_LINES+3:
|
||||
self.matrix[coord.y][coord.x] = mino
|
||||
def on_hold(self, held_piece):
|
||||
pass
|
||||
|
||||
# Clear complete lines
|
||||
nb_lines_cleared = 0
|
||||
for y, line in reversed(list(enumerate(self.matrix))):
|
||||
if all(mino for mino in line):
|
||||
nb_lines_cleared += 1
|
||||
self.matrix.pop(y)
|
||||
self.append_new_line_to_matrix()
|
||||
if nb_lines_cleared:
|
||||
self.nb_lines_cleared += nb_lines_cleared
|
||||
|
||||
# Scoring
|
||||
lock_strings = []
|
||||
lock_score = 0
|
||||
|
||||
if t_spin:
|
||||
lock_strings.append(t_spin)
|
||||
if nb_lines_cleared:
|
||||
lock_strings.append(self.SCORES[nb_lines_cleared][LINES_CLEAR_NAME])
|
||||
self.combo += 1
|
||||
else:
|
||||
self.combo = -1
|
||||
|
||||
if nb_lines_cleared or t_spin:
|
||||
ds = self.SCORES[nb_lines_cleared][t_spin]
|
||||
self.goal -= ds
|
||||
ds *= 100 * self.level
|
||||
lock_score += ds
|
||||
lock_strings.append(str(ds))
|
||||
self.show_text("\n".join(lock_strings))
|
||||
|
||||
if self.combo >= 1:
|
||||
ds = (20 if nb_lines_cleared==1 else 50) * self.combo * self.level
|
||||
lock_score += ds
|
||||
self.show_text("COMBO x{:n}\n{:n}".format(self.combo, ds))
|
||||
|
||||
self.score += lock_score
|
||||
|
||||
if self.goal <= 0:
|
||||
self.new_level()
|
||||
else:
|
||||
self.new_current()
|
||||
|
||||
def can_move(self, potential_coord, minoes_coords):
|
||||
return all(
|
||||
self.matrix.cell_is_free(potential_coord+mino_coord)
|
||||
for mino_coord in minoes_coords
|
||||
)
|
||||
|
||||
T_SLOT_COORDS = (
|
||||
Coord(-1, 1),
|
||||
Coord( 1, 1),
|
||||
Coord(-1, 1),
|
||||
Coord(-1, -1)
|
||||
)
|
||||
T_SLOT_COORDS = (Coord(-1, 1), Coord(1, 1), Coord(-1, 1), Coord(-1, -1))
|
||||
|
||||
def is_t_slot(self, n):
|
||||
t_slot_coord = self.current.coord + self.T_SLOT_COORDS[
|
||||
(self.current.orientation + n) % 4
|
||||
]
|
||||
t_slot_coord = (
|
||||
self.matrix.piece.coord
|
||||
+ self.T_SLOT_COORDS[(self.matrix.piece.orientation + n) % 4]
|
||||
)
|
||||
return not self.matrix.cell_is_free(t_slot_coord)
|
||||
|
||||
def swap(self):
|
||||
if self.current.hold_enabled:
|
||||
self.current.hold_enabled = False
|
||||
self.current.prelocked = False
|
||||
self.stop(self.lock)
|
||||
self.current, self.held = self.held, self.current
|
||||
if self.held.__class__ == Tetromino.I:
|
||||
self.held.coord = HELD_I_COORD
|
||||
else:
|
||||
self.held.coord = HELD_COORD
|
||||
for mino, coord in zip(self.held, self.held.MINOES_COORDS):
|
||||
mino.coord = coord
|
||||
|
||||
if self.current:
|
||||
self.current.coord = CURRENT_COORD
|
||||
self.ghost = self.current.ghost()
|
||||
self.move_ghost()
|
||||
else:
|
||||
self.new_current()
|
||||
|
||||
def pause(self):
|
||||
self.state = State.PAUSED
|
||||
self.stop_all()
|
||||
self.pressed_actions = []
|
||||
self.auto_repeat = False
|
||||
self.stop(self.repeat_action)
|
||||
self.timer.cancel(self.repeat_action)
|
||||
self.on_pause()
|
||||
|
||||
def on_pause(self):
|
||||
pass
|
||||
|
||||
def resume(self):
|
||||
self.state = State.PLAYING
|
||||
self.start(self.fall, self.fall_delay)
|
||||
if self.current.prelocked:
|
||||
self.start(self.lock, self.lock_delay)
|
||||
self.start(self.update_time, 1)
|
||||
self.timer.postpone(self.lock_phase, self.stats.fall_delay)
|
||||
if self.matrix.piece.locked:
|
||||
self.timer.postpone(self.locks_down, self.stats.lock_delay)
|
||||
self.timer.postpone(self.stats.update_time, 1)
|
||||
self.on_resume()
|
||||
|
||||
def on_resume(self):
|
||||
pass
|
||||
|
||||
def game_over(self):
|
||||
self.state = State.OVER
|
||||
self.stop_all()
|
||||
self.save_high_score()
|
||||
self.on_game_over()
|
||||
|
||||
def on_game_over(self):
|
||||
pass
|
||||
|
||||
def stop_all(self):
|
||||
self.stop(self.fall)
|
||||
self.stop(self.lock)
|
||||
self.stop(self.update_time)
|
||||
|
||||
def update_time(self):
|
||||
self.time += 1
|
||||
self.timer.cancel(self.lock_phase)
|
||||
self.timer.cancel(self.locks_down)
|
||||
self.timer.cancel(self.stats.update_time)
|
||||
|
||||
def do_action(self, action):
|
||||
action()
|
||||
if action in self.autorepeatable_actions:
|
||||
self.auto_repeat = False
|
||||
self.pressed_actions.append(action)
|
||||
self.restart(self.repeat_action, AUTOREPEAT_DELAY)
|
||||
if action == self.soft_drop:
|
||||
delay = self.stats.fall_delay / 20
|
||||
else:
|
||||
delay = self.AUTOREPEAT_DELAY
|
||||
self.timer.reset(self.repeat_action, delay)
|
||||
|
||||
def repeat_action(self):
|
||||
if self.pressed_actions:
|
||||
self.pressed_actions[-1]()
|
||||
if not self.auto_repeat:
|
||||
self.auto_repeat = True
|
||||
self.restart(self.repeat_action, AUTOREPEAT_PERIOD)
|
||||
else:
|
||||
self.auto_repeat = False
|
||||
self.stop(self.repeat_action)
|
||||
if not self.pressed_actions:
|
||||
return
|
||||
|
||||
self.pressed_actions[-1]()
|
||||
self.timer.postpone(self.repeat_action, self.AUTOREPEAT_PERIOD)
|
||||
|
||||
def remove_action(self, action):
|
||||
if action in self.autorepeatable_actions:
|
||||
@ -388,27 +500,18 @@ class TetrisLogic():
|
||||
print(text)
|
||||
raise Warning("TetrisLogic.show_text not implemented.")
|
||||
|
||||
def load_high_score(self):
|
||||
self.high_score = 0
|
||||
raise Warning(
|
||||
"""TetrisLogic.load_high_score not implemented.
|
||||
def load_high_score(self, crypted_high_score=None):
|
||||
if crypted_high_score:
|
||||
crypted_high_score = int(pickle.loads(crypted_high_score))
|
||||
self.stats.high_score = crypted_high_score ^ CRYPT_KEY
|
||||
else:
|
||||
raise Warning(
|
||||
"""TetrisLogic.load_high_score not implemented.
|
||||
High score is set to 0"""
|
||||
)
|
||||
)
|
||||
self.stats.high_score = 0
|
||||
|
||||
def save_high_score(self):
|
||||
print("High score: {:n}".format(self.high_score))
|
||||
raise Warning(
|
||||
"""TetrisLogic.save_high_score not implemented.
|
||||
High score is not saved"""
|
||||
)
|
||||
|
||||
def start(task, period):
|
||||
raise Warning("TetrisLogic.start is not implemented.")
|
||||
|
||||
def stop(self, task):
|
||||
raise Warning("TetrisLogic.stop is not implemented.")
|
||||
|
||||
def restart(self, task, period):
|
||||
self.stop(task)
|
||||
self.start(task, period)
|
||||
|
||||
crypted_high_score = self.stats.high_score ^ CRYPT_KEY
|
||||
crypted_high_score = pickle.dumps(crypted_high_score)
|
||||
return crypted_high_score
|
||||
|
@ -1,17 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import random
|
||||
|
||||
from .utils import Coord, Rotation
|
||||
from .utils import Coord, Spin, Color
|
||||
|
||||
|
||||
class Mino:
|
||||
|
||||
def __init__(self, color, coord):
|
||||
self.color = color
|
||||
self.coord = coord
|
||||
|
||||
|
||||
class MetaTetromino(type):
|
||||
|
||||
def __init__(cls, name, bases, dct):
|
||||
super().__init__(name, bases, dct)
|
||||
Tetromino.shapes.append(cls)
|
||||
@ -29,17 +28,17 @@ class Tetromino:
|
||||
return cls.random_bag.pop()()
|
||||
|
||||
|
||||
class AbstractTetromino(list):
|
||||
class TetrominoBase(list):
|
||||
|
||||
# Super rotation system
|
||||
SRS = {
|
||||
Rotation.CLOCKWISE: (
|
||||
Spin.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: (
|
||||
Spin.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)),
|
||||
@ -48,41 +47,39 @@ class AbstractTetromino(list):
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
Mino(self.MINOES_COLOR, coord)
|
||||
for coord in self.MINOES_COORDS
|
||||
)
|
||||
super().__init__(Mino(self.MINOES_COLOR, coord) for coord in self.MINOES_COORDS)
|
||||
self.orientation = 0
|
||||
self.last_rotation_point = None
|
||||
self.rotated_last = False
|
||||
self.rotation_point_5_used = False
|
||||
self.hold_enabled = True
|
||||
self.prelocked = False
|
||||
|
||||
def ghost(self):
|
||||
return self.__class__()
|
||||
return type(self)()
|
||||
|
||||
class O(AbstractTetromino, metaclass=MetaTetromino):
|
||||
|
||||
class O_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
SRS = {
|
||||
Rotation.CLOCKWISE: (tuple(), tuple(), tuple(), tuple()),
|
||||
Rotation.COUNTER: (tuple(), tuple(), tuple(), tuple()),
|
||||
Spin.CLOCKWISE: (tuple(), tuple(), tuple(), tuple()),
|
||||
Spin.COUNTER: (tuple(), tuple(), tuple(), tuple()),
|
||||
}
|
||||
MINOES_COORDS = (Coord(0, 0), Coord(1, 0), Coord(0, 1), Coord(1, 1))
|
||||
MINOES_COLOR = "yellow"
|
||||
MINOES_COLOR = Color.YELLOW
|
||||
|
||||
def rotate(self, direction):
|
||||
return False
|
||||
|
||||
|
||||
class I(AbstractTetromino, metaclass=MetaTetromino):
|
||||
class I_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
SRS = {
|
||||
Rotation.CLOCKWISE: (
|
||||
Spin.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: (
|
||||
Spin.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)),
|
||||
@ -90,34 +87,34 @@ class I(AbstractTetromino, metaclass=MetaTetromino):
|
||||
),
|
||||
}
|
||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(1, 0), Coord(2, 0))
|
||||
MINOES_COLOR = "cyan"
|
||||
MINOES_COLOR = Color.CYAN
|
||||
|
||||
|
||||
class T(AbstractTetromino, metaclass=MetaTetromino):
|
||||
class T_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(0, 1), Coord(1, 0))
|
||||
MINOES_COLOR = "magenta"
|
||||
MINOES_COLOR = Color.MAGENTA
|
||||
|
||||
|
||||
class L(AbstractTetromino, metaclass=MetaTetromino):
|
||||
class L_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(1, 0), Coord(1, 1))
|
||||
MINOES_COLOR = "orange"
|
||||
MINOES_COLOR = Color.ORANGE
|
||||
|
||||
|
||||
class J(AbstractTetromino, metaclass=MetaTetromino):
|
||||
class J_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
MINOES_COORDS = (Coord(-1, 1), Coord(-1, 0), Coord(0, 0), Coord(1, 0))
|
||||
MINOES_COLOR = "blue"
|
||||
MINOES_COLOR = Color.BLUE
|
||||
|
||||
|
||||
class S(AbstractTetromino, metaclass=MetaTetromino):
|
||||
class S_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
MINOES_COORDS = (Coord(-1, 0), Coord(0, 0), Coord(0, 1), Coord(1, 1))
|
||||
MINOES_COLOR = "green"
|
||||
MINOES_COLOR = Color.GREEN
|
||||
|
||||
|
||||
class Z(AbstractTetromino, metaclass=MetaTetromino):
|
||||
class Z_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||
|
||||
MINOES_COORDS = (Coord(-1, 1), Coord(0, 1), Coord(0, 0), Coord(1, 0))
|
||||
MINOES_COLOR = "red"
|
||||
MINOES_COLOR = Color.RED
|
||||
|
@ -1,34 +1,50 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
class Coord:
|
||||
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __add__(self, other):
|
||||
return Coord(self.x+other.x, self.y+other.y)
|
||||
return Coord(self.x + other.x, self.y + other.y)
|
||||
|
||||
def __matmul__(self, spin):
|
||||
return Coord(spin * self.y, -spin * self.x)
|
||||
|
||||
|
||||
class Movement:
|
||||
|
||||
LEFT = Coord(-1, 0)
|
||||
RIGHT = Coord( 1, 0)
|
||||
DOWN = Coord( 0, -1)
|
||||
LEFT = Coord(-1, 0)
|
||||
RIGHT = Coord(1, 0)
|
||||
DOWN = Coord(0, -1)
|
||||
|
||||
|
||||
class Rotation:
|
||||
class Spin:
|
||||
|
||||
CLOCKWISE = 1
|
||||
COUNTER = -1
|
||||
CLOCKWISE = 1
|
||||
COUNTER = -1
|
||||
|
||||
|
||||
class T_Spin:
|
||||
|
||||
NONE = ""
|
||||
NONE = ""
|
||||
MINI = "MINI\nT-SPIN"
|
||||
T_SPIN = "T-SPIN"
|
||||
T_SPIN = "T-SPIN"
|
||||
|
||||
|
||||
class Line(list):
|
||||
pass
|
||||
class T_Slot:
|
||||
|
||||
A = 0
|
||||
B = 1
|
||||
C = 3
|
||||
D = 2
|
||||
|
||||
|
||||
class Color:
|
||||
|
||||
BLUE = 0
|
||||
CYAN = 1
|
||||
GREEN = 2
|
||||
MAGENTA = 3
|
||||
ORANGE = 4
|
||||
RED = 5
|
||||
YELLOW = 6
|
||||
|