Compare commits
4 Commits
b8e20199af
...
02e4aa066d
Author | SHA1 | Date | |
---|---|---|---|
|
02e4aa066d | ||
|
5b6cfd6512 | ||
|
f1ffb1a7c6 | ||
|
dda475a584 |
@ -26,3 +26,10 @@ python tetrarcade.py
|
|||||||
* Linux: Edit `~/.local/share/Tetrarcade/TetrArcade.ini`
|
* Linux: Edit `~/.local/share/Tetrarcade/TetrArcade.ini`
|
||||||
|
|
||||||
Use key name from [arcade.key package](http://arcade.academy/arcade.key.html).
|
Use key name from [arcade.key package](http://arcade.academy/arcade.key.html).
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```shell
|
||||||
|
python -m pip install cx-freeze
|
||||||
|
python setup.py build
|
||||||
|
```
|
214
TetrArcade.py
@ -3,6 +3,7 @@ import sys
|
|||||||
import locale
|
import locale
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import configparser
|
import configparser
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -38,32 +39,39 @@ NORMAL_ALPHA = 200
|
|||||||
PRELOCKED_ALPHA = 100
|
PRELOCKED_ALPHA = 100
|
||||||
GHOST_ALPHA = 30
|
GHOST_ALPHA = 30
|
||||||
MATRIX_BG_ALPHA = 100
|
MATRIX_BG_ALPHA = 100
|
||||||
|
BAR_ALPHA = 75
|
||||||
|
|
||||||
# Sprites
|
# Sprites
|
||||||
WINDOW_BG_PATH = "images/bg.jpg"
|
WINDOW_BG_PATH = "res/bg.jpg"
|
||||||
MATRIX_SPRITE_PATH = "images/matrix.png"
|
MATRIX_BG_PATH = "res/matrix.png"
|
||||||
|
HELD_BG_PATH = "res/held.png"
|
||||||
|
NEXT_BG_PATH = "res/next.png"
|
||||||
MINOES_SPRITES_PATHS = {
|
MINOES_SPRITES_PATHS = {
|
||||||
"orange": "images/orange_mino.png",
|
"orange": "res/orange_mino.png",
|
||||||
"blue": "images/blue_mino.png",
|
"blue": "res/blue_mino.png",
|
||||||
"yellow": "images/yellow_mino.png",
|
"yellow": "res/yellow_mino.png",
|
||||||
"cyan": "images/cyan_mino.png",
|
"cyan": "res/cyan_mino.png",
|
||||||
"green": "images/green_mino.png",
|
"green": "res/green_mino.png",
|
||||||
"red": "images/red_mino.png",
|
"red": "res/red_mino.png",
|
||||||
"magenta": "images/magenta_mino.png"
|
"magenta": "res/magenta_mino.png",
|
||||||
}
|
}
|
||||||
|
|
||||||
# User profile path
|
# User profile path
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
USER_PROFILE_DIR = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
|
USER_PROFILE_DIR = os.environ.get(
|
||||||
|
"appdata", os.path.expanduser("~\Appdata\Roaming")
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
USER_PROFILE_DIR = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
|
USER_PROFILE_DIR = os.environ.get(
|
||||||
|
"XDG_DATA_HOME", os.path.expanduser("~/.local/share")
|
||||||
|
)
|
||||||
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, "TetrArcade.ini")
|
CONF_PATH = os.path.join(USER_PROFILE_DIR, "TetrArcade.ini")
|
||||||
|
|
||||||
# Text
|
# Text
|
||||||
TEXT_COLOR = arcade.color.BUBBLES
|
TEXT_COLOR = arcade.color.BUBBLES
|
||||||
FONT_NAME = "joystix monospace.ttf"
|
FONT_NAME = "res/joystix monospace.ttf"
|
||||||
STATS_TEXT_MARGIN = 40
|
STATS_TEXT_MARGIN = 40
|
||||||
STATS_TEXT_SIZE = 14
|
STATS_TEXT_SIZE = 14
|
||||||
STATS_TEXT_WIDTH = 150
|
STATS_TEXT_WIDTH = 150
|
||||||
@ -72,7 +80,6 @@ HIGHLIGHT_TEXT_SIZE = 20
|
|||||||
|
|
||||||
|
|
||||||
class MinoSprite(arcade.Sprite):
|
class MinoSprite(arcade.Sprite):
|
||||||
|
|
||||||
def __init__(self, mino, window, alpha):
|
def __init__(self, mino, window, alpha):
|
||||||
super().__init__(MINOES_SPRITES_PATHS[mino.color], window.scale)
|
super().__init__(MINOES_SPRITES_PATHS[mino.color], window.scale)
|
||||||
self.alpha = alpha
|
self.alpha = alpha
|
||||||
@ -80,12 +87,11 @@ class MinoSprite(arcade.Sprite):
|
|||||||
|
|
||||||
def set_position(self, x, y):
|
def set_position(self, x, y):
|
||||||
size = MINO_SIZE * self.scale
|
size = MINO_SIZE * self.scale
|
||||||
self.left = self.window.matrix_bg.left + x*size
|
self.left = self.window.matrix_bg.left + x * size
|
||||||
self.bottom = self.window.matrix_bg.bottom + y*size
|
self.bottom = self.window.matrix_bg.bottom + y * size
|
||||||
|
|
||||||
|
|
||||||
class MinoesSprites(arcade.SpriteList):
|
class MinoesSprites(arcade.SpriteList):
|
||||||
|
|
||||||
def resize(self, scale):
|
def resize(self, scale):
|
||||||
for sprite in self:
|
for sprite in self:
|
||||||
sprite.scale = scale
|
sprite.scale = scale
|
||||||
@ -93,7 +99,6 @@ class MinoesSprites(arcade.SpriteList):
|
|||||||
|
|
||||||
|
|
||||||
class TetrominoSprites(MinoesSprites):
|
class TetrominoSprites(MinoesSprites):
|
||||||
|
|
||||||
def __init__(self, tetromino, window, alpha=NORMAL_ALPHA):
|
def __init__(self, tetromino, window, alpha=NORMAL_ALPHA):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.tetromino = tetromino
|
self.tetromino = tetromino
|
||||||
@ -114,7 +119,6 @@ class TetrominoSprites(MinoesSprites):
|
|||||||
|
|
||||||
|
|
||||||
class MatrixSprites(MinoesSprites):
|
class MatrixSprites(MinoesSprites):
|
||||||
|
|
||||||
def __init__(self, matrix):
|
def __init__(self, matrix):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.matrix = matrix
|
self.matrix = matrix
|
||||||
@ -129,9 +133,8 @@ class MatrixSprites(MinoesSprites):
|
|||||||
|
|
||||||
|
|
||||||
class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
||||||
|
|
||||||
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.tasks = {}
|
||||||
|
|
||||||
@ -149,22 +152,23 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
arcade.Window.__init__(
|
arcade.Window.__init__(
|
||||||
self,
|
self,
|
||||||
width = self.init_width,
|
width=self.init_width,
|
||||||
height = self.init_height,
|
height=self.init_height,
|
||||||
title = WINDOW_TITLE,
|
title=WINDOW_TITLE,
|
||||||
resizable = True,
|
resizable=True,
|
||||||
antialiasing = False,
|
antialiasing=False,
|
||||||
fullscreen = self.init_fullscreen
|
fullscreen=self.init_fullscreen,
|
||||||
)
|
)
|
||||||
|
|
||||||
arcade.set_background_color(BG_COLOR)
|
arcade.set_background_color(BG_COLOR)
|
||||||
self.set_minimum_size(WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT)
|
self.set_minimum_size(WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT)
|
||||||
self.bg = arcade.Sprite(WINDOW_BG_PATH)
|
self.bg = arcade.Sprite(WINDOW_BG_PATH)
|
||||||
self.matrix_bg = arcade.Sprite(MATRIX_SPRITE_PATH)
|
self.matrix_bg = arcade.Sprite(MATRIX_BG_PATH)
|
||||||
self.matrix_bg.alpha = MATRIX_BG_ALPHA
|
self.matrix_bg.alpha = MATRIX_BG_ALPHA
|
||||||
self.held_bg = arcade.Sprite("images/held.png")
|
self.held_bg = arcade.Sprite(HELD_BG_PATH)
|
||||||
self.held_bg.alpha = MATRIX_BG_ALPHA
|
self.held_bg.alpha = BAR_ALPHA
|
||||||
self.next_bg = arcade.Sprite("images/next.png")
|
self.next_bg = arcade.Sprite(NEXT_BG_PATH)
|
||||||
self.next_bg.alpha = MATRIX_BG_ALPHA
|
self.next_bg.alpha = BAR_ALPHA
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
self.on_resize(self.init_width, self.init_height)
|
self.on_resize(self.init_width, self.init_height)
|
||||||
|
|
||||||
@ -172,7 +176,7 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
|||||||
self.conf["WINDOW"] = {
|
self.conf["WINDOW"] = {
|
||||||
"width": WINDOW_WIDTH,
|
"width": WINDOW_WIDTH,
|
||||||
"height": WINDOW_HEIGHT,
|
"height": WINDOW_HEIGHT,
|
||||||
"fullscreen": False
|
"fullscreen": False,
|
||||||
}
|
}
|
||||||
self.conf["KEYBOARD"] = {
|
self.conf["KEYBOARD"] = {
|
||||||
"start": "ENTER",
|
"start": "ENTER",
|
||||||
@ -184,12 +188,12 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
|||||||
"rotate counter": "Z",
|
"rotate counter": "Z",
|
||||||
"hold": "C",
|
"hold": "C",
|
||||||
"pause": "ESCAPE",
|
"pause": "ESCAPE",
|
||||||
"fullscreen": "F11"
|
"fullscreen": "F11",
|
||||||
}
|
}
|
||||||
self.load_conf()
|
self.load_conf()
|
||||||
if not os.path.exists(USER_PROFILE_DIR):
|
if not os.path.exists(USER_PROFILE_DIR):
|
||||||
os.makedirs(USER_PROFILE_DIR)
|
os.makedirs(USER_PROFILE_DIR)
|
||||||
with open(CONF_PATH, 'w') as f:
|
with open(CONF_PATH, "w") as f:
|
||||||
self.conf.write(f)
|
self.conf.write(f)
|
||||||
|
|
||||||
def load_conf(self):
|
def load_conf(self):
|
||||||
@ -200,38 +204,61 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
|||||||
self.key_map = {
|
self.key_map = {
|
||||||
tetrislogic.State.STARTING: {
|
tetrislogic.State.STARTING: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["fullscreen"]): self.toggle_fullscreen
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
|
): self.toggle_fullscreen,
|
||||||
},
|
},
|
||||||
tetrislogic.State.PLAYING: {
|
tetrislogic.State.PLAYING: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["move left"]): self.move_left,
|
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"]["move right"]
|
||||||
|
): self.move_right,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["soft drop"]): self.soft_drop,
|
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"]["hard drop"]): self.hard_drop,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["rotate clockwise"]): self.rotate_clockwise,
|
getattr(
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["rotate counter"]): self.rotate_counter,
|
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.swap,
|
getattr(arcade.key, self.conf["KEYBOARD"]["hold"]): self.swap,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.pause,
|
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.pause,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["fullscreen"]): self.toggle_fullscreen
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
|
): self.toggle_fullscreen,
|
||||||
},
|
},
|
||||||
tetrislogic.State.PAUSED: {
|
tetrislogic.State.PAUSED: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.resume,
|
getattr(arcade.key, self.conf["KEYBOARD"]["pause"]): self.resume,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["fullscreen"]): self.toggle_fullscreen
|
getattr(
|
||||||
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
|
): self.toggle_fullscreen,
|
||||||
},
|
},
|
||||||
tetrislogic.State.OVER: {
|
tetrislogic.State.OVER: {
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
getattr(arcade.key, self.conf["KEYBOARD"]["start"]): self.new_game,
|
||||||
getattr(arcade.key, self.conf["KEYBOARD"]["fullscreen"]): self.toggle_fullscreen
|
getattr(
|
||||||
}
|
arcade.key, self.conf["KEYBOARD"]["fullscreen"]
|
||||||
|
): self.toggle_fullscreen,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
controls_text = "\n\n\nCONTROLS\n\n" + "\n".join(
|
controls_text = (
|
||||||
"{:<16s}{:>6s}".format(key, action)
|
"\n\n\nCONTROLS\n\n"
|
||||||
for key, action in tuple(self.conf["KEYBOARD"].items()) + (("QUIT", "ALT+F4"),)
|
+ "\n".join(
|
||||||
) + "\n\n\n"
|
"{:<16s}{:>6s}".format(key, action)
|
||||||
self.start_text = "TETRARCADE" + controls_text + "PRESS [{}] TO START".format(
|
for key, action in tuple(self.conf["KEYBOARD"].items())
|
||||||
self.conf["KEYBOARD"]["start"]
|
+ (("QUIT", "ALT+F4"),)
|
||||||
|
)
|
||||||
|
+ "\n\n\n"
|
||||||
)
|
)
|
||||||
self.pause_text = "PAUSE" + controls_text + "PRESS [{}] TO RESUME".format(
|
self.start_text = (
|
||||||
self.conf["KEYBOARD"]["pause"]
|
"TETRARCADE"
|
||||||
|
+ controls_text
|
||||||
|
+ "PRESS [{}] TO START".format(self.conf["KEYBOARD"]["start"])
|
||||||
|
)
|
||||||
|
self.pause_text = (
|
||||||
|
"PAUSE"
|
||||||
|
+ controls_text
|
||||||
|
+ "PRESS [{}] TO RESUME".format(self.conf["KEYBOARD"]["pause"])
|
||||||
)
|
)
|
||||||
self.game_over_text = """GAME
|
self.game_over_text = """GAME
|
||||||
OVER
|
OVER
|
||||||
@ -330,65 +357,59 @@ AGAIN""".format(
|
|||||||
t = time.localtime(self.time)
|
t = time.localtime(self.time)
|
||||||
font_size = STATS_TEXT_SIZE * self.scale
|
font_size = STATS_TEXT_SIZE * self.scale
|
||||||
for y, text in enumerate(
|
for y, text in enumerate(
|
||||||
(
|
("TIME", "LINES", "GOAL", "LEVEL", "HIGH SCORE", "SCORE")
|
||||||
"TIME",
|
|
||||||
"LINES",
|
|
||||||
"GOAL",
|
|
||||||
"LEVEL",
|
|
||||||
"HIGH SCORE",
|
|
||||||
"SCORE"
|
|
||||||
)
|
|
||||||
):
|
):
|
||||||
arcade.draw_text(
|
arcade.draw_text(
|
||||||
text = text,
|
text=text,
|
||||||
start_x = self.matrix_bg.left - self.scale*(STATS_TEXT_MARGIN + STATS_TEXT_WIDTH),
|
start_x=self.matrix_bg.left
|
||||||
start_y = self.matrix_bg.bottom + 1.5*(2*y+1)*font_size,
|
- self.scale * (STATS_TEXT_MARGIN + STATS_TEXT_WIDTH),
|
||||||
color = TEXT_COLOR,
|
start_y=self.matrix_bg.bottom + 1.5 * (2 * y + 1) * font_size,
|
||||||
font_size = font_size,
|
color=TEXT_COLOR,
|
||||||
align = 'right',
|
font_size=font_size,
|
||||||
font_name = FONT_NAME,
|
align="right",
|
||||||
anchor_x = 'left'
|
font_name=FONT_NAME,
|
||||||
|
anchor_x="left",
|
||||||
)
|
)
|
||||||
for y, text in enumerate(
|
for y, text in enumerate(
|
||||||
(
|
(
|
||||||
"{:02d}:{:02d}:{:02d}".format(
|
"{:02d}:{:02d}:{:02d}".format(t.tm_hour - 1, t.tm_min, t.tm_sec),
|
||||||
t.tm_hour-1, t.tm_min, t.tm_sec
|
|
||||||
),
|
|
||||||
"{:n}".format(self.nb_lines_cleared),
|
"{:n}".format(self.nb_lines_cleared),
|
||||||
"{:n}".format(self.goal),
|
"{:n}".format(self.goal),
|
||||||
"{:n}".format(self.level),
|
"{:n}".format(self.level),
|
||||||
"{:n}".format(self.high_score),
|
"{:n}".format(self.high_score),
|
||||||
"{:n}".format(self.score)
|
"{:n}".format(self.score),
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
arcade.draw_text(
|
arcade.draw_text(
|
||||||
text = text,
|
text=text,
|
||||||
start_x = self.matrix_bg.left - STATS_TEXT_MARGIN*self.scale,
|
start_x=self.matrix_bg.left - STATS_TEXT_MARGIN * self.scale,
|
||||||
start_y = self.matrix_bg.bottom + 3*y*font_size,
|
start_y=self.matrix_bg.bottom + 3 * y * font_size,
|
||||||
color = TEXT_COLOR,
|
color=TEXT_COLOR,
|
||||||
font_size = font_size,
|
font_size=font_size,
|
||||||
align = 'right',
|
align="right",
|
||||||
font_name = FONT_NAME,
|
font_name=FONT_NAME,
|
||||||
anchor_x = 'right'
|
anchor_x="right",
|
||||||
)
|
)
|
||||||
|
|
||||||
highlight_text = {
|
highlight_text = {
|
||||||
tetrislogic.State.STARTING: self.start_text,
|
tetrislogic.State.STARTING: self.start_text,
|
||||||
tetrislogic.State.PLAYING: self.highlight_texts[0] if self.highlight_texts else "",
|
tetrislogic.State.PLAYING: self.highlight_texts[0]
|
||||||
|
if self.highlight_texts
|
||||||
|
else "",
|
||||||
tetrislogic.State.PAUSED: self.pause_text,
|
tetrislogic.State.PAUSED: self.pause_text,
|
||||||
tetrislogic.State.OVER: self.game_over_text
|
tetrislogic.State.OVER: self.game_over_text,
|
||||||
}.get(self.state, "")
|
}.get(self.state, "")
|
||||||
if highlight_text:
|
if highlight_text:
|
||||||
arcade.draw_text(
|
arcade.draw_text(
|
||||||
text = highlight_text,
|
text=highlight_text,
|
||||||
start_x = self.matrix_bg.center_x,
|
start_x=self.matrix_bg.center_x,
|
||||||
start_y = self.matrix_bg.center_y,
|
start_y=self.matrix_bg.center_y,
|
||||||
color = HIGHLIGHT_TEXT_COLOR,
|
color=HIGHLIGHT_TEXT_COLOR,
|
||||||
font_size = HIGHLIGHT_TEXT_SIZE * self.scale,
|
font_size=HIGHLIGHT_TEXT_SIZE * self.scale,
|
||||||
align = 'center',
|
align="center",
|
||||||
font_name = FONT_NAME,
|
font_name=FONT_NAME,
|
||||||
anchor_x = 'center',
|
anchor_x="center",
|
||||||
anchor_y = 'center'
|
anchor_y="center",
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_hide(self):
|
def on_hide(self):
|
||||||
@ -401,9 +422,9 @@ AGAIN""".format(
|
|||||||
super().on_resize(width, height)
|
super().on_resize(width, height)
|
||||||
center_x = width / 2
|
center_x = width / 2
|
||||||
center_y = height / 2
|
center_y = height / 2
|
||||||
self.scale = min(width/WINDOW_WIDTH, height/WINDOW_HEIGHT)
|
self.scale = min(width / WINDOW_WIDTH, height / WINDOW_HEIGHT)
|
||||||
|
|
||||||
self.bg.scale = max(width/WINDOW_WIDTH, height/WINDOW_HEIGHT)
|
self.bg.scale = max(width / WINDOW_WIDTH, height / WINDOW_HEIGHT)
|
||||||
self.bg.center_x = center_x
|
self.bg.center_x = center_x
|
||||||
self.bg.center_y = center_y
|
self.bg.center_y = center_y
|
||||||
|
|
||||||
@ -411,7 +432,7 @@ AGAIN""".format(
|
|||||||
self.matrix_bg.center_x = center_x
|
self.matrix_bg.center_x = center_x
|
||||||
self.matrix_bg.center_y = center_y
|
self.matrix_bg.center_y = center_y
|
||||||
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.held_bg.scale = self.scale
|
self.held_bg.scale = self.scale
|
||||||
self.held_bg.right = self.matrix_bg.left
|
self.held_bg.right = self.matrix_bg.left
|
||||||
@ -439,14 +460,17 @@ AGAIN""".format(
|
|||||||
try:
|
try:
|
||||||
if not os.path.exists(USER_PROFILE_DIR):
|
if not os.path.exists(USER_PROFILE_DIR):
|
||||||
os.makedirs(USER_PROFILE_DIR)
|
os.makedirs(USER_PROFILE_DIR)
|
||||||
with open(HIGH_SCORE_PATH, mode='wb') as f:
|
with open(HIGH_SCORE_PATH, mode="wb") as f:
|
||||||
crypted_high_score = super().save_high_score()
|
crypted_high_score = super().save_high_score()
|
||||||
f.write(crypted_high_score)
|
f.write(crypted_high_score)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"""High score: {:n}
|
"""High score: {:n}
|
||||||
High score could not be saved:
|
High score could not be saved:
|
||||||
""".format(self.high_score) + str(e)
|
""".format(
|
||||||
|
self.high_score
|
||||||
|
)
|
||||||
|
+ str(e)
|
||||||
)
|
)
|
||||||
|
|
||||||
def start(self, task, period):
|
def start(self, task, period):
|
||||||
@ -477,9 +501,11 @@ High score could not be saved:
|
|||||||
self.save_high_score()
|
self.save_high_score()
|
||||||
super().on_close()
|
super().on_close()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
TetrArcade()
|
TetrArcade()
|
||||||
arcade.run()
|
arcade.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
BIN
icon48.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 153 KiB After Width: | Height: | Size: 153 KiB |
Before Width: | Height: | Size: 165 B After Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 151 B After Width: | Height: | Size: 151 B |
Before Width: | Height: | Size: 167 B After Width: | Height: | Size: 167 B |
Before Width: | Height: | Size: 499 B After Width: | Height: | Size: 499 B |
Before Width: | Height: | Size: 165 B After Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 475 B After Width: | Height: | Size: 475 B |
Before Width: | Height: | Size: 165 B After Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 165 B After Width: | Height: | Size: 165 B |
Before Width: | Height: | Size: 143 B After Width: | Height: | Size: 143 B |
30
setup.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# -*- 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
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name="TetrArcade",
|
||||||
|
version="0.1",
|
||||||
|
description="Tetris clone",
|
||||||
|
author="adrienmalin",
|
||||||
|
executables=[Executable(
|
||||||
|
script="TetrArcade.py",
|
||||||
|
icon=icon,
|
||||||
|
base=base
|
||||||
|
)],
|
||||||
|
options={
|
||||||
|
"build_exe": {
|
||||||
|
"packages": ["arcade"],
|
||||||
|
"excludes": ["tkinter"],
|
||||||
|
"include_files": "res",
|
||||||
|
"silent": True
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
@ -10,14 +10,11 @@ NB_NEXT = 5
|
|||||||
# Delays (seconds)
|
# Delays (seconds)
|
||||||
LOCK_DELAY = 0.5
|
LOCK_DELAY = 0.5
|
||||||
FALL_DELAY = 1
|
FALL_DELAY = 1
|
||||||
AUTOREPEAT_DELAY = 0.200 # Official : 0.300
|
AUTOREPEAT_DELAY = 0.200 # Official : 0.300
|
||||||
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010
|
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010
|
||||||
|
|
||||||
|
|
||||||
# Piece init coord
|
# Piece init coord
|
||||||
CURRENT_COORD = Coord(4, NB_LINES)
|
CURRENT_COORD = Coord(4, NB_LINES)
|
||||||
NEXT_COORDS = [
|
NEXT_COORDS = [Coord(NB_COLS + 4, NB_LINES - 4 * n - 3) for n in range(NB_NEXT)]
|
||||||
Coord(NB_COLS+4, NB_LINES-4*n-3)
|
HELD_COORD = Coord(-5, NB_LINES - 3)
|
||||||
for n in range(NB_NEXT)
|
|
||||||
]
|
|
||||||
HELD_COORD = Coord(-5, NB_LINES-3)
|
|
||||||
|
@ -5,10 +5,16 @@ import pickle
|
|||||||
from .utils import Coord, Movement, Rotation, T_Spin, Line
|
from .utils import Coord, Movement, Rotation, T_Spin, Line
|
||||||
from .tetromino import Tetromino, T, I
|
from .tetromino import Tetromino, T, I
|
||||||
from .consts import (
|
from .consts import (
|
||||||
NB_LINES, NB_COLS, NB_NEXT,
|
NB_LINES,
|
||||||
LOCK_DELAY, FALL_DELAY,
|
NB_COLS,
|
||||||
AUTOREPEAT_DELAY, AUTOREPEAT_PERIOD,
|
NB_NEXT,
|
||||||
CURRENT_COORD, NEXT_COORDS, HELD_COORD
|
LOCK_DELAY,
|
||||||
|
FALL_DELAY,
|
||||||
|
AUTOREPEAT_DELAY,
|
||||||
|
AUTOREPEAT_PERIOD,
|
||||||
|
CURRENT_COORD,
|
||||||
|
NEXT_COORDS,
|
||||||
|
HELD_COORD,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -19,22 +25,17 @@ CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
|
|||||||
class State:
|
class State:
|
||||||
|
|
||||||
STARTING = "STARTING"
|
STARTING = "STARTING"
|
||||||
PLAYING = "PLAYING"
|
PLAYING = "PLAYING"
|
||||||
PAUSED = "PAUSED"
|
PAUSED = "PAUSED"
|
||||||
OVER = "OVER"
|
OVER = "OVER"
|
||||||
|
|
||||||
|
|
||||||
class Matrix(list):
|
class Matrix(list):
|
||||||
|
|
||||||
def cell_is_free(self, coord):
|
def cell_is_free(self, coord):
|
||||||
return (
|
return 0 <= coord.x < NB_COLS and 0 <= coord.y and not self[coord.y][coord.x]
|
||||||
0 <= coord.x < NB_COLS
|
|
||||||
and 0 <= coord.y
|
|
||||||
and not self[coord.y][coord.x]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TetrisLogic():
|
class TetrisLogic:
|
||||||
|
|
||||||
NB_LINES = NB_LINES
|
NB_LINES = NB_LINES
|
||||||
NB_COLS = NB_COLS
|
NB_COLS = NB_COLS
|
||||||
@ -85,7 +86,7 @@ class TetrisLogic():
|
|||||||
self.fall_delay = self.FALL_DELAY
|
self.fall_delay = self.FALL_DELAY
|
||||||
|
|
||||||
self.matrix.clear()
|
self.matrix.clear()
|
||||||
for y in range(self.NB_LINES+3):
|
for y in range(self.NB_LINES + 3):
|
||||||
self.append_new_line_to_matrix()
|
self.append_new_line_to_matrix()
|
||||||
self.next = [self.new_tetromino() for n in range(self.NB_NEXT)]
|
self.next = [self.new_tetromino() for n in range(self.NB_NEXT)]
|
||||||
self.held = None
|
self.held = None
|
||||||
@ -107,9 +108,9 @@ class TetrisLogic():
|
|||||||
self.level += 1
|
self.level += 1
|
||||||
self.goal += 5 * self.level
|
self.goal += 5 * self.level
|
||||||
if self.level <= 20:
|
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:
|
if self.level > 15:
|
||||||
self.lock_delay = 0.5 * pow(0.9, self.level-15)
|
self.lock_delay = 0.5 * pow(0.9, self.level - 15)
|
||||||
self.show_text("LEVEL\n{:n}".format(self.level))
|
self.show_text("LEVEL\n{:n}".format(self.level))
|
||||||
self.restart(self.fall, self.fall_delay)
|
self.restart(self.fall, self.fall_delay)
|
||||||
|
|
||||||
@ -122,13 +123,10 @@ class TetrisLogic():
|
|||||||
self.move_ghost()
|
self.move_ghost()
|
||||||
self.next.append(self.new_tetromino())
|
self.next.append(self.new_tetromino())
|
||||||
self.next[-1].coord = self.NEXT_COORDS[-1]
|
self.next[-1].coord = self.NEXT_COORDS[-1]
|
||||||
for tetromino, coord in zip (self.next, self.NEXT_COORDS):
|
for tetromino, coord in zip(self.next, self.NEXT_COORDS):
|
||||||
tetromino.coord = coord
|
tetromino.coord = coord
|
||||||
|
|
||||||
if not self.can_move(
|
if not self.can_move(self.current.coord, (mino.coord for mino in self.current)):
|
||||||
self.current.coord,
|
|
||||||
(mino.coord for mino in self.current)
|
|
||||||
):
|
|
||||||
self.game_over()
|
self.game_over()
|
||||||
|
|
||||||
def move_left(self):
|
def move_left(self):
|
||||||
@ -148,8 +146,7 @@ class TetrisLogic():
|
|||||||
for ghost_mino, current_mino in zip(self.ghost, self.current):
|
for ghost_mino, current_mino in zip(self.ghost, self.current):
|
||||||
ghost_mino.coord = current_mino.coord
|
ghost_mino.coord = current_mino.coord
|
||||||
while self.can_move(
|
while self.can_move(
|
||||||
self.ghost.coord + Movement.DOWN,
|
self.ghost.coord + Movement.DOWN, (mino.coord for mino in self.ghost)
|
||||||
(mino.coord for mino in self.ghost)
|
|
||||||
):
|
):
|
||||||
self.ghost.coord += Movement.DOWN
|
self.ghost.coord += Movement.DOWN
|
||||||
|
|
||||||
@ -169,10 +166,7 @@ class TetrisLogic():
|
|||||||
|
|
||||||
def move(self, movement, prelock=True):
|
def move(self, movement, prelock=True):
|
||||||
potential_coord = self.current.coord + movement
|
potential_coord = self.current.coord + movement
|
||||||
if self.can_move(
|
if self.can_move(potential_coord, (mino.coord for mino in self.current)):
|
||||||
potential_coord,
|
|
||||||
(mino.coord for mino in self.current)
|
|
||||||
):
|
|
||||||
if self.current.prelocked:
|
if self.current.prelocked:
|
||||||
self.restart(self.lock, self.lock_delay)
|
self.restart(self.lock, self.lock_delay)
|
||||||
self.current.coord = potential_coord
|
self.current.coord = potential_coord
|
||||||
@ -181,22 +175,18 @@ class TetrisLogic():
|
|||||||
self.move_ghost()
|
self.move_ghost()
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
if (
|
if prelock and not self.current.prelocked and movement == Movement.DOWN:
|
||||||
prelock and not self.current.prelocked
|
|
||||||
and movement == Movement.DOWN
|
|
||||||
):
|
|
||||||
self.current.prelocked = True
|
self.current.prelocked = True
|
||||||
self.start(self.lock, self.lock_delay)
|
self.start(self.lock, self.lock_delay)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def rotate(self, rotation):
|
def rotate(self, rotation):
|
||||||
rotated_coords = tuple(
|
rotated_coords = tuple(
|
||||||
Coord(rotation*mino.coord.y, -rotation*mino.coord.x)
|
Coord(rotation * mino.coord.y, -rotation * mino.coord.x)
|
||||||
for mino in self.current
|
for mino in self.current
|
||||||
)
|
)
|
||||||
for rotation_point, liberty_degree in enumerate(
|
for rotation_point, liberty_degree in enumerate(
|
||||||
self.current.SRS[rotation][self.current.orientation],
|
self.current.SRS[rotation][self.current.orientation], start=1
|
||||||
start = 1
|
|
||||||
):
|
):
|
||||||
potential_coord = self.current.coord + liberty_degree
|
potential_coord = self.current.coord + liberty_degree
|
||||||
if self.can_move(potential_coord, rotated_coords):
|
if self.can_move(potential_coord, rotated_coords):
|
||||||
@ -205,9 +195,7 @@ class TetrisLogic():
|
|||||||
self.current.coord = potential_coord
|
self.current.coord = potential_coord
|
||||||
for mino, coord in zip(self.current, rotated_coords):
|
for mino, coord in zip(self.current, rotated_coords):
|
||||||
mino.coord = coord
|
mino.coord = coord
|
||||||
self.current.orientation = (
|
self.current.orientation = (self.current.orientation + rotation) % 4
|
||||||
(self.current.orientation + rotation) % 4
|
|
||||||
)
|
|
||||||
self.current.last_rotation_point = rotation_point
|
self.current.last_rotation_point = rotation_point
|
||||||
self.move_ghost()
|
self.move_ghost()
|
||||||
return True
|
return True
|
||||||
@ -219,7 +207,7 @@ class TetrisLogic():
|
|||||||
{LINES_CLEAR_NAME: "SINGLE", T_Spin.NONE: 1, T_Spin.MINI: 2, T_Spin.T_SPIN: 8},
|
{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: "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: "TRIPLE", T_Spin.NONE: 5, T_Spin.T_SPIN: 16},
|
||||||
{LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8}
|
{LINES_CLEAR_NAME: "TETRIS", T_Spin.NONE: 8},
|
||||||
)
|
)
|
||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
@ -228,8 +216,7 @@ class TetrisLogic():
|
|||||||
|
|
||||||
# Piece unlocked
|
# Piece unlocked
|
||||||
if self.can_move(
|
if self.can_move(
|
||||||
self.current.coord + Movement.DOWN,
|
self.current.coord + Movement.DOWN, (mino.coord for mino in self.current)
|
||||||
(mino.coord for mino in self.current)
|
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -246,17 +233,12 @@ class TetrisLogic():
|
|||||||
self.restart(self.repeat_action, self.AUTOREPEAT_DELAY)
|
self.restart(self.repeat_action, self.AUTOREPEAT_DELAY)
|
||||||
|
|
||||||
# T-Spin
|
# T-Spin
|
||||||
if (
|
if type(self.current) == T and self.current.last_rotation_point is not None:
|
||||||
type(self.current) == T
|
|
||||||
and self.current.last_rotation_point is not None
|
|
||||||
):
|
|
||||||
a = self.is_t_slot(0)
|
a = self.is_t_slot(0)
|
||||||
b = self.is_t_slot(1)
|
b = self.is_t_slot(1)
|
||||||
c = self.is_t_slot(3)
|
c = self.is_t_slot(3)
|
||||||
d = self.is_t_slot(2)
|
d = self.is_t_slot(2)
|
||||||
if self.current.last_rotation_point == 5 or (
|
if self.current.last_rotation_point == 5 or (a and b and (c or d)):
|
||||||
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):
|
||||||
t_spin = T_Spin.MINI
|
t_spin = T_Spin.MINI
|
||||||
@ -267,8 +249,8 @@ class TetrisLogic():
|
|||||||
|
|
||||||
for mino in self.current:
|
for mino in self.current:
|
||||||
coord = mino.coord + self.current.coord
|
coord = mino.coord + self.current.coord
|
||||||
del(mino.coord)
|
del mino.coord
|
||||||
if coord.y <= self.NB_LINES+3:
|
if coord.y <= self.NB_LINES + 3:
|
||||||
self.matrix[coord.y][coord.x] = mino
|
self.matrix[coord.y][coord.x] = mino
|
||||||
|
|
||||||
# Clear complete lines
|
# Clear complete lines
|
||||||
@ -302,7 +284,7 @@ class TetrisLogic():
|
|||||||
self.show_text("\n".join(lock_strings))
|
self.show_text("\n".join(lock_strings))
|
||||||
|
|
||||||
if self.combo >= 1:
|
if self.combo >= 1:
|
||||||
ds = (20 if nb_lines_cleared==1 else 50) * self.combo * self.level
|
ds = (20 if nb_lines_cleared == 1 else 50) * self.combo * self.level
|
||||||
lock_score += ds
|
lock_score += ds
|
||||||
self.show_text("COMBO x{:n}\n{:n}".format(self.combo, ds))
|
self.show_text("COMBO x{:n}\n{:n}".format(self.combo, ds))
|
||||||
|
|
||||||
@ -315,21 +297,16 @@ class TetrisLogic():
|
|||||||
|
|
||||||
def can_move(self, potential_coord, minoes_coords):
|
def can_move(self, potential_coord, minoes_coords):
|
||||||
return all(
|
return all(
|
||||||
self.matrix.cell_is_free(potential_coord+mino_coord)
|
self.matrix.cell_is_free(potential_coord + mino_coord)
|
||||||
for mino_coord in minoes_coords
|
for mino_coord in minoes_coords
|
||||||
)
|
)
|
||||||
|
|
||||||
T_SLOT_COORDS = (
|
T_SLOT_COORDS = (Coord(-1, 1), Coord(1, 1), Coord(-1, 1), Coord(-1, -1))
|
||||||
Coord(-1, 1),
|
|
||||||
Coord( 1, 1),
|
|
||||||
Coord(-1, 1),
|
|
||||||
Coord(-1, -1)
|
|
||||||
)
|
|
||||||
|
|
||||||
def is_t_slot(self, n):
|
def is_t_slot(self, n):
|
||||||
t_slot_coord = self.current.coord + self.T_SLOT_COORDS[
|
t_slot_coord = (
|
||||||
(self.current.orientation + n) % 4
|
self.current.coord + self.T_SLOT_COORDS[(self.current.orientation + n) % 4]
|
||||||
]
|
)
|
||||||
return not self.matrix.cell_is_free(t_slot_coord)
|
return not self.matrix.cell_is_free(t_slot_coord)
|
||||||
|
|
||||||
def swap(self):
|
def swap(self):
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from .utils import Coord, Rotation
|
from .utils import Coord, Rotation
|
||||||
|
|
||||||
class Mino:
|
|
||||||
|
|
||||||
|
class Mino:
|
||||||
def __init__(self, color, coord):
|
def __init__(self, color, coord):
|
||||||
self.color = color
|
self.color = color
|
||||||
self.coord = coord
|
self.coord = coord
|
||||||
|
|
||||||
|
|
||||||
class MetaTetromino(type):
|
class MetaTetromino(type):
|
||||||
|
|
||||||
def __init__(cls, name, bases, dct):
|
def __init__(cls, name, bases, dct):
|
||||||
super().__init__(name, bases, dct)
|
super().__init__(name, bases, dct)
|
||||||
Tetromino.shapes.append(cls)
|
Tetromino.shapes.append(cls)
|
||||||
@ -35,10 +34,7 @@ class Tetromino(list):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(
|
super().__init__(Mino(self.MINOES_COLOR, coord) for coord in self.MINOES_COORDS)
|
||||||
Mino(self.MINOES_COLOR, coord)
|
|
||||||
for coord in self.MINOES_COORDS
|
|
||||||
)
|
|
||||||
self.orientation = 0
|
self.orientation = 0
|
||||||
self.last_rotation_point = None
|
self.last_rotation_point = None
|
||||||
self.hold_enabled = True
|
self.hold_enabled = True
|
||||||
@ -47,6 +43,7 @@ class Tetromino(list):
|
|||||||
def ghost(self):
|
def ghost(self):
|
||||||
return type(self)()
|
return type(self)()
|
||||||
|
|
||||||
|
|
||||||
class O(Tetromino, metaclass=MetaTetromino):
|
class O(Tetromino, metaclass=MetaTetromino):
|
||||||
|
|
||||||
SRS = {
|
SRS = {
|
||||||
|
@ -1,34 +1,32 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
class Coord:
|
class Coord:
|
||||||
|
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
return Coord(self.x+other.x, self.y+other.y)
|
return Coord(self.x + other.x, self.y + other.y)
|
||||||
|
|
||||||
|
|
||||||
class Movement:
|
class Movement:
|
||||||
|
|
||||||
LEFT = Coord(-1, 0)
|
LEFT = Coord(-1, 0)
|
||||||
RIGHT = Coord( 1, 0)
|
RIGHT = Coord(1, 0)
|
||||||
DOWN = Coord( 0, -1)
|
DOWN = Coord(0, -1)
|
||||||
|
|
||||||
|
|
||||||
class Rotation:
|
class Rotation:
|
||||||
|
|
||||||
CLOCKWISE = 1
|
CLOCKWISE = 1
|
||||||
COUNTER = -1
|
COUNTER = -1
|
||||||
|
|
||||||
|
|
||||||
class T_Spin:
|
class T_Spin:
|
||||||
|
|
||||||
NONE = ""
|
NONE = ""
|
||||||
MINI = "MINI\nT-SPIN"
|
MINI = "MINI\nT-SPIN"
|
||||||
T_SPIN = "T-SPIN"
|
T_SPIN = "T-SPIN"
|
||||||
|
|
||||||
|
|
||||||
class Line(list):
|
class Line(list):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|