Compare commits
No commits in common. "master" and "0.4" have entirely different histories.
@ -6,8 +6,7 @@ Tetris clone made with Python and Arcade graphic library
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
* [Python 3.6 or later](https://www.python.org/)
|
* [Python](https://www.python.org/) 3.6 or upper
|
||||||
* [FFmpeg 4](http://ubuntuhandbook.org/index.php/2019/08/install-ffmpeg-4-2-ubuntu-18-04/)
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
297
TetrArcade.py
297
TetrArcade.py
@ -1,7 +1,4 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Tetris clone with arcade GUI library"""
|
|
||||||
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import random
|
import random
|
||||||
|
|
||||||
@ -23,14 +20,7 @@ import os
|
|||||||
import itertools
|
import itertools
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
from tetrislogic import (
|
from tetrislogic import TetrisLogic, Color, Phase, Coord, I_Tetrimino, Movement
|
||||||
TetrisLogic,
|
|
||||||
Color,
|
|
||||||
Coord,
|
|
||||||
I_Tetrimino,
|
|
||||||
Movement,
|
|
||||||
AbstractScheduler,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
@ -45,7 +35,6 @@ FALL_DELAY = 1
|
|||||||
AUTOREPEAT_DELAY = 0.300
|
AUTOREPEAT_DELAY = 0.300
|
||||||
AUTOREPEAT_PERIOD = 0.010
|
AUTOREPEAT_PERIOD = 0.010
|
||||||
PARTICULE_ACCELERATION = 1.1
|
PARTICULE_ACCELERATION = 1.1
|
||||||
EXPLOSION_ANIMATION = 1
|
|
||||||
|
|
||||||
# Piece init coord
|
# Piece init coord
|
||||||
MATRIX_PIECE_COORD = Coord(4, LINES)
|
MATRIX_PIECE_COORD = Coord(4, LINES)
|
||||||
@ -103,6 +92,8 @@ TEXTURES = arcade.load_textures(
|
|||||||
((i * MINO_SPRITE_SIZE, 0, MINO_SPRITE_SIZE, MINO_SPRITE_SIZE) for i in range(8)),
|
((i * MINO_SPRITE_SIZE, 0, MINO_SPRITE_SIZE, MINO_SPRITE_SIZE) for i in range(8)),
|
||||||
)
|
)
|
||||||
TEXTURES = {color: TEXTURES[i] for color, i in MINOES_COLOR_ID.items()}
|
TEXTURES = {color: TEXTURES[i] for color, i in MINOES_COLOR_ID.items()}
|
||||||
|
NORMAL_TEXTURE = 0
|
||||||
|
LOCKED_TEXTURE = 1
|
||||||
|
|
||||||
# Music
|
# Music
|
||||||
MUSIC_DIR = os.path.join(RESOURCES_DIR, "musics")
|
MUSIC_DIR = os.path.join(RESOURCES_DIR, "musics")
|
||||||
@ -128,50 +119,7 @@ else:
|
|||||||
)
|
)
|
||||||
USER_PROFILE_DIR = os.path.join(USER_PROFILE_DIR, "TetrArcade")
|
USER_PROFILE_DIR = os.path.join(USER_PROFILE_DIR, "TetrArcade")
|
||||||
HIGH_SCORE_PATH = os.path.join(USER_PROFILE_DIR, ".high_score")
|
HIGH_SCORE_PATH = os.path.join(USER_PROFILE_DIR, ".high_score")
|
||||||
CONF_PATH = os.path.join(USER_PROFILE_DIR, "config.ini")
|
CONF_PATH = os.path.join(USER_PROFILE_DIR, "TetrArcade.ini")
|
||||||
|
|
||||||
|
|
||||||
class Texture:
|
|
||||||
|
|
||||||
NORMAL = 0
|
|
||||||
LOCKED = 1
|
|
||||||
|
|
||||||
|
|
||||||
class State:
|
|
||||||
|
|
||||||
STARTING = 0
|
|
||||||
PLAYING = 1
|
|
||||||
PAUSED = 2
|
|
||||||
OVER = 3
|
|
||||||
|
|
||||||
|
|
||||||
class Scheduler(AbstractScheduler):
|
|
||||||
def __init__(self):
|
|
||||||
self.tasks = {}
|
|
||||||
|
|
||||||
def postpone(self, task, delay):
|
|
||||||
_task = lambda _: task()
|
|
||||||
self.tasks[task] = _task
|
|
||||||
pyglet.clock.schedule_once(_task, delay)
|
|
||||||
|
|
||||||
def cancel(self, task):
|
|
||||||
try:
|
|
||||||
_task = self.tasks[task]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
arcade.unschedule(_task)
|
|
||||||
del self.tasks[task]
|
|
||||||
|
|
||||||
def reset(self, task, delay):
|
|
||||||
try:
|
|
||||||
_task = self.tasks[task]
|
|
||||||
except KeyError:
|
|
||||||
_task = lambda _: task()
|
|
||||||
self.tasks[task] = _task
|
|
||||||
else:
|
|
||||||
arcade.unschedule(_task)
|
|
||||||
pyglet.clock.schedule_once(_task, delay)
|
|
||||||
|
|
||||||
|
|
||||||
class MinoSprite(arcade.Sprite):
|
class MinoSprite(arcade.Sprite):
|
||||||
@ -182,24 +130,19 @@ class MinoSprite(arcade.Sprite):
|
|||||||
self.append_texture(TEXTURES[mino.color])
|
self.append_texture(TEXTURES[mino.color])
|
||||||
self.append_texture(TEXTURES[Color.LOCKED])
|
self.append_texture(TEXTURES[Color.LOCKED])
|
||||||
self.set_texture(0)
|
self.set_texture(0)
|
||||||
self.resize()
|
|
||||||
|
|
||||||
def resize(self):
|
def update(self, x, y, texture=0):
|
||||||
self.scale = self.window.scale
|
self.scale = self.window.scale
|
||||||
self.size = MINO_SIZE * self.window.scale
|
size = MINO_SIZE * self.scale
|
||||||
|
self.left = self.window.matrix.bg.left + x * size
|
||||||
def update(self, x, y):
|
self.bottom = self.window.matrix.bg.bottom + y * size
|
||||||
self.left = self.window.matrix.bg.left + x * self.size
|
self.set_texture(texture)
|
||||||
self.bottom = self.window.matrix.bg.bottom + y * self.size
|
|
||||||
|
|
||||||
def fall(self, lines_cleared):
|
|
||||||
self.bottom -= MINO_SIZE * self.window.scale * lines_cleared
|
|
||||||
|
|
||||||
|
|
||||||
class MinoesSprites(arcade.SpriteList):
|
class MinoesSprites(arcade.SpriteList):
|
||||||
def resize(self):
|
def resize(self, scale):
|
||||||
for sprite in self:
|
for sprite in self:
|
||||||
sprite.resize()
|
sprite.scale = scale
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
|
||||||
@ -208,26 +151,21 @@ class TetrominoSprites(MinoesSprites):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.tetromino = tetromino
|
self.tetromino = tetromino
|
||||||
self.alpha = alpha
|
self.alpha = alpha
|
||||||
self.window = window
|
|
||||||
for mino in tetromino:
|
for mino in tetromino:
|
||||||
mino.sprite = MinoSprite(mino, window, alpha)
|
mino.sprite = MinoSprite(mino, window, alpha)
|
||||||
self.append(mino.sprite)
|
self.append(mino.sprite)
|
||||||
|
|
||||||
def update(self):
|
def update(self, texture=NORMAL_TEXTURE):
|
||||||
for mino in self.tetromino:
|
for mino in self.tetromino:
|
||||||
coord = mino.coord + self.tetromino.coord
|
coord = mino.coord + self.tetromino.coord
|
||||||
mino.sprite.update(coord.x, coord.y)
|
mino.sprite.update(coord.x, coord.y, texture)
|
||||||
|
|
||||||
def set_texture(self, texture):
|
|
||||||
for mino in self.tetromino:
|
|
||||||
mino.sprite.set_texture(texture)
|
|
||||||
mino.sprite.scale = self.window.scale
|
|
||||||
|
|
||||||
|
|
||||||
class MatrixSprites(MinoesSprites):
|
class MatrixSprites(MinoesSprites):
|
||||||
def __init__(self, matrix):
|
def __init__(self, matrix):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.matrix = matrix
|
self.matrix = matrix
|
||||||
|
self.update()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
for y, line in enumerate(self.matrix):
|
for y, line in enumerate(self.matrix):
|
||||||
@ -235,21 +173,17 @@ class MatrixSprites(MinoesSprites):
|
|||||||
if mino:
|
if mino:
|
||||||
mino.sprite.update(x, y)
|
mino.sprite.update(x, y)
|
||||||
|
|
||||||
def remove_lines(self, lines_to_remove):
|
def remove_line(self, y):
|
||||||
for y in lines_to_remove:
|
for mino in self.matrix[y]:
|
||||||
for mino in self.matrix[y]:
|
if mino:
|
||||||
if mino:
|
self.remove(mino.sprite)
|
||||||
self.remove(mino.sprite)
|
|
||||||
|
|
||||||
|
|
||||||
class TetrArcade(TetrisLogic, arcade.Window):
|
class TetrArcade(TetrisLogic, arcade.Window):
|
||||||
"""Tetris clone with arcade GUI library"""
|
|
||||||
|
|
||||||
timer = Scheduler()
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
locale.setlocale(locale.LC_ALL, "")
|
locale.setlocale(locale.LC_ALL, "")
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
|
self.tasks = {}
|
||||||
|
|
||||||
self.conf = configparser.ConfigParser()
|
self.conf = configparser.ConfigParser()
|
||||||
if self.conf.read(CONF_PATH):
|
if self.conf.read(CONF_PATH):
|
||||||
@ -295,8 +229,6 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
else:
|
else:
|
||||||
self.music = None
|
self.music = None
|
||||||
|
|
||||||
self.state = State.STARTING
|
|
||||||
|
|
||||||
def new_conf(self):
|
def new_conf(self):
|
||||||
self.conf["WINDOW"] = {
|
self.conf["WINDOW"] = {
|
||||||
"width": WINDOW_WIDTH,
|
"width": WINDOW_WIDTH,
|
||||||
@ -331,13 +263,13 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
for action, key in self.conf["KEYBOARD"].items():
|
for action, key in self.conf["KEYBOARD"].items():
|
||||||
self.conf["KEYBOARD"][action] = key.upper()
|
self.conf["KEYBOARD"][action] = key.upper()
|
||||||
self.key_map = {
|
self.key_map = {
|
||||||
State.STARTING: {
|
Phase.STARTING: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
||||||
getattr(
|
getattr(
|
||||||
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
): self.toggle_fullscreen,
|
): self.toggle_fullscreen,
|
||||||
},
|
},
|
||||||
State.PLAYING: {
|
Phase.FALLING: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["move left"]): self.move_left,
|
getattr(arcade.key, self.conf["KEYBOARD"]["move left"]): self.move_left,
|
||||||
getattr(
|
getattr(
|
||||||
arcade.key, self.conf["KEYBOARD"]["move right"]
|
arcade.key, self.conf["KEYBOARD"]["move right"]
|
||||||
@ -356,13 +288,32 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
): self.toggle_fullscreen,
|
): self.toggle_fullscreen,
|
||||||
},
|
},
|
||||||
State.PAUSED: {
|
Phase.LOCK: {
|
||||||
|
getattr(arcade.key, self.conf["KEYBOARD"]["move left"]): self.move_left,
|
||||||
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["move right"]
|
||||||
|
): self.move_right,
|
||||||
|
getattr(arcade.key, self.conf["KEYBOARD"]["soft drop"]): self.soft_drop,
|
||||||
|
getattr(arcade.key, self.conf["KEYBOARD"]["hard drop"]): self.hard_drop,
|
||||||
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["rotate clockwise"]
|
||||||
|
): self.rotate_clockwise,
|
||||||
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["rotate counter"]
|
||||||
|
): self.rotate_counter,
|
||||||
|
getattr(arcade.key, self.conf["KEYBOARD"]["hold"]): self.hold,
|
||||||
|
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.pause,
|
||||||
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
|
): self.toggle_fullscreen,
|
||||||
|
},
|
||||||
|
Phase.PAUSED: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.resume,
|
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.resume,
|
||||||
getattr(
|
getattr(
|
||||||
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
): self.toggle_fullscreen,
|
): self.toggle_fullscreen,
|
||||||
},
|
},
|
||||||
State.OVER: {
|
Phase.OVER: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
||||||
getattr(
|
getattr(
|
||||||
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
@ -404,10 +355,10 @@ AGAIN""".format(
|
|||||||
|
|
||||||
self.play_music = self.conf["MUSIC"].getboolean("play")
|
self.play_music = self.conf["MUSIC"].getboolean("play")
|
||||||
|
|
||||||
def on_new_game(self, matrix, next_pieces):
|
def on_new_game(self, next_pieces):
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
|
|
||||||
self.matrix.sprites = MatrixSprites(matrix)
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
for piece in next_pieces:
|
for piece in next_pieces:
|
||||||
piece.sprites = TetrominoSprites(piece, self)
|
piece.sprites = TetrominoSprites(piece, self)
|
||||||
|
|
||||||
@ -415,8 +366,6 @@ AGAIN""".format(
|
|||||||
self.music.seek(0)
|
self.music.seek(0)
|
||||||
self.music.play()
|
self.music.play()
|
||||||
|
|
||||||
self.state = State.PLAYING
|
|
||||||
|
|
||||||
def on_new_level(self, level):
|
def on_new_level(self, level):
|
||||||
self.show_text("LEVEL\n{:n}".format(level))
|
self.show_text("LEVEL\n{:n}".format(level))
|
||||||
|
|
||||||
@ -427,115 +376,97 @@ AGAIN""".format(
|
|||||||
next_pieces[-1].sprites = TetrominoSprites(next_pieces[-1], self)
|
next_pieces[-1].sprites = TetrominoSprites(next_pieces[-1], self)
|
||||||
for piece, coord in zip(next_pieces, NEXT_PIECES_COORDS):
|
for piece, coord in zip(next_pieces, NEXT_PIECES_COORDS):
|
||||||
piece.coord = coord
|
piece.coord = coord
|
||||||
for piece in [falling_piece, ghost_piece] + next_pieces:
|
|
||||||
piece.sprites.update()
|
|
||||||
|
|
||||||
def on_falling_phase(self, falling_piece, ghost_piece):
|
def on_locked(self, matrix, locked_piece):
|
||||||
falling_piece.sprites.set_texture(Texture.NORMAL)
|
for mino in locked_piece:
|
||||||
falling_piece.sprites.update()
|
|
||||||
ghost_piece.sprites.update()
|
|
||||||
|
|
||||||
def on_locked(self, falling_piece, ghost_piece):
|
|
||||||
falling_piece.sprites.set_texture(Texture.LOCKED)
|
|
||||||
falling_piece.sprites.update()
|
|
||||||
ghost_piece.sprites.update()
|
|
||||||
|
|
||||||
def on_locks_down(self, matrix, falling_piece):
|
|
||||||
falling_piece.sprites.set_texture(Texture.NORMAL)
|
|
||||||
for mino in falling_piece:
|
|
||||||
matrix.sprites.append(mino.sprite)
|
matrix.sprites.append(mino.sprite)
|
||||||
|
|
||||||
def on_animate_phase(self, matrix, lines_to_remove):
|
def on_line_remove(self, matrix, y):
|
||||||
if not lines_to_remove:
|
line_textures = tuple(TEXTURES[mino.color] for mino in matrix[y])
|
||||||
return
|
self.exploding_minoes[y] = arcade.Emitter(
|
||||||
|
center_xy=(matrix.bg.left, matrix.bg.bottom + (y + 0.5) * MINO_SIZE),
|
||||||
self.timer.cancel(self.clean_particules)
|
emit_controller=arcade.EmitBurst(COLLUMNS),
|
||||||
for y in lines_to_remove:
|
particle_factory=lambda emitter: arcade.LifetimeParticle(
|
||||||
line_textures = tuple(TEXTURES[mino.color] for mino in matrix[y])
|
filename_or_texture=random.choice(line_textures),
|
||||||
self.exploding_minoes[y] = arcade.Emitter(
|
change_xy=arcade.rand_in_rect(
|
||||||
center_xy=(matrix.bg.left, matrix.bg.bottom + (y + 0.5) * MINO_SIZE),
|
(-COLLUMNS * MINO_SIZE, -4 * MINO_SIZE),
|
||||||
emit_controller=arcade.EmitBurst(COLLUMNS),
|
2 * COLLUMNS * MINO_SIZE,
|
||||||
particle_factory=lambda emitter: arcade.LifetimeParticle(
|
5 * MINO_SIZE,
|
||||||
filename_or_texture=random.choice(line_textures),
|
|
||||||
change_xy=arcade.rand_in_rect(
|
|
||||||
(-COLLUMNS * MINO_SIZE, -4 * MINO_SIZE),
|
|
||||||
2 * COLLUMNS * MINO_SIZE,
|
|
||||||
5 * MINO_SIZE,
|
|
||||||
),
|
|
||||||
lifetime=EXPLOSION_ANIMATION,
|
|
||||||
center_xy=arcade.rand_on_line((0, 0), (matrix.bg.width, 0)),
|
|
||||||
scale=self.scale,
|
|
||||||
alpha=NORMAL_ALPHA,
|
|
||||||
change_angle=2,
|
|
||||||
),
|
),
|
||||||
)
|
lifetime=1,
|
||||||
self.timer.postpone(self.clean_particules, EXPLOSION_ANIMATION)
|
center_xy=arcade.rand_on_line((0, 0), (matrix.bg.width, 0)),
|
||||||
|
scale=self.scale,
|
||||||
|
alpha=NORMAL_ALPHA,
|
||||||
|
change_angle=2,
|
||||||
|
mutation_callback=self.speed_up_particule,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
def clean_particules(self):
|
matrix.sprites.remove_line(y)
|
||||||
self.exploding_minoes = [None for y in range(LINES)]
|
|
||||||
|
|
||||||
def on_eliminate_phase(self, matrix, lines_to_remove):
|
def speed_up_particule(self, particule):
|
||||||
matrix.sprites.remove_lines(lines_to_remove)
|
particule.change_x *= PARTICULE_ACCELERATION
|
||||||
|
particule.change_y *= PARTICULE_ACCELERATION
|
||||||
|
|
||||||
def on_completion_phase(self, pattern_name, pattern_score, nb_combo, combo_score):
|
def on_pattern_phase(self, pattern_name, pattern_score, nb_combo, combo_score):
|
||||||
if pattern_score:
|
if pattern_score:
|
||||||
self.show_text("{:s}\n{:n}".format(pattern_name, pattern_score))
|
self.show_text("{:s}\n{:n}".format(pattern_name, pattern_score))
|
||||||
if combo_score:
|
if combo_score:
|
||||||
self.show_text("COMBO x{:n}\n{:n}".format(nb_combo, combo_score))
|
self.show_text("COMBO x{:n}\n{:n}".format(nb_combo, combo_score))
|
||||||
|
|
||||||
def on_hold(self, held_piece):
|
def on_hold(self, held_piece, falling_piece, ghost_piece):
|
||||||
held_piece.coord = HELD_PIECE_COORD
|
held_piece.coord = HELD_PIECE_COORD
|
||||||
if type(held_piece) == I_Tetrimino:
|
if type(held_piece) == I_Tetrimino:
|
||||||
held_piece.coord += Movement.LEFT
|
held_piece.coord += Movement.LEFT
|
||||||
held_piece.sprites.set_texture(Texture.NORMAL)
|
ghost_piece.sprites = TetrominoSprites(ghost_piece, self, GHOST_ALPHA)
|
||||||
held_piece.sprites.update()
|
|
||||||
|
|
||||||
def on_pause(self):
|
def pause(self):
|
||||||
self.state = State.PAUSED
|
super().pause()
|
||||||
if self.music:
|
if self.music:
|
||||||
self.music.pause()
|
self.music.pause()
|
||||||
|
|
||||||
def resume(self):
|
def resume(self):
|
||||||
|
super().resume()
|
||||||
if self.music:
|
if self.music:
|
||||||
self.music.play()
|
self.music.play()
|
||||||
self.state = State.PLAYING
|
|
||||||
|
|
||||||
def on_game_over(self):
|
def on_game_over(self):
|
||||||
self.state = State.OVER
|
|
||||||
if self.music:
|
if self.music:
|
||||||
self.music.pause()
|
self.music.pause()
|
||||||
|
|
||||||
def on_key_press(self, key, modifiers):
|
def on_key_press(self, key, modifiers):
|
||||||
try:
|
for key_or_modifier in (key, modifiers):
|
||||||
action = self.key_map[self.state][key]
|
try:
|
||||||
except KeyError:
|
action = self.key_map[self.phase][key_or_modifier]
|
||||||
return
|
except KeyError:
|
||||||
else:
|
pass
|
||||||
self.do_action(action)
|
else:
|
||||||
|
self.do_action(action)
|
||||||
|
|
||||||
def on_key_release(self, key, modifiers):
|
def on_key_release(self, key, modifiers):
|
||||||
try:
|
for key_or_modifier in (key, modifiers):
|
||||||
action = self.key_map[self.state][key]
|
try:
|
||||||
except KeyError:
|
action = self.key_map[self.phase][key_or_modifier]
|
||||||
return
|
except KeyError:
|
||||||
else:
|
pass
|
||||||
self.remove_action(action)
|
else:
|
||||||
|
self.remove_action(action)
|
||||||
|
|
||||||
def show_text(self, text):
|
def show_text(self, text):
|
||||||
self.highlight_texts.append(text)
|
self.highlight_texts.append(text)
|
||||||
self.timer.postpone(self.del_highlight_text, HIGHLIGHT_TEXT_DISPLAY_DELAY)
|
self.restart(self.del_highlight_text, HIGHLIGHT_TEXT_DISPLAY_DELAY)
|
||||||
|
|
||||||
def del_highlight_text(self):
|
def del_highlight_text(self):
|
||||||
if self.highlight_texts:
|
if self.highlight_texts:
|
||||||
self.highlight_texts.pop(0)
|
self.highlight_texts.pop(0)
|
||||||
else:
|
else:
|
||||||
self.timer.cancel(self.del_highlight_text)
|
self.stop(self.del_highlight_text)
|
||||||
|
|
||||||
def on_draw(self):
|
def on_draw(self):
|
||||||
arcade.start_render()
|
arcade.start_render()
|
||||||
self.bg.draw()
|
self.bg.draw()
|
||||||
|
|
||||||
if self.state not in (State.STARTING, State.PAUSED):
|
if self.phase not in (Phase.STARTING, Phase.PAUSED):
|
||||||
self.matrix.bg.draw()
|
self.matrix.bg.draw()
|
||||||
self.matrix.sprites.draw()
|
self.matrix.sprites.draw()
|
||||||
|
|
||||||
@ -589,11 +520,11 @@ AGAIN""".format(
|
|||||||
exploding_minoes.draw()
|
exploding_minoes.draw()
|
||||||
|
|
||||||
highlight_text = {
|
highlight_text = {
|
||||||
State.STARTING: self.start_text,
|
Phase.STARTING: self.start_text,
|
||||||
State.PLAYING: self.highlight_texts[0] if self.highlight_texts else "",
|
Phase.FALLING: self.highlight_texts[0] if self.highlight_texts else "",
|
||||||
State.PAUSED: self.pause_text,
|
Phase.PAUSED: self.pause_text,
|
||||||
State.OVER: self.game_over_text,
|
Phase.OVER: self.game_over_text,
|
||||||
}.get(self.state, "")
|
}.get(self.phase, "")
|
||||||
if highlight_text:
|
if highlight_text:
|
||||||
arcade.draw_text(
|
arcade.draw_text(
|
||||||
text=highlight_text,
|
text=highlight_text,
|
||||||
@ -629,7 +560,7 @@ AGAIN""".format(
|
|||||||
self.matrix.bg.left = int(self.matrix.bg.left)
|
self.matrix.bg.left = int(self.matrix.bg.left)
|
||||||
self.matrix.bg.top = int(self.matrix.bg.top)
|
self.matrix.bg.top = int(self.matrix.bg.top)
|
||||||
|
|
||||||
self.matrix.sprites.resize()
|
self.matrix.sprites.resize(self.scale)
|
||||||
|
|
||||||
for tetromino in [
|
for tetromino in [
|
||||||
self.held.piece,
|
self.held.piece,
|
||||||
@ -637,7 +568,7 @@ AGAIN""".format(
|
|||||||
self.matrix.ghost,
|
self.matrix.ghost,
|
||||||
] + self.next.pieces:
|
] + self.next.pieces:
|
||||||
if tetromino:
|
if tetromino:
|
||||||
tetromino.sprites.resize()
|
tetromino.sprites.resize(self.scale)
|
||||||
|
|
||||||
def load_high_score(self):
|
def load_high_score(self):
|
||||||
try:
|
try:
|
||||||
@ -664,7 +595,37 @@ High score could not be saved:
|
|||||||
+ str(e)
|
+ str(e)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def start(self, task, period):
|
||||||
|
_task = lambda _: task()
|
||||||
|
self.tasks[task] = _task
|
||||||
|
arcade.schedule(_task, period)
|
||||||
|
|
||||||
|
def stop(self, task):
|
||||||
|
try:
|
||||||
|
_task = self.tasks[task]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
arcade.unschedule(_task)
|
||||||
|
del self.tasks[task]
|
||||||
|
|
||||||
|
def restart(self, task, period):
|
||||||
|
try:
|
||||||
|
_task = self.tasks[task]
|
||||||
|
except KeyError:
|
||||||
|
_task = lambda _: task()
|
||||||
|
self.tasks[task] = _task
|
||||||
|
else:
|
||||||
|
arcade.unschedule(_task)
|
||||||
|
arcade.schedule(_task, period)
|
||||||
|
|
||||||
def update(self, delta_time):
|
def update(self, delta_time):
|
||||||
|
for piece in [self.held.piece, self.matrix.ghost] + self.next.pieces:
|
||||||
|
if piece:
|
||||||
|
piece.sprites.update()
|
||||||
|
if self.matrix.piece:
|
||||||
|
texture = LOCKED_TEXTURE if self.phase == Phase.LOCK else NORMAL_TEXTURE
|
||||||
|
self.matrix.piece.sprites.update(texture=texture)
|
||||||
for exploding_minoes in self.exploding_minoes:
|
for exploding_minoes in self.exploding_minoes:
|
||||||
if exploding_minoes:
|
if exploding_minoes:
|
||||||
exploding_minoes.update()
|
exploding_minoes.update()
|
||||||
|
2
setup.py
2
setup.py
@ -29,7 +29,7 @@ options = {
|
|||||||
}
|
}
|
||||||
setup(
|
setup(
|
||||||
name="TetrArcade",
|
name="TetrArcade",
|
||||||
version="0.6",
|
version="0.4",
|
||||||
description="Tetris clone",
|
description="Tetris clone",
|
||||||
author="AdrienMalin",
|
author="AdrienMalin",
|
||||||
executables=[executable],
|
executables=[executable],
|
||||||
|
10
test.py
10
test.py
@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from TetrArcade import TetrArcade, MinoSprite, State
|
from TetrArcade import TetrArcade, Phase, MinoSprite
|
||||||
from tetrislogic import Mino, Color, Coord
|
from tetrislogic import Mino, Color, Coord
|
||||||
|
|
||||||
game = TetrArcade()
|
game = TetrArcade()
|
||||||
@ -15,20 +15,16 @@ game.pause()
|
|||||||
game.resume()
|
game.resume()
|
||||||
game.move_right()
|
game.move_right()
|
||||||
game.hold()
|
game.hold()
|
||||||
game.update(0)
|
|
||||||
game.on_draw()
|
|
||||||
game.rotate_clockwise()
|
game.rotate_clockwise()
|
||||||
game.hold()
|
game.hold()
|
||||||
game.update(0)
|
|
||||||
game.on_draw()
|
|
||||||
game.rotate_counter()
|
game.rotate_counter()
|
||||||
for i in range(22):
|
for i in range(22):
|
||||||
game.soft_drop()
|
game.soft_drop()
|
||||||
game.on_draw()
|
game.on_draw()
|
||||||
game.lock_phase()
|
game.lock_phase()
|
||||||
game.hold()
|
game.hold()
|
||||||
game.update(0)
|
game.matrix.sprites.update()
|
||||||
game.on_draw()
|
game.on_draw()
|
||||||
while game.state != State.OVER:
|
while game.phase != Phase.OVER:
|
||||||
game.hard_drop()
|
game.hard_drop()
|
||||||
game.on_draw()
|
game.on_draw()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from .consts import LINES, COLLUMNS, NEXT_PIECES
|
from .consts import LINES, COLLUMNS, NEXT_PIECES
|
||||||
from .utils import Movement, Spin, Color, Coord
|
from .utils import Movement, Rotation, Color, Coord, Phase
|
||||||
from .tetromino import (
|
from .tetromino import (
|
||||||
Mino,
|
Mino,
|
||||||
Tetromino,
|
Tetromino,
|
||||||
@ -12,4 +12,4 @@ from .tetromino import (
|
|||||||
T_Tetrimino,
|
T_Tetrimino,
|
||||||
Z_Tetrimino,
|
Z_Tetrimino,
|
||||||
)
|
)
|
||||||
from .tetrislogic import TetrisLogic, Matrix, AbstractScheduler
|
from .tetrislogic import TetrisLogic, Matrix
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from .utils import Coord, T_Spin
|
from .utils import Coord
|
||||||
|
|
||||||
|
|
||||||
# Matrix
|
# Matrix
|
||||||
@ -14,14 +14,4 @@ AUTOREPEAT_DELAY = 0.300 # Official : 0.300 s
|
|||||||
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010 s
|
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010 s
|
||||||
|
|
||||||
# Piece init coord
|
# Piece init coord
|
||||||
FALLING_PIECE_COORD = Coord(4, LINES)
|
MATRIX_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,12 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Tetris game logic meant to be implemented with GUI
|
|
||||||
Follows Tetris Guidelines 2009 (see https://tetris.fandom.com/wiki/Tetris_Guideline)
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
from .utils import Coord, Movement, Spin, T_Spin, T_Slot
|
from .utils import Coord, Movement, Rotation, T_Spin, Phase
|
||||||
from .tetromino import Tetromino, T_Tetrimino
|
from .tetromino import Tetromino, T_Tetrimino
|
||||||
from .consts import (
|
from .consts import (
|
||||||
LINES,
|
LINES,
|
||||||
@ -16,55 +11,32 @@ from .consts import (
|
|||||||
FALL_DELAY,
|
FALL_DELAY,
|
||||||
AUTOREPEAT_DELAY,
|
AUTOREPEAT_DELAY,
|
||||||
AUTOREPEAT_PERIOD,
|
AUTOREPEAT_PERIOD,
|
||||||
FALLING_PIECE_COORD,
|
MATRIX_PIECE_COORD,
|
||||||
SCORES,
|
|
||||||
LINES_CLEAR_NAME,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
|
||||||
CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
|
CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
|
||||||
|
|
||||||
|
|
||||||
class AbstractScheduler:
|
class PieceContainer:
|
||||||
"""Scheduler class to be implemented"""
|
|
||||||
|
|
||||||
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 AbstractPieceContainer:
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.piece = None
|
self.piece = None
|
||||||
|
|
||||||
|
|
||||||
class HoldQueue(AbstractPieceContainer):
|
class HoldQueue(PieceContainer):
|
||||||
"""the storage place where players can Hold any falling Tetrimino for use later"""
|
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Matrix(list, AbstractPieceContainer):
|
class Matrix(list, PieceContainer):
|
||||||
"""the rectangular arrangement of cells creating the active game area, usually 10 columns wide by 20 rows high."""
|
|
||||||
|
|
||||||
def __init__(self, lines, collumns):
|
def __init__(self, lines, collumns):
|
||||||
list.__init__(self)
|
list.__init__(self)
|
||||||
AbstractPieceContainer.__init__(self)
|
PieceContainer.__init__(self)
|
||||||
self.lines = lines
|
self.lines = lines
|
||||||
self.collumns = collumns
|
self.collumns = collumns
|
||||||
self.ghost = None
|
self.ghost = None
|
||||||
|
|
||||||
def new_game(self):
|
def reset(self):
|
||||||
"""Removes all minoes in matrix"""
|
|
||||||
self.clear()
|
self.clear()
|
||||||
for y in range(self.lines + 3):
|
for y in range(self.lines + 3):
|
||||||
self.append_new_line()
|
self.append_new_line()
|
||||||
@ -77,36 +49,23 @@ class Matrix(list, AbstractPieceContainer):
|
|||||||
0 <= coord.x < self.collumns 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 NextQueue(AbstractPieceContainer):
|
|
||||||
"""Displays the Next Tetrimino(s) to be placed (generated) just above the Matrix"""
|
|
||||||
|
|
||||||
|
class NextQueue(PieceContainer):
|
||||||
def __init__(self, nb_pieces):
|
def __init__(self, nb_pieces):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.nb_pieces = nb_pieces
|
self.nb_pieces = nb_pieces
|
||||||
self.pieces = []
|
self.pieces = []
|
||||||
|
|
||||||
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:
|
class Stats:
|
||||||
"""Game statistics"""
|
|
||||||
|
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 _get_score(self):
|
def _get_score(self):
|
||||||
return self._score
|
return self._score
|
||||||
@ -145,7 +104,7 @@ class Stats:
|
|||||||
def update_time(self):
|
def update_time(self):
|
||||||
self.time += 1
|
self.time += 1
|
||||||
|
|
||||||
def locks_down(self, t_spin, lines_cleared):
|
def pattern_phase(self, t_spin, lines_cleared):
|
||||||
pattern_name = []
|
pattern_name = []
|
||||||
pattern_score = 0
|
pattern_score = 0
|
||||||
combo_score = 0
|
combo_score = 0
|
||||||
@ -153,13 +112,13 @@ class Stats:
|
|||||||
if t_spin:
|
if t_spin:
|
||||||
pattern_name.append(t_spin)
|
pattern_name.append(t_spin)
|
||||||
if lines_cleared:
|
if lines_cleared:
|
||||||
pattern_name.append(SCORES[lines_cleared][LINES_CLEAR_NAME])
|
pattern_name.append(self.SCORES[lines_cleared][LINES_CLEAR_NAME])
|
||||||
self.combo += 1
|
self.combo += 1
|
||||||
else:
|
else:
|
||||||
self.combo = -1
|
self.combo = -1
|
||||||
|
|
||||||
if lines_cleared or t_spin:
|
if lines_cleared or t_spin:
|
||||||
pattern_score = SCORES[lines_cleared][t_spin]
|
pattern_score = self.SCORES[lines_cleared][t_spin]
|
||||||
self.goal -= pattern_score
|
self.goal -= pattern_score
|
||||||
pattern_score *= 100 * self.level
|
pattern_score *= 100 * self.level
|
||||||
pattern_name = "\n".join(pattern_name)
|
pattern_name = "\n".join(pattern_name)
|
||||||
@ -173,41 +132,39 @@ class Stats:
|
|||||||
|
|
||||||
|
|
||||||
class TetrisLogic:
|
class TetrisLogic:
|
||||||
"""Tetris game logic"""
|
|
||||||
|
|
||||||
# These class attributes can be redefined on inheritance
|
LINES = LINES
|
||||||
|
COLLUMNS = COLLUMNS
|
||||||
|
NEXT_PIECES = NEXT_PIECES
|
||||||
AUTOREPEAT_DELAY = AUTOREPEAT_DELAY
|
AUTOREPEAT_DELAY = AUTOREPEAT_DELAY
|
||||||
AUTOREPEAT_PERIOD = AUTOREPEAT_PERIOD
|
AUTOREPEAT_PERIOD = AUTOREPEAT_PERIOD
|
||||||
FALLING_PIECE_COORD = FALLING_PIECE_COORD
|
MATRIX_PIECE_COORD = MATRIX_PIECE_COORD
|
||||||
|
|
||||||
timer = AbstractScheduler()
|
def __init__(self, lines=LINES, collumns=COLLUMNS, next_pieces=NEXT_PIECES):
|
||||||
|
|
||||||
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.stats = Stats()
|
||||||
self.load_high_score()
|
self.load_high_score()
|
||||||
|
self.phase = Phase.STARTING
|
||||||
self.held = HoldQueue()
|
self.held = HoldQueue()
|
||||||
self.matrix = Matrix(lines, collumns)
|
self.matrix = Matrix(lines, collumns)
|
||||||
self.next = NextQueue(nb_next_pieces)
|
self.next = NextQueue(next_pieces)
|
||||||
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
||||||
self.pressed_actions = []
|
self.pressed_actions = []
|
||||||
|
|
||||||
def new_game(self, level=1):
|
def new_game(self, level=1):
|
||||||
"""start a new game at `level`"""
|
|
||||||
self.stats.new_game(level)
|
self.stats.new_game(level)
|
||||||
|
|
||||||
self.pressed_actions = []
|
self.pressed_actions = []
|
||||||
|
self.auto_repeat = False
|
||||||
|
|
||||||
self.matrix.new_game()
|
self.matrix.reset()
|
||||||
self.next.new_game()
|
self.next.pieces = [Tetromino() for n in range(self.next.nb_pieces)]
|
||||||
self.held.piece = None
|
self.held.piece = None
|
||||||
self.timer.postpone(self.stats.update_time, 1)
|
self.start(self.stats.update_time, 1)
|
||||||
|
|
||||||
self.on_new_game(self.matrix, self.next.pieces)
|
self.on_new_game(self.next.pieces)
|
||||||
self.new_level()
|
self.new_level()
|
||||||
|
|
||||||
def on_new_game(self, matrix, next_pieces):
|
def on_new_game(self, next_pieces):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def new_level(self):
|
def new_level(self):
|
||||||
@ -218,95 +175,133 @@ class TetrisLogic:
|
|||||||
def on_new_level(self, level):
|
def on_new_level(self, level):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Tetris Engine
|
def generation_phase(self):
|
||||||
|
self.phase = Phase.GENERATION
|
||||||
def generation_phase(self, held_piece=None):
|
self.matrix.piece = self.next.pieces.pop(0)
|
||||||
if not held_piece:
|
self.next.pieces.append(Tetromino())
|
||||||
self.matrix.piece = self.next.generation_phase()
|
self.matrix.piece.coord = self.MATRIX_PIECE_COORD
|
||||||
self.matrix.piece.coord = self.FALLING_PIECE_COORD
|
|
||||||
self.matrix.ghost = self.matrix.piece.ghost()
|
self.matrix.ghost = self.matrix.piece.ghost()
|
||||||
self.refresh_ghost()
|
|
||||||
|
|
||||||
self.on_generation_phase(
|
self.on_generation_phase(
|
||||||
self.matrix, self.matrix.piece, self.matrix.ghost, self.next.pieces
|
self.matrix, self.matrix.piece, self.matrix.ghost, self.next.pieces
|
||||||
)
|
)
|
||||||
if self.matrix.space_to_move(
|
if not self.move(Movement.DOWN):
|
||||||
self.matrix.piece.coord, (mino.coord for mino in self.matrix.piece)
|
|
||||||
):
|
|
||||||
self.falling_phase()
|
|
||||||
else:
|
|
||||||
self.game_over()
|
self.game_over()
|
||||||
|
else:
|
||||||
def refresh_ghost(self):
|
self.restart(self.fall, self.stats.fall_delay)
|
||||||
self.matrix.ghost.coord = self.matrix.piece.coord
|
self.falling_phase()
|
||||||
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):
|
def on_generation_phase(self, matrix, falling_piece, ghost_piece, next_pieces):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def falling_phase(self):
|
def falling_phase(self):
|
||||||
self.timer.cancel(self.lock_phase)
|
self.phase = Phase.FALLING
|
||||||
self.timer.cancel(self.locks_down)
|
self.matrix.ghost.coord = self.matrix.piece.coord
|
||||||
self.matrix.piece.locked = False
|
for ghost_mino, current_mino in zip(self.matrix.ghost, self.matrix.piece):
|
||||||
self.timer.postpone(self.lock_phase, self.stats.fall_delay)
|
ghost_mino.coord = current_mino.coord
|
||||||
|
while self.space_to_move(
|
||||||
|
self.matrix.ghost.coord + Movement.DOWN,
|
||||||
|
(mino.coord for mino in self.matrix.ghost),
|
||||||
|
):
|
||||||
|
self.matrix.ghost.coord += Movement.DOWN
|
||||||
|
|
||||||
self.on_falling_phase(self.matrix.piece, self.matrix.ghost)
|
self.on_falling_phase(self.matrix.piece, self.matrix.ghost)
|
||||||
|
|
||||||
def on_falling_phase(self, falling_piece, ghost_piece):
|
def on_falling_phase(self, falling_piece, ghost_piece):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def lock_phase(self):
|
def fall(self):
|
||||||
self.move(Movement.DOWN)
|
self.move(Movement.DOWN)
|
||||||
|
|
||||||
def move(self, movement, rotated_coords=None, lock=True):
|
def move(self, movement, rotated_coords=None, lock=True):
|
||||||
potential_coord = self.matrix.piece.coord + movement
|
potential_coord = self.matrix.piece.coord + movement
|
||||||
potential_minoes_coords = rotated_coords or (
|
if self.space_to_move(
|
||||||
mino.coord for mino in self.matrix.piece
|
potential_coord,
|
||||||
)
|
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
|
self.matrix.piece.coord = potential_coord
|
||||||
if rotated_coords:
|
if rotated_coords:
|
||||||
for mino, coord in zip(self.matrix.piece, rotated_coords):
|
for mino, coord in zip(self.matrix.piece, rotated_coords):
|
||||||
mino.coord = coord
|
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:
|
else:
|
||||||
self.matrix.piece.locked = True
|
if movement != Movement.DOWN:
|
||||||
self.on_locked(self.matrix.piece, self.matrix.ghost)
|
self.matrix.piece.last_rotation_point = None
|
||||||
self.timer.reset(self.locks_down, self.stats.lock_delay)
|
if self.phase == Phase.LOCK:
|
||||||
|
self.restart(self.pattern_phase, self.stats.lock_delay)
|
||||||
|
self.falling_phase()
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
if lock and self.phase != Phase.LOCK and movement == Movement.DOWN:
|
||||||
|
self.lock_phase()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def on_locked(self, falling_piece, ghost_piece):
|
def lock_phase(self):
|
||||||
|
self.phase = Phase.LOCK
|
||||||
|
self.on_lock_phase(self.matrix.piece)
|
||||||
|
self.start(self.pattern_phase, self.stats.lock_delay)
|
||||||
|
|
||||||
|
def on_lock_phase(self, locked_piece):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def rotate(self, spin):
|
def space_to_move(self, potential_coord, minoes_coord):
|
||||||
rotated_coords = tuple(mino.coord @ spin for mino in self.matrix.piece)
|
return all(
|
||||||
|
self.matrix.cell_is_free(potential_coord + mino_coord)
|
||||||
|
for mino_coord in minoes_coord
|
||||||
|
)
|
||||||
|
|
||||||
|
def rotate(self, rotation):
|
||||||
|
rotated_coords = tuple(
|
||||||
|
Coord(rotation * mino.coord.y, -rotation * mino.coord.x)
|
||||||
|
for mino in self.matrix.piece
|
||||||
|
)
|
||||||
for rotation_point, liberty_degree in enumerate(
|
for rotation_point, liberty_degree in enumerate(
|
||||||
self.matrix.piece.SRS[spin][self.matrix.piece.orientation], start=1
|
self.matrix.piece.SRS[rotation][self.matrix.piece.orientation], start=1
|
||||||
):
|
):
|
||||||
if self.move(liberty_degree, rotated_coords, lock=False):
|
if self.move(liberty_degree, rotated_coords):
|
||||||
self.matrix.piece.orientation = (
|
self.matrix.piece.orientation = (
|
||||||
self.matrix.piece.orientation + spin
|
self.matrix.piece.orientation + rotation
|
||||||
) % 4
|
) % 4
|
||||||
self.matrix.piece.rotated_last = True
|
self.matrix.piece.last_rotation_point = rotation_point
|
||||||
if rotation_point == 5:
|
|
||||||
self.matrix.piece.rotation_point_5_used = True
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def locks_down(self):
|
def hold(self):
|
||||||
self.timer.cancel(self.lock_phase)
|
if not self.matrix.piece.hold_enabled:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.matrix.piece.hold_enabled = False
|
||||||
|
self.stop(self.pattern_phase)
|
||||||
|
self.stop(self.fall)
|
||||||
|
self.matrix.piece, self.held.piece = self.held.piece, self.matrix.piece
|
||||||
|
|
||||||
|
for mino, coord in zip(self.held.piece, self.held.piece.MINOES_COORDS):
|
||||||
|
mino.coord = coord
|
||||||
|
|
||||||
|
if self.matrix.piece:
|
||||||
|
self.matrix.piece.coord = self.MATRIX_PIECE_COORD
|
||||||
|
self.matrix.ghost = self.matrix.piece.ghost()
|
||||||
|
self.on_hold(self.held.piece, self.matrix.piece, self.matrix.ghost)
|
||||||
|
self.falling_phase()
|
||||||
|
else:
|
||||||
|
self.generation_phase()
|
||||||
|
self.on_hold(self.held.piece, self.matrix.piece, self.matrix.ghost)
|
||||||
|
|
||||||
|
def on_hold(self, held_piece, falling_piece, ghost_piece):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def pattern_phase(self):
|
||||||
|
self.phase = Phase.PATTERN
|
||||||
|
self.matrix.piece.prelocked = False
|
||||||
|
self.stop(self.pattern_phase)
|
||||||
|
self.stop(self.fall)
|
||||||
|
|
||||||
|
# Piece unlocked
|
||||||
|
if self.space_to_move(
|
||||||
|
self.matrix.piece.coord + Movement.DOWN,
|
||||||
|
(mino.coord for mino in self.matrix.piece),
|
||||||
|
):
|
||||||
|
return
|
||||||
|
|
||||||
# Game over
|
# Game over
|
||||||
if all(
|
if all(
|
||||||
@ -316,79 +311,67 @@ class TetrisLogic:
|
|||||||
self.game_over()
|
self.game_over()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if self.pressed_actions:
|
||||||
|
self.auto_repeat = False
|
||||||
|
self.stop(self.repeat_action)
|
||||||
|
|
||||||
for mino in self.matrix.piece:
|
for mino in self.matrix.piece:
|
||||||
coord = mino.coord + self.matrix.piece.coord
|
coord = mino.coord + self.matrix.piece.coord
|
||||||
if coord.y <= self.matrix.lines + 3:
|
if coord.y <= self.matrix.lines + 3:
|
||||||
self.matrix[coord.y][coord.x] = mino
|
self.matrix[coord.y][coord.x] = mino
|
||||||
|
self.on_locked(self.matrix, self.matrix.piece)
|
||||||
self.on_locks_down(self.matrix, self.matrix.piece)
|
|
||||||
|
|
||||||
# Pattern phase
|
|
||||||
|
|
||||||
# T-Spin
|
# T-Spin
|
||||||
if type(self.matrix.piece) == T_Tetrimino and self.matrix.piece.rotated_last:
|
if (
|
||||||
a = self.is_t_slot(T_Slot.A)
|
type(self.matrix.piece) == T_Tetrimino
|
||||||
b = self.is_t_slot(T_Slot.B)
|
and self.matrix.piece.last_rotation_point is not None
|
||||||
c = self.is_t_slot(T_Slot.C)
|
):
|
||||||
d = self.is_t_slot(T_Slot.D)
|
a = self.is_t_slot(0)
|
||||||
if a and b and (c or d):
|
b = self.is_t_slot(1)
|
||||||
|
c = self.is_t_slot(3)
|
||||||
|
d = self.is_t_slot(2)
|
||||||
|
if self.matrix.piece.last_rotation_point == 5 or (a and b and (c or d)):
|
||||||
t_spin = T_Spin.T_SPIN
|
t_spin = T_Spin.T_SPIN
|
||||||
elif c and d and (a or b):
|
elif c and d and (a or b):
|
||||||
if self.matrix.piece.rotation_point_5_used:
|
t_spin = T_Spin.MINI
|
||||||
t_spin = T_Spin.T_SPIN
|
|
||||||
else:
|
|
||||||
t_spin = T_Spin.MINI
|
|
||||||
else:
|
else:
|
||||||
t_spin = T_Spin.NONE
|
t_spin = T_Spin.NONE
|
||||||
else:
|
else:
|
||||||
t_spin = T_Spin.NONE
|
t_spin = T_Spin.NONE
|
||||||
|
|
||||||
# Complete lines
|
# Clear complete lines
|
||||||
lines_to_remove = []
|
lines_cleared = 0
|
||||||
for y, line in reversed(list(enumerate(self.matrix))):
|
for y, line in reversed(list(enumerate(self.matrix))):
|
||||||
if all(mino for mino in line):
|
if all(mino for mino in line):
|
||||||
lines_to_remove.append(y)
|
lines_cleared += 1
|
||||||
|
self.on_line_remove(self.matrix, y)
|
||||||
lines_cleared = len(lines_to_remove)
|
self.matrix.pop(y)
|
||||||
|
self.matrix.append_new_line()
|
||||||
if lines_cleared:
|
if lines_cleared:
|
||||||
self.stats.lines_cleared += lines_cleared
|
self.stats.lines_cleared += lines_cleared
|
||||||
|
|
||||||
# Animate phase
|
pattern_name, pattern_score, nb_combo, combo_score = self.stats.pattern_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
|
t_spin, lines_cleared
|
||||||
)
|
)
|
||||||
self.on_completion_phase(pattern_name, pattern_score, nb_combo, combo_score)
|
self.on_pattern_phase(pattern_name, pattern_score, nb_combo, combo_score)
|
||||||
|
|
||||||
if self.stats.goal <= 0:
|
if self.stats.goal <= 0:
|
||||||
self.new_level()
|
self.new_level()
|
||||||
else:
|
else:
|
||||||
self.generation_phase()
|
self.generation_phase()
|
||||||
|
|
||||||
def on_locks_down(self, matrix, falling_piece):
|
if self.pressed_actions:
|
||||||
|
self.start(self.repeat_action, self.AUTOREPEAT_DELAY)
|
||||||
|
|
||||||
|
def on_locked(self, matrix, locked_piece):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_animate_phase(self, matrix, lines_to_remove):
|
def on_line_remove(self, matrix, y):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_eliminate_phase(self, matrix, lines_to_remove):
|
def on_pattern_phase(self, pattern_name, pattern_score, nb_combo, combo_score):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_completion_phase(self, pattern_name, pattern_score, nb_combo, combo_score):
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Actions
|
|
||||||
|
|
||||||
def move_left(self):
|
def move_left(self):
|
||||||
self.move(Movement.LEFT)
|
self.move(Movement.LEFT)
|
||||||
|
|
||||||
@ -396,10 +379,10 @@ class TetrisLogic:
|
|||||||
self.move(Movement.RIGHT)
|
self.move(Movement.RIGHT)
|
||||||
|
|
||||||
def rotate_clockwise(self):
|
def rotate_clockwise(self):
|
||||||
self.rotate(Spin.CLOCKWISE)
|
self.rotate(Rotation.CLOCKWISE)
|
||||||
|
|
||||||
def rotate_counter(self):
|
def rotate_counter(self):
|
||||||
self.rotate(Spin.COUNTER)
|
self.rotate(Rotation.COUNTER)
|
||||||
|
|
||||||
def soft_drop(self):
|
def soft_drop(self):
|
||||||
moved = self.move(Movement.DOWN)
|
moved = self.move(Movement.DOWN)
|
||||||
@ -408,28 +391,9 @@ class TetrisLogic:
|
|||||||
return moved
|
return moved
|
||||||
|
|
||||||
def hard_drop(self):
|
def hard_drop(self):
|
||||||
self.timer.cancel(self.lock_phase)
|
|
||||||
self.timer.cancel(self.locks_down)
|
|
||||||
while self.move(Movement.DOWN, lock=False):
|
while self.move(Movement.DOWN, lock=False):
|
||||||
self.stats.score += 2
|
self.stats.score += 2
|
||||||
self.locks_down()
|
self.pattern_phase()
|
||||||
|
|
||||||
def hold(self):
|
|
||||||
if not self.matrix.piece.hold_enabled:
|
|
||||||
return
|
|
||||||
|
|
||||||
self.matrix.piece.hold_enabled = False
|
|
||||||
self.timer.cancel(self.lock_phase)
|
|
||||||
self.matrix.piece, self.held.piece = self.held.piece, self.matrix.piece
|
|
||||||
|
|
||||||
for mino, coord in zip(self.held.piece, self.held.piece.MINOES_COORDS):
|
|
||||||
mino.coord = coord
|
|
||||||
|
|
||||||
self.on_hold(self.held.piece)
|
|
||||||
self.generation_phase(self.matrix.piece)
|
|
||||||
|
|
||||||
def on_hold(self, held_piece):
|
|
||||||
pass
|
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
@ -441,25 +405,21 @@ class TetrisLogic:
|
|||||||
return not self.matrix.cell_is_free(t_slot_coord)
|
return not self.matrix.cell_is_free(t_slot_coord)
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
|
self.phase = Phase.PAUSED
|
||||||
self.stop_all()
|
self.stop_all()
|
||||||
self.pressed_actions = []
|
self.pressed_actions = []
|
||||||
self.timer.cancel(self.repeat_action)
|
self.auto_repeat = False
|
||||||
self.on_pause()
|
self.stop(self.repeat_action)
|
||||||
|
|
||||||
def on_pause(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def resume(self):
|
def resume(self):
|
||||||
self.timer.postpone(self.lock_phase, self.stats.fall_delay)
|
self.phase = Phase.FALLING
|
||||||
if self.matrix.piece.locked:
|
self.start(self.fall, self.stats.fall_delay)
|
||||||
self.timer.postpone(self.locks_down, self.stats.lock_delay)
|
if self.phase == Phase.LOCK:
|
||||||
self.timer.postpone(self.stats.update_time, 1)
|
self.start(self.pattern_phase, self.stats.lock_delay)
|
||||||
self.on_resume()
|
self.start(self.stats.update_time, 1)
|
||||||
|
|
||||||
def on_resume(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
|
self.phase = Phase.OVER
|
||||||
self.stop_all()
|
self.stop_all()
|
||||||
self.save_high_score()
|
self.save_high_score()
|
||||||
self.on_game_over()
|
self.on_game_over()
|
||||||
@ -468,26 +428,30 @@ class TetrisLogic:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def stop_all(self):
|
def stop_all(self):
|
||||||
self.timer.cancel(self.lock_phase)
|
self.stop(self.fall)
|
||||||
self.timer.cancel(self.locks_down)
|
self.stop(self.pattern_phase)
|
||||||
self.timer.cancel(self.stats.update_time)
|
self.stop(self.stats.update_time)
|
||||||
|
|
||||||
def do_action(self, action):
|
def do_action(self, action):
|
||||||
action()
|
action()
|
||||||
if action in self.autorepeatable_actions:
|
if action in self.autorepeatable_actions:
|
||||||
|
self.auto_repeat = False
|
||||||
self.pressed_actions.append(action)
|
self.pressed_actions.append(action)
|
||||||
if action == self.soft_drop:
|
if action == self.soft_drop:
|
||||||
delay = self.stats.fall_delay / 20
|
delay = self.stats.fall_delay / 20
|
||||||
else:
|
else:
|
||||||
delay = self.AUTOREPEAT_DELAY
|
delay = self.AUTOREPEAT_DELAY
|
||||||
self.timer.reset(self.repeat_action, delay)
|
self.restart(self.repeat_action, delay)
|
||||||
|
|
||||||
def repeat_action(self):
|
def repeat_action(self):
|
||||||
if not self.pressed_actions:
|
if self.pressed_actions:
|
||||||
return
|
self.pressed_actions[-1]()
|
||||||
|
if not self.auto_repeat:
|
||||||
self.pressed_actions[-1]()
|
self.auto_repeat = True
|
||||||
self.timer.postpone(self.repeat_action, self.AUTOREPEAT_PERIOD)
|
self.restart(self.repeat_action, self.AUTOREPEAT_PERIOD)
|
||||||
|
else:
|
||||||
|
self.auto_repeat = False
|
||||||
|
self.stop(self.repeat_action)
|
||||||
|
|
||||||
def remove_action(self, action):
|
def remove_action(self, action):
|
||||||
if action in self.autorepeatable_actions:
|
if action in self.autorepeatable_actions:
|
||||||
@ -515,3 +479,13 @@ High score is set to 0"""
|
|||||||
crypted_high_score = self.stats.high_score ^ CRYPT_KEY
|
crypted_high_score = self.stats.high_score ^ CRYPT_KEY
|
||||||
crypted_high_score = pickle.dumps(crypted_high_score)
|
crypted_high_score = pickle.dumps(crypted_high_score)
|
||||||
return crypted_high_score
|
return crypted_high_score
|
||||||
|
|
||||||
|
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)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from .utils import Coord, Spin, Color
|
from .utils import Coord, Rotation, Color
|
||||||
|
|
||||||
|
|
||||||
class Mino:
|
class Mino:
|
||||||
@ -32,13 +32,13 @@ class TetrominoBase(list):
|
|||||||
|
|
||||||
# Super rotation system
|
# Super rotation system
|
||||||
SRS = {
|
SRS = {
|
||||||
Spin.CLOCKWISE: (
|
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)),
|
||||||
(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)),
|
||||||
),
|
),
|
||||||
Spin.COUNTER: (
|
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)),
|
||||||
(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)),
|
||||||
@ -49,9 +49,9 @@ class TetrominoBase(list):
|
|||||||
def __init__(self):
|
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.orientation = 0
|
||||||
self.rotated_last = False
|
self.last_rotation_point = None
|
||||||
self.rotation_point_5_used = False
|
|
||||||
self.hold_enabled = True
|
self.hold_enabled = True
|
||||||
|
self.prelocked = False
|
||||||
|
|
||||||
def ghost(self):
|
def ghost(self):
|
||||||
return type(self)()
|
return type(self)()
|
||||||
@ -60,8 +60,8 @@ class TetrominoBase(list):
|
|||||||
class O_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
class O_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||||
|
|
||||||
SRS = {
|
SRS = {
|
||||||
Spin.CLOCKWISE: (tuple(), tuple(), tuple(), tuple()),
|
Rotation.CLOCKWISE: (tuple(), tuple(), tuple(), tuple()),
|
||||||
Spin.COUNTER: (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_COORDS = (Coord(0, 0), Coord(1, 0), Coord(0, 1), Coord(1, 1))
|
||||||
MINOES_COLOR = Color.YELLOW
|
MINOES_COLOR = Color.YELLOW
|
||||||
@ -73,13 +73,13 @@ class O_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
|||||||
class I_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
class I_Tetrimino(TetrominoBase, metaclass=MetaTetromino):
|
||||||
|
|
||||||
SRS = {
|
SRS = {
|
||||||
Spin.CLOCKWISE: (
|
Rotation.CLOCKWISE: (
|
||||||
(Coord(1, 0), Coord(-1, 0), Coord(2, 0), 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(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(-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(0, -1), Coord(1, 1), Coord(-2, 1), Coord(1, -1), Coord(-2, 2)),
|
||||||
),
|
),
|
||||||
Spin.COUNTER: (
|
Rotation.COUNTER: (
|
||||||
(Coord(0, -1), Coord(-1, -1), Coord(2, -1), 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(-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(0, 1), Coord(1, 1), Coord(-2, 1), Coord(1, -1), Coord(-2, 2)),
|
||||||
|
@ -7,9 +7,6 @@ class Coord:
|
|||||||
def __add__(self, other):
|
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:
|
class Movement:
|
||||||
|
|
||||||
@ -18,7 +15,7 @@ class Movement:
|
|||||||
DOWN = Coord(0, -1)
|
DOWN = Coord(0, -1)
|
||||||
|
|
||||||
|
|
||||||
class Spin:
|
class Rotation:
|
||||||
|
|
||||||
CLOCKWISE = 1
|
CLOCKWISE = 1
|
||||||
COUNTER = -1
|
COUNTER = -1
|
||||||
@ -31,14 +28,6 @@ class T_Spin:
|
|||||||
T_SPIN = "T-SPIN"
|
T_SPIN = "T-SPIN"
|
||||||
|
|
||||||
|
|
||||||
class T_Slot:
|
|
||||||
|
|
||||||
A = 0
|
|
||||||
B = 1
|
|
||||||
C = 3
|
|
||||||
D = 2
|
|
||||||
|
|
||||||
|
|
||||||
class Color:
|
class Color:
|
||||||
|
|
||||||
BLUE = 0
|
BLUE = 0
|
||||||
@ -48,3 +37,14 @@ class Color:
|
|||||||
ORANGE = 4
|
ORANGE = 4
|
||||||
RED = 5
|
RED = 5
|
||||||
YELLOW = 6
|
YELLOW = 6
|
||||||
|
|
||||||
|
|
||||||
|
class Phase:
|
||||||
|
|
||||||
|
STARTING = "STARTING"
|
||||||
|
GENERATION = "GENERATION"
|
||||||
|
FALLING = "FALLING"
|
||||||
|
LOCK = "LOCK"
|
||||||
|
PATTERN = "PATTERN"
|
||||||
|
PAUSED = "PAUSED"
|
||||||
|
OVER = "OVER"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user