Compare commits

..

8 Commits

Author SHA1 Message Date
adrienmalin
56ea9bc63a fixes 2019-02-25 22:36:29 +01:00
adrienmalin
1e1d88ad69 hell 2019-02-25 20:42:47 +01:00
adrienmalin
0073f87bee merge from master 2019-02-25 17:36:44 +01:00
adrienmalin
bab1dff379 forgot to commit before week-end 2019-02-25 09:50:41 +01:00
adrienmalin
04761e19bb Update terminis.py 2019-02-22 16:35:06 +01:00
adrienmalin
3fb83acb31 Update core.py 2019-02-22 16:17:00 +01:00
adrienmalin
3cb7a4feb6 Update core.py 2019-02-22 15:39:06 +01:00
adrienmalin
31690ce04f Split game logic from GUI 2019-02-22 11:18:58 +01:00
6 changed files with 541 additions and 504 deletions

View File

@ -1,14 +1,7 @@
# Terminis # Terminis
Tetris clone for terminal. Ideal for servers without GUI! Tetris clone for terminal. Ideal for servers without GUI!
## Requirements ## Install
[Python 2 or 3](https://www.python.org/)
beep command to play music with internal speaker
## Installation
```bash ```bash
pip install --user terminis pip install --user terminis
@ -19,8 +12,7 @@ pip install --user terminis
```bash ```bash
terminis [options] terminis [options]
``` ```
* --help: show command usage (this message)
- --help -h: show command usage (this message) * --edit: edit controls in text editor
- --edit -e: edit controls in text editor * --reset: reset to default controls settings
- --reset -r: reset to default controls settings * --level=n: start at level n (integer between 1 and 15)
- --level=n: start at level n (integer between 1 and 15)

View File

@ -1,11 +1,11 @@
[tool.poetry] [tool.poetry]
name = "terminis" name = "terminis"
version = "0.2.4.2" version = "0.2.2"
description = "Tetris clone for terminal. Ideal for servers without GUI!" description = "Tetris clone for terminal. Ideal for servers without GUI!"
authors = ["adrienmalin <41926238+adrienmalin@users.noreply.github.com>"] authors = ["adrienmalin <41926238+adrienmalin@users.noreply.github.com>"]
license = "MIT" license = "MIT"
repository = "https://github.com/adrienmalin/Terminis" repository = "https://github.com/adrienmalin/Terminis"
keywords = ["Tetris", "terminal", "curses","beep"] keywords = ["Tetris", "terminal", "curses"]
classifiers = [ classifiers = [
"Environment :: Console :: Curses", "Environment :: Console :: Curses",
"Programming Language :: Python", "Programming Language :: Python",
@ -16,7 +16,6 @@ classifiers = [
"Topic :: System :: Systems Administration" "Topic :: System :: Systems Administration"
] ]
readme = "README.md" readme = "README.md"
include = ["music.sh"]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = ">=2.7" python = ">=2.7"

View File

@ -1,36 +0,0 @@
#!/bin/sh
C0=-f16; Db0=-f17; D0=-f18; Eb0=-f19; E0=-f20; F0=-f21; Gb0=-f23; G0=-f24; Ab0=-f25; A0=-f27; Bb0=-f29; B0=-f30
C1=-f32; Db1=-f34; D1=-f36; Eb1=-f38; E1=-f41; F1=-f43; Gb1=-f46; G1=-f49; Ab1=-f51; A1=-f55; Bb1=-f58; B1=-f61
C2=-f65; Db2=-f69; D2=-f73; Eb2=-f77; E2=-f82; F2=-f87; Gb2=-f92; G2=-f98; Ab2=-f103; A2=-f110; Bb2=-f116; B2=-f123
C3=-f130; Db3=-f138; D3=-f146; Eb3=-f155; E3=-f164; F3=-f174; Gb3=-f185; G3=-f196; Ab3=-f207; A3=-f220; Bb3=-f233; B3=-f246
C4=-f261; Db4=-f277; D4=-f293; Eb4=-f311; E4=-f329; F4=-f349; Gb4=-f369; G4=-f392; Ab4=-f415; A4=-f440; Bb4=-f466; B4=-f493
C5=-f523; Db5=-f554; D5=-f587; Eb5=-f622; E5=-f659; F5=-f698; Gb5=-f739; G5=-f783; Ab5=-f830; A5=-f880; Bb5=-f932; B5=-f987
C6=-f1046; Db6=-f1108; D6=-f1174; Eb6=-f1244; E6=-f1318; F6=-f1396; Gb6=-f1479; G6=-f1567; Ab6=-f1661; A6=-f1760; Bb6=-f1864; B6=-f1975
C7=-f2093; Db7=-f2217; D7=-f2349; Eb7=-f2489; E7=-f2637; F7=-f2793; Gb7=-f2959; G7=-f3135; Ab7=-f3322; A7=-f3520; Bb7=-f3729; B7=-f3951
C8=-f4186; Db8=-f4434; D8=-f4698; Eb8=-f4978; E8=-f5274; F8=-f5587; Gb8=-f5919; G8=-f6271; Ab8=-f6644; A8=-f7040; Bb8=-f7458; B8=-f7902
dc=-l100; dcp=-l150; c=-l200; cp=-l300; n=-l400; np=-l600; b=-l800; bp=-l1200; r=-l1600
if command -v beep > /dev/null; then
while [ $? -eq 0 ]; do
beep $n $E5 -n $c $B4 -n $c $C5 -n $c $D5 -n $dc $E5 -n $dc $D5 -n $c $C5 -n $c $B4 \
-n $n $A4 -n $c $A4 -n $c $C5 -n $n $E5 -n $c $D5 -n $c $C5 \
-n $c $B4 -n $c $E4 -n $c $Ab4 -n $c $C5 -n $n $D5 -n $n $E5 \
-n $n $C5 -n $n $A4 -n $n $A4 -n $c $B3 -n $c $C4 \
-n $np $D5 -n $c $F5 -n $c $A5 -n $dc $A5 -n $dc $A5 -n $c $G5 -n $c $F5 \
-n $n $E5 -n $c $E5 -n $c $C5 -n $c $E5 -n $dc $F5 -n $dc $E5 -n $c $D5 -n $c $C5 \
-n $c $B4 -n $c $E4 -n $c $Ab4 -n $c $C5 -n $n $D5 -n $n $E5 \
-n $n $C5 -n $n $A4 -n $b $A4 \
-n $n $E5 -n $c $B4 -n $c $C5 -n $c $D5 -n $dc $E5 -n $dc $D5 -n $c $C5 -n $c $B4 \
-n $n $A4 -n $c $A4 -n $c $C5 -n $n $E5 -n $c $D5 -n $c $C5 \
-n $c $B4 -n $c $E4 -n $c $Ab4 -n $c $C5 -n $n $D5 -n $n $E5 \
-n $n $C5 -n $n $A4 -n $n $A4 -n $c $B3 -n $c $C4 \
-n $np $D5 -n $c $F5 -n $c $A5 -n $dc $A5 -n $dc $A5 -n $c $G5 -n $c $F5 \
-n $n $E5 -n $c $E5 -n $c $C5 -n $c $E5 -n $dc $F5 -n $dc $E5 -n $c $D5 -n $c $C5 \
-n $c $B4 -n $c $E4 -n $c $Ab4 -n $c $C5 -n $n $D5 -n $n $E5 \
-n $n $C5 -n $n $A4 -n $b $A4 \
-n $b $E5 -n $b $C5 -n $b $D5 -n $b $B4 -n $b $C5 -n $b $A4 -n $b $Ab4 -n $c $B4 -n $c $E4 -n $c $Ab4 -n $c $B4 \
-n $b $E5 -n $b $C5 -n $b $D5 -n $b $B4 -n $n $C5 -n $n $E5 -n $n $A5 -n $n $A5 -n $r $Ab5 > /dev/null
done
fi

View File

@ -0,0 +1,3 @@
from .core import Tetris, Mino, Point
__all__ = ["Tetris", "Mino", "Point"]

362
terminis/pytetris/core.py Normal file
View File

@ -0,0 +1,362 @@
# -*- coding: utf-8 -*-
import random
import time
class Rotation:
CLOCKWISE = 1
COUNTERCLOCKWISE = -1
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x+other.x, self.y+other.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class Movement:
LEFT = Point(-1, 0)
RIGHT = Point(1, 0)
DOWN = Point(0, 1)
class Mino:
NO_MINO = 0
I = 1
J = 2
L = 3
O = 4
S = 5
T = 6
Z = 7
class Tetromino:
SUPER_ROTATION_SYSTEM = (
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, -1), Point(0, 2), Point(1, 2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, -1), Point(0, 2), Point(-1, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, 1), Point(0, -2), Point(1, -2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, 1), Point(0, -2), Point(1, -2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, -1), Point(0, 2), Point(-1, 2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, -1), Point(0, 2), Point(1, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, 1), Point(0, -2), Point(-1, -2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, 1), Point(0, 2), Point(-1, -2))
}
)
INIT_POSITION = Point(4, -1)
LOCK_DELAY = 0.5
def __init__(self):
self.position = self.INIT_POSITION
self.minoes_positions = self.MINOES_POSITIONS
self.orientation = 0
self.rotation_point_5_used = False
self.rotated_last = False
self.hold_enabled = True
self.prelocked = False
def t_spin(self):
return ""
class O(Tetromino):
MINOES_POSITIONS = (Point(0, 0), Point(1, 0), Point(0, -1), Point(1, -1))
MINOES_TYPE = Mino.O
SUPER_ROTATION_SYSTEM = (tuple(),)
def _rotate(self, direction):
return False
class I(Tetromino):
SUPER_ROTATION_SYSTEM = (
{
Rotation.COUNTERCLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
Rotation.CLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
Rotation.CLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, -1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
Rotation.CLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
Rotation.CLOCKWISE: (Point(0, 1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
},
)
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(2, 0))
MINOES_TYPE = Mino.I
class T(Tetromino):
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, 0))
MINOES_TYPE = Mino.T
T_SLOT = (Point(-1, -1), Point(1, -1), Point(1, 1), Point(-1, 1))
def t_spin(self):
if self.rotated_last:
a = not self.matrix.is_free_cell(self.position+self.T_SLOT[self.orientation])
b = not self.matrix.is_free_cell(self.position+self.T_SLOT[(1+self.orientation)%4])
c = not self.matrix.is_free_cell(self.position+self.T_SLOT[(3+self.orientation)%4])
d = not self.matrix.is_free_cell(self.position+self.T_SLOT[(2+self.orientation)%4])
if self.rotation_point_5_used or (a and b and (c or d)):
return "T-SPIN"
elif c and d and (a or b):
return "MINI T-SPIN"
return ""
class L(Tetromino):
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(1, -1))
MINOES_TYPE = Mino.L
class J(Tetromino):
MINOES_POSITIONS = (Point(-1, -1), Point(-1, 0), Point(0, 0), Point(1, 0))
MINOES_TYPE = Mino.J
class S(Tetromino):
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, -1))
MINOES_TYPE = Mino.S
class Z(Tetromino):
MINOES_POSITIONS = (Point(-1, -1), Point(0, -1), Point(0, 0), Point(1, 0))
MINOES_TYPE = Mino.Z
class Tetris:
TETROMINOES = (O, I, T, L, J, S, Z)
LEN_NEXT_QUEUE = 1
MATRIX_ROWS = 20
MATRIX_COLS = 10
INIT_POSITION = Point(4, 0)
FALL_DELAY = 1
LOCK_DELAY = 0.5
AUTOSHIFT_DELAY = 0.2
INIT_POSITION = Point(4, -1)
SCORES = (
{"name": "", "": 0, "MINI T-SPIN": 1, "T-SPIN": 4},
{"name": "SINGLE", "": 1, "MINI T-SPIN": 2, "T-SPIN": 8},
{"name": "DOUBLE", "": 3, "T-SPIN": 12},
{"name": "TRIPLE", "": 5, "T-SPIN": 16},
{"name": "TETRIS", "": 8}
)
def __init__(self, high_score=0):
self.high_score = high_score
def _random_piece(self):
if not self.random_bag:
self.random_bag = list(self.TETROMINOES)
random.shuffle(self.random_bag)
return self.random_bag.pop()()
def new_game(self, level=1):
self.matrix = [
[Mino.NO_MINO for x in range(self.MATRIX_COLS)]
for y in range(self.MATRIX_ROWS)
]
self.level = level - 1
self.goal = 0
self.score = 0
self.random_bag = []
self.next_queue = [
self._random_piece()
for _ in range(self.LEN_NEXT_QUEUE)
]
self.held_piece = None
self.fall_delay = self.FALL_DELAY
self.lock_delay = self.LOCK_DELAY
self.time = time.time()
self.next_level()
self.current_piece = None
self.new_piece()
def next_level(self):
self.level += 1
if self.level <= 20:
self.fall_delay = pow(0.8 - ((self.level-1)*0.007), self.level-1)
if self.level > 15:
self.lock_delay = 0.5 * pow(0.9, self.level-15)
self.goal += 5 * self.level
self.show_text("LEVEL %d" % self.level)
def new_piece(self):
if not self.current_piece:
self.current_piece = self.next_queue.pop(0)
self.next_queue.append(self._random_piece())
self.current_piece.position = self.INIT_POSITION
if not self._move(Movement.DOWN):
self.game_over()
def hold_piece(self):
if self.current_piece.hold_enabled:
self.current_piece, self.held_piece = self.held_piece, self.current_piece
self.held_piece.minoes_positions = self.held_piece.MINOES_POSITIONS
self.held_piece.hold_enabled = False
self.new_piece()
def _move_rotate(self, movement, minoes_positions):
potential_position = self.current_piece.position + movement
if all(
self.is_free_cell(potential_position+mino_position)
for mino_position in minoes_positions
):
self.position = potential_position
if self.current_piece.prelocked:
self.postpone_lock()
return True
else:
return False
def _move(self, movement):
if self._move_rotate(movement, self.current_piece.minoes_positions):
self.current_piece.rotated_last = False
return True
else:
if movement == Movement.DOWN and not self.current_piece.prelocked:
self.prelock()
return False
def _rotate(self, direction):
rotated_minoes_positions = tuple(
Point(-direction*mino_position.y, direction*mino_position.x)
for mino_position in self.current_piece.minoes_positions
)
for rotation_point, liberty_degree in enumerate(self.current_piece.SUPER_ROTATION_SYSTEM[self.current_piece.orientation][direction], start=1):
potential_position = self.position + liberty_degree
if self._move_rotate(potential_position, rotated_minoes_positions):
self.current_piece.orientation = (self.current_piece.orientation+direction) % 4
self.current_piece.minoes_position = rotated_minoes_positions
self.current_piece.rotated_last = True
if rotation_point == 5:
self.current_piece.rotation_point_5_used = True
return True
else:
return False
def move_left(self):
self._move(Movement.LEFT)
def move_right(self):
self._move(Movement.RIGHT)
def soft_drop(self):
if self._move(Movement.DOWN):
self.rows_dropped(1)
def hard_drop(self):
points = 0
while self._move(Movement.DOWN):
points += 2
self.rows_dropped(points)
self.lock_piece()
def rows_dropped(self, points):
self.update_score(points, "")
def fall(self):
self._move(Movement.DOWN)
def rotate_clockwise(self):
return self._rotate(Rotation.CLOCKWISE)
def rotate_counterclockwise(self):
return self._rotate(Rotation.COUNTERCLOCKWISE)
def is_free_cell(self, position):
return (
0 <= position.x < self.MATRIX_COLS
and position.y < self.MATRIX_ROWS
and not (position.y >= 0 and self.matrix[position.y][position.x] != Mino.NO_MINO)
)
def prelock(self):
"""
Schedules self.lock in self.lock_delay
"""
raise NotImplementedError
def postpone_lock(self):
"""
Reset timer calling self.lock to self.lock_delay
"""
raise NotImplementedError
def lock_piece(self):
if self.shape_fits(self.current_piece.position+Movement.DOWN, self.current_piece.minoes_positions):
self.postpone_lock()
return
t_spin = self.current_piece.t_spin()
for mino_position in self.current_piece.minoes_position:
position = mino_position + self.current_piece.position
if position.y >= 0:
self.matrix[position.y][position.x] = self.current_piece.MINOES_TYPE
else:
self.game_over()
return
nb_rows = 0
for y, row in enumerate(self.cells):
if all(mino for mino in row):
self.cells.pop(y)
self.cells.insert(0, [Mino.NO_MINO for x in range(self.NB_COLS)])
nb_rows += 1
self.current_piece = None
self.piece_locked(nb_rows, t_spin)
if t_spin or nb_rows:
points = self.SCORES[nb_rows][t_spin]
self.goal -= points
points *= 100 * self.level
text = t_spin
if t_spin and nb_rows:
text += " "
if nb_rows:
text += self.SCORES[nb_rows]["name"]
self.update_score(points, text)
self.combo = self.combo + 1 if nb_rows else -1
if self.combo >= 1:
points = (20 if nb_rows==1 else 50) * self.combo * self.level
text = "COMBO x%d" % self.combo
self.update_score(points, text)
if self.goal <= 0:
self.new_level()
self.new_piece()
def update_score(self, points, text):
self.score += points
if self.score > self.high_score:
self.high_score = self.score
self.show_text("%s\n%d" % (text, points))
def show_text(self, text):
print(text)
def pause(self):
self.time = time.time() - self.time
def resume(self):
self.time = time.time() - self.time
def game_over(self):
self.show_text("GAME OVER")

View File

@ -1,9 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .pytetris import Tetris, Mino, Point
import sys import sys
import os
import subprocess
import psutil
try: try:
import curses import curses
@ -14,9 +13,8 @@ You can install it on Windows with:
pip install --user windows-curses""" pip install --user windows-curses"""
) )
else: else:
curses.COLOR_ORANGE = curses.COLOR_WHITE curses.COLOR_ORANGE = 8
import random
import sched import sched
import time import time
import os import os
@ -24,9 +22,9 @@ import locale
import subprocess import subprocess
try: try:
from configparser import ConfigParser import configparser
except ImportError: # Python2 except ImportError: # Python2
from ConfigParser import SafeConfigParser as ConfigParser import ConfigParser as configparser
DIR_NAME = "Terminis" DIR_NAME = "Terminis"
@ -34,30 +32,10 @@ HELP_MSG = """terminis [options]
Tetris clone for terminal Tetris clone for terminal
--help\t-h\tshow command usage (this message) --help\tshow command usage (this message)
--edit\t-e\tedit controls in text editor --edit\tedit controls in text editor
--reset\t-r\treset to default controls settings --reset\treset to default controls settings
--level=n\t\tstart at level n (integer between 1 and 15)""" --level=n\tstart at level n (integer between 1 and 15)"""
class Rotation:
CLOCKWISE = 1
COUNTERCLOCKWISE = -1
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x+other.x, self.y+other.y)
class Movement:
LEFT = Point(-1, 0)
RIGHT = Point(1, 0)
DOWN = Point(0, 1)
class Scheduler(sched.scheduler, dict): class Scheduler(sched.scheduler, dict):
@ -82,181 +60,32 @@ class Scheduler(sched.scheduler, dict):
def cancel(self, name): def cancel(self, name):
if name in self: if name in self:
sched.scheduler.cancel(self, self.pop(name)) try:
sched.scheduler.cancel(self, self.pop(name))
except:
sys.exit(name)
scheduler = Scheduler() scheduler = Scheduler()
class Tetromino:
SUPER_ROTATION_SYSTEM = (
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, -1), Point(0, 2), Point(1, 2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, -1), Point(0, 2), Point(-1, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, 1), Point(0, -2), Point(1, -2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, 1), Point(0, -2), Point(1, -2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, -1), Point(0, 2), Point(-1, 2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(1, 0), Point(1, -1), Point(0, 2), Point(1, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, 1), Point(0, -2), Point(-1, -2)),
Rotation.CLOCKWISE: (Point(0, 0), Point(-1, 0), Point(-1, 1), Point(0, 2), Point(-1, -2))
}
)
lock_delay = 0.5
fall_delay = 1
color_pair = curses.COLOR_BLACK
def __init__(self, matrix, position):
self.matrix = matrix
self.position = position
self.minoes_positions = self.MINOES_POSITIONS
self.orientation = 0
self.rotation_point_5_used = False
self.rotated_last = False
self.hold_enabled = True
def move_rotate(self, movement, minoes_positions):
potential_position = self.position + movement
if all(
self.matrix.is_free_cell(potential_position+mino_position)
for mino_position in minoes_positions
):
self.position = potential_position
if "lock" in scheduler:
scheduler.cancel("lock")
scheduler.single_shot("lock", self.lock_delay, self.matrix.lock)
return True
else:
return False
def move(self, movement, lock=True, refresh=True):
if self.move_rotate(movement, self.minoes_positions):
self.rotated_last = False
if refresh:
self.matrix.refresh()
return True
else:
if (
lock
and movement == Movement.DOWN
and "lock" not in scheduler
):
scheduler.single_shot("lock", self.lock_delay, self.matrix.lock)
self.matrix.refresh()
return False
def rotate(self, direction):
rotated_minoes_positions = tuple(
Point(-direction*mino_position.y, direction*mino_position.x)
for mino_position in self.minoes_positions
)
for rotation_point, liberty_degree in enumerate(self.SUPER_ROTATION_SYSTEM[self.orientation][direction], start=1):
if self.move_rotate(liberty_degree, rotated_minoes_positions):
self.minoes_positions = rotated_minoes_positions
self.orientation = (self.orientation+direction) % 4
self.rotated_last = False
if rotation_point == 5:
self.rotation_point_5_used = True
self.matrix.refresh()
return True
else:
return False
def soft_drop(self):
if self.move(Movement.DOWN):
self.matrix.game.stats.piece_dropped(1)
def hard_drop(self):
lines = 0
while self.move(Movement.DOWN, lock=False, refresh=False):
lines += 2
self.matrix.refresh()
self.matrix.game.stats.piece_dropped(lines)
self.matrix.lock()
def fall(self):
self.move(Movement.DOWN)
def t_spin(self):
return ""
class O(Tetromino):
SUPER_ROTATION_SYSTEM = tuple()
MINOES_POSITIONS = (Point(0, 0), Point(1, 0), Point(0, -1), Point(1, -1))
COLOR = curses.COLOR_YELLOW
def rotate(self, direction):
return False
class I(Tetromino):
SUPER_ROTATION_SYSTEM = (
{
Rotation.COUNTERCLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
Rotation.CLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
Rotation.CLOCKWISE: (Point(0, 1), Point(-1, 1), Point(2, 1), Point(-1, -1), Point(2, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(0, -1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
Rotation.CLOCKWISE: (Point(-1, 0), Point(1, 0), Point(-2, 0), Point(1, -1), Point(-2, 2)),
},
{
Rotation.COUNTERCLOCKWISE: (Point(1, 0), Point(-1, 0), Point(2, 0), Point(-1, 1), Point(2, -2)),
Rotation.CLOCKWISE: (Point(0, 1), Point(1, -1), Point(-2, -1), Point(1, 1), Point(-2, -2)),
},
)
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(2, 0))
COLOR = curses.COLOR_CYAN
class T(Tetromino):
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, 0))
COLOR = curses.COLOR_MAGENTA
T_SLOT = (Point(-1, -1), Point(1, -1), Point(1, 1), Point(-1, 1))
def t_spin(self):
if self.rotated_last:
a = not self.matrix.is_free_cell(self.position+self.T_SLOT[self.orientation])
b = not self.matrix.is_free_cell(self.position+self.T_SLOT[(1+self.orientation)%4])
c = not self.matrix.is_free_cell(self.position+self.T_SLOT[(3+self.orientation)%4])
d = not self.matrix.is_free_cell(self.position+self.T_SLOT[(2+self.orientation)%4])
if self.rotation_point_5_used or (a and b and (c or d)):
return "T-SPIN"
elif c and d and (a or b):
return "MINI T-SPIN"
return ""
class L(Tetromino):
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(1, 0), Point(1, -1))
COLOR = curses.COLOR_ORANGE
class J(Tetromino):
MINOES_POSITIONS = (Point(-1, -1), Point(-1, 0), Point(0, 0), Point(1, 0))
COLOR = curses.COLOR_BLUE
class S(Tetromino):
MINOES_POSITIONS = (Point(-1, 0), Point(0, 0), Point(0, -1), Point(1, -1))
COLOR = curses.COLOR_GREEN
class Z(Tetromino):
MINOES_POSITIONS = (Point(-1, -1), Point(0, -1), Point(0, 0), Point(1, 0))
COLOR = curses.COLOR_RED
class Window: class Window:
def __init__(self, width, height, begin_x, begin_y): MINO_COLOR = {
Mino.O: 0,
Mino.I: 0,
Mino.T: 0,
Mino.L: 0,
Mino.J: 0,
Mino.S: 0,
Mino.Z: 0
}
def __init__(self, game, width, height, begin_x, begin_y):
self.game = game
self.window = curses.newwin(height, width, begin_y, begin_x) self.window = curses.newwin(height, width, begin_y, begin_x)
if self.TITLE: if self.TITLE:
self.title_begin_x = (width-len(self.TITLE)) // 2 + 1 self.title_begin_x = (width-len(self.TITLE)) // 2 + 1
self.piece = None self.piece = None
self.refresh()
def draw_border(self): def draw_border(self):
self.window.erase() self.window.erase()
@ -264,14 +93,14 @@ class Window:
if self.TITLE: if self.TITLE:
self.window.addstr(0, self.title_begin_x, self.TITLE, curses.A_BOLD) self.window.addstr(0, self.title_begin_x, self.TITLE, curses.A_BOLD)
def draw_piece(self): def draw_piece(self, piece, position):
if self.piece: if piece:
if "lock" in scheduler: if piece.prelocked:
attr = self.piece.color_pair | curses.A_BLINK | curses.A_REVERSE attr = self.MINO_COLOR[piece.MINOES_TYPE] | curses.A_BLINK | curses.A_REVERSE
else: else:
attr = self.piece.color_pair attr = self.MINO_COLOR[piece.MINOES_TYPE]
for mino_position in self.piece.minoes_positions: for mino_position in piece.minoes_positions:
position = mino_position + self.piece.position position = mino_position + position
self.draw_mino(position.x, position.y, attr) self.draw_mino(position.x, position.y, attr)
def draw_mino(self, x, y, attr): def draw_mino(self, x, y, attr):
@ -284,19 +113,17 @@ class Matrix(Window):
NB_LINES = 21 NB_LINES = 21
WIDTH = NB_COLS*2+2 WIDTH = NB_COLS*2+2
HEIGHT = NB_LINES+1 HEIGHT = NB_LINES+1
PIECE_POSITION = Point(4, -1)
TITLE = "" TITLE = ""
def __init__(self, game, begin_x, begin_y): def __init__(self, game, begin_x, begin_y):
begin_x += (game.WIDTH - self.WIDTH) // 2 begin_x += (game.WIDTH - self.WIDTH) // 2
begin_y += (game.HEIGHT - self.HEIGHT) // 2 begin_y += (game.HEIGHT - self.HEIGHT) // 2
self.game = game
self.cells = [ self.cells = [
[None for x in range(self.NB_COLS)] [None for x in range(self.NB_COLS)]
for y in range(self.NB_LINES) for y in range(self.NB_LINES)
] ]
self.piece = None self.piece = None
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y) Window.__init__(self, game, self.WIDTH, self.HEIGHT, begin_x, begin_y)
def refresh(self, paused=False): def refresh(self, paused=False):
self.draw_border() self.draw_border()
@ -307,183 +134,74 @@ class Matrix(Window):
for x, color in enumerate(line): for x, color in enumerate(line):
if color is not None: if color is not None:
self.draw_mino(x, y, color) self.draw_mino(x, y, color)
self.draw_piece() self.draw_piece(self.game.current_piece, self.game.current_piece.position)
self.window.refresh() self.window.refresh()
def is_free_cell(self, position):
return (
0 <= position.x < self.NB_COLS
and position.y < self.NB_LINES
and not (position.y >= 0 and self.cells[position.y][position.x] is not None)
)
def lock(self):
if not self.piece.move(Movement.DOWN):
scheduler.cancel("fall")
t_spin = self.piece.t_spin()
for mino_position in self.piece.minoes_positions:
position = mino_position + self.piece.position
if position.y >= 0:
self.cells[position.y][position.x] = self.piece.color_pair
else:
self.game.over()
return
nb_lines_cleared = 0
for y, line in enumerate(self.cells):
if all(mino for mino in line):
self.cells.pop(y)
self.cells.insert(0, [None for x in range(self.NB_COLS)])
nb_lines_cleared += 1
self.game.stats.piece_locked(nb_lines_cleared, t_spin)
self.piece = None
self.game.new_piece()
class HoldNext(Window): class HoldNext(Window):
HEIGHT = 6 HEIGHT = 6
PIECE_POSITION = Point(6, 3) PIECE_POSITION = Point(6, 3)
def __init__(self, width, begin_x, begin_y): def __init__(self, game, width, begin_x, begin_y):
Window.__init__(self, width, self.HEIGHT, begin_x, begin_y) Window.__init__(self, game, width, self.HEIGHT, begin_x, begin_y)
def refresh(self, paused=False):
self.draw_border()
if not paused:
self.draw_piece()
self.window.refresh()
class Hold(HoldNext): class Hold(HoldNext):
TITLE = "HOLD" TITLE = "HOLD"
def refresh(self, paused=False):
self.draw_border()
if not paused:
self.draw_piece(self.game.held_piece, self.PIECE_POSITION)
self.window.refresh()
class Next(HoldNext): class Next(HoldNext):
TITLE = "NEXT" TITLE = "NEXT"
def refresh(self, paused=False):
self.draw_border()
if not paused:
self.draw_piece(self.game.next_queue[0], self.PIECE_POSITION)
self.window.refresh()
class Stats(Window): class Stats(Window):
SCORES = (
{"name": "", "": 0, "MINI T-SPIN": 1, "T-SPIN": 4},
{"name": "SINGLE", "": 1, "MINI T-SPIN": 2, "T-SPIN": 8},
{"name": "DOUBLE", "": 3, "T-SPIN": 12},
{"name": "TRIPLE", "": 5, "T-SPIN": 16},
{"name": "TETRIS", "": 8}
)
TITLE = "STATS" TITLE = "STATS"
FILE_NAME = ".high_score" FILE_NAME = ".high_score"
if sys.platform == "win32": if sys.platform == "win32":
DIR_PATH = os.environ.get("appdata", os.path.expanduser(r"~\Appdata\Roaming")) DIR_PATH = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
else: else:
DIR_PATH = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share")) DIR_PATH = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
DIR_PATH = os.path.join(DIR_PATH, DIR_NAME) DIR_PATH = os.path.join(DIR_PATH, DIR_NAME)
FILE_PATH = os.path.join(DIR_PATH, FILE_NAME) FILE_PATH = os.path.join(DIR_PATH, FILE_NAME)
def __init__(self, game, width, height, begin_x, begin_y): def __init__(self, game, width, height, begin_x, begin_y):
for arg in sys.argv[1:]:
if arg.startswith("--level="):
try:
self.level = int(arg[8:])
except ValueError:
sys.exit(HELP_MSG)
else:
self.level = max(1, self.level)
self.level = min(15, self.level)
self.level -= 1
break
else:
self.level = 0
self.game = game
self.width = width self.width = width
self.height = height self.height = height
self.goal = 0
self.score = 0
try: try:
with open(self.FILE_PATH, "r") as f: with open(self.FILE_PATH, "r") as f:
self.high_score = int(f.read()) self.high_score = int(f.read())
except: except:
self.high_score = 0 self.high_score = 0
self.combo = -1 Window.__init__(self, game, width, height, begin_x, begin_y)
self.time = time.time()
self.lines_cleared = 0
self.strings = []
Window.__init__(self, width, height, begin_x, begin_y)
self.new_level()
def refresh(self): def refresh(self):
self.draw_border() self.draw_border()
self.window.addstr(2, 2, "SCORE\t{:n}".format(self.score)) self.window.addstr(2, 2, "SCORE\t{:n}".format(self.game.score))
if self.score >= self.high_score: if self.game.score >= self.game.high_score:
self.window.addstr(3, 2, "HIGH\t{:n}".format(self.high_score), curses.A_BLINK|curses.A_BOLD) self.window.addstr(3, 2, "HIGH\t{:n}".format(self.game.high_score), curses.A_BLINK|curses.A_BOLD)
else: else:
self.window.addstr(3, 2, "HIGH\t{:n}".format(self.high_score)) self.window.addstr(3, 2, "HIGH\t{:n}".format(self.game.high_score))
self.window.addstr(5, 2, "LEVEL\t%d" % self.level) self.window.addstr(5, 2, "LEVEL\t%d" % self.game.level)
self.window.addstr(6, 2, "GOAL\t%d" % self.goal) self.window.addstr(6, 2, "GOAL\t%d" % self.game.goal)
self.window.addstr(7, 2, "LINES\t%d" % self.lines_cleared)
start_y = self.height - len(self.strings) - 2
for y, string in enumerate(self.strings, start=start_y):
x = (self.width-len(string)) // 2 + 1
self.window.addstr(y, x, string)
self.refresh_time() self.refresh_time()
def refresh_time(self): def refresh_time(self):
t = time.localtime(time.time() - self.time) t = time.localtime(time.time() - self.game.time)
self.window.addstr(4, 2, "TIME\t%02d:%02d:%02d" % (t.tm_hour-1, t.tm_min, t.tm_sec)) self.window.addstr(4, 2, "TIME\t%02d:%02d:%02d" % (t.tm_hour-1, t.tm_min, t.tm_sec))
self.window.refresh() self.window.refresh()
def new_level(self):
self.level += 1
if self.level <= 20:
Tetromino.fall_delay = pow(0.8 - ((self.level-1)*0.007), self.level-1)
if self.level > 15:
Tetromino.lock_delay = 0.5 * pow(0.9, self.level-15)
self.goal += 5 * self.level
self.refresh()
def piece_dropped(self, lines):
self.score += lines
if self.score > self.high_score:
self.high_score = self.score
self.refresh()
def piece_locked(self, nb_lines, t_spin):
self.strings = []
if t_spin:
self.strings.append(t_spin)
if nb_lines:
self.strings.append(self.SCORES[nb_lines]["name"])
self.combo += 1
else:
self.combo = -1
if nb_lines or t_spin:
self.lines_cleared += nb_lines
ds = self.SCORES[nb_lines][t_spin]
self.goal -= ds
ds *= 100 * self.level
self.score += ds
self.strings.append(str(ds))
if self.combo >= 1:
self.strings.append("COMBO x%d" % self.combo)
ds = (20 if nb_lines==1 else 50) * self.combo * self.level
self.score += ds
self.strings.append(str(ds))
if nb_lines == 4 or (nb_lines and t_spin):
curses.beep()
if self.score > self.high_score:
self.high_score = self.score
if self.goal <= 0:
self.new_level()
else:
self.refresh()
def save(self): def save(self):
if not os.path.exists(self.DIR_PATH): if not os.path.exists(self.DIR_PATH):
os.makedirs(self.DIR_PATH) os.makedirs(self.DIR_PATH)
@ -495,10 +213,10 @@ class Stats(Window):
print(e) print(e)
class ControlsParser(ConfigParser): class ControlsParser(configparser.SafeConfigParser):
FILE_NAME = "config.cfg" FILE_NAME = "config.cfg"
if sys.platform == "win32": if sys.platform == "win32":
DIR_PATH = os.environ.get("appdata", os.path.expanduser(r"~\Appdata\Roaming")) DIR_PATH = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
else: else:
DIR_PATH = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")) DIR_PATH = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
DIR_PATH = os.path.join(DIR_PATH, DIR_NAME) DIR_PATH = os.path.join(DIR_PATH, DIR_NAME)
@ -525,7 +243,7 @@ class ControlsParser(ConfigParser):
} }
def __init__(self): def __init__(self):
ConfigParser.__init__(self) configparser.SafeConfigParser.__init__(self)
self.optionxform = str self.optionxform = str
self.add_section(self.SECTION) self.add_section(self.SECTION)
for action, key in self.DEFAULTS.items(): for action, key in self.DEFAULTS.items():
@ -564,10 +282,11 @@ class ControlsParser(ConfigParser):
class ControlsWindow(Window, ControlsParser): class ControlsWindow(Window, ControlsParser):
TITLE = "CONTROLS" TITLE = "CONTROLS"
def __init__(self, width, height, begin_x, begin_y): def __init__(self, game, width, height, begin_x, begin_y):
ControlsParser.__init__(self) ControlsParser.__init__(self)
self.read(self.FILE_PATH) self.read(self.FILE_PATH)
Window.__init__(self, width, height, begin_x, begin_y) Window.__init__(self, game, width, height, begin_x, begin_y)
self.refresh()
for action, key in self.items(self.SECTION): for action, key in self.items(self.SECTION):
if key == "SPACE": if key == "SPACE":
self[action] = " " self[action] = " "
@ -584,40 +303,45 @@ class ControlsWindow(Window, ControlsParser):
self.window.refresh() self.window.refresh()
class Music: class Game(Tetris):
PATH = os.path.join(os.path.dirname(__file__), "music.sh")
def __init__(self):
self.process = None
def play(self):
self.process = subprocess.Popen(["sh", self.PATH])
def stop(self):
if self.process:
for proc in psutil.Process(self.process.pid).children(recursive=True):
proc.terminate()
self.process.terminate()
self.process = None
class Game:
WIDTH = 80 WIDTH = 80
HEIGHT = Matrix.HEIGHT HEIGHT = Matrix.HEIGHT
AUTOREPEAT_DELAY = 0.02 MINO_COLOR = {
TETROMINOES = (O, I, T, L, J, S, Z) Mino.O: curses.COLOR_YELLOW,
Mino.I: curses.COLOR_CYAN,
Mino.T: curses.COLOR_MAGENTA,
Mino.L: curses.COLOR_ORANGE,
Mino.J: curses.COLOR_BLUE,
Mino.S: curses.COLOR_GREEN,
Mino.Z: curses.COLOR_RED
}
HIGH_SCORE_FILE_NAME = ".high_score"
if sys.platform == "win32":
DATA_DIR_PATH = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
else:
DATA_DIR_PATH = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
DATA_DIR_PATH = os.path.join(DATA_DIR_PATH, DIR_NAME)
HIGH_SCORE_FILE_PATH = os.path.join(DATA_DIR_PATH, HIGH_SCORE_FILE_NAME)
def __init__(self, scr): def __init__(self, scr):
try:
with open(self.HIGH_SCORE_FILE_PATH, "r") as f:
high_score = int(f.read())
except:
high_score = 0
Tetris.__init__(self, high_score)
if curses.has_colors(): if curses.has_colors():
curses.start_color() curses.start_color()
if curses.can_change_color(): if curses.can_change_color():
curses.init_color(curses.COLOR_YELLOW, 1000, 500, 0) curses.init_color(curses.COLOR_YELLOW, 1000, 500, 0)
for tetromino_class in self.TETROMINOES: for mino_type, color in self.MINO_COLOR.items():
curses.init_pair(tetromino_class.COLOR, tetromino_class.COLOR, curses.COLOR_WHITE) curses.init_pair(color, color, curses.COLOR_WHITE)
if tetromino_class.COLOR == curses.COLOR_ORANGE: if color == curses.COLOR_ORANGE:
tetromino_class.color_pair = curses.color_pair(curses.COLOR_YELLOW) Window.MINO_COLOR[mino_type] = curses.color_pair(curses.COLOR_YELLOW)
else: else:
tetromino_class.color_pair = curses.color_pair(tetromino_class.COLOR)|curses.A_BOLD Window.MINO_COLOR[mino_type] = curses.color_pair(color)|curses.A_BOLD
try: try:
curses.curs_set(0) curses.curs_set(0)
except curses.error: except curses.error:
@ -633,55 +357,58 @@ class Game:
right_x = left_x + Matrix.WIDTH + side_width + 2 right_x = left_x + Matrix.WIDTH + side_width + 2
bottom_y = top_y + Hold.HEIGHT bottom_y = top_y + Hold.HEIGHT
self.matrix = Matrix(self, left_x, top_y) self.matrix_window = Matrix(self, left_x, top_y)
self.hold = Hold(side_width, left_x, top_y) self.hold_window = Hold(self, side_width, left_x, top_y)
self.next = Next(side_width, right_x, top_y) self.next_window = Next(self, side_width, right_x, top_y)
self.stats = Stats(self, side_width, side_height, left_x, bottom_y) self.stats_window = Stats(self, side_width, side_height, left_x, bottom_y)
self.controls = ControlsWindow(side_width, side_height, right_x, bottom_y) self.controls_window = ControlsWindow(self, side_width, side_height, right_x, bottom_y)
self.music = Music()
self.actions = { self.actions = {
self.controls["QUIT"]: self.quit, self.controls_window["QUIT"]: self.quit,
self.controls["PAUSE"]: self.pause, self.controls_window["PAUSE"]: self.pause,
self.controls["HOLD"]: self.swap, self.controls_window["HOLD"]: self.hold_piece,
self.controls["MOVE LEFT"]: lambda: self.matrix.piece.move(Movement.LEFT), self.controls_window["MOVE LEFT"]: self.move_left,
self.controls["MOVE RIGHT"]: lambda: self.matrix.piece.move(Movement.RIGHT), self.controls_window["MOVE RIGHT"]: self.move_right,
self.controls["SOFT DROP"]: lambda: self.matrix.piece.soft_drop(), self.controls_window["SOFT DROP"]: self.soft_drop,
self.controls["ROTATE COUNTER"]: lambda: self.matrix.piece.rotate(Rotation.COUNTERCLOCKWISE), self.controls_window["ROTATE COUNTER"]: self.rotate_counterclockwise,
self.controls["ROTATE CLOCKWISE"]: lambda: self.matrix.piece.rotate(Rotation.CLOCKWISE), self.controls_window["ROTATE CLOCKWISE"]: self.rotate_clockwise,
self.controls["HARD DROP"]: lambda: self.matrix.piece.hard_drop() self.controls_window["HARD DROP"]: self.hard_drop
} }
self.paused = False self.paused = False
self.random_bag = []
self.next.piece = self.random_piece() for arg in sys.argv[1:]:
self.new_piece() if arg.startswith("--level="):
scheduler.repeat("time", 1, self.stats.refresh_time) try:
scheduler.repeat("input", self.AUTOREPEAT_DELAY, self.process_input) level = int(arg[8:])
self.music.play() except ValueError:
sys.exit(HELP_MSG)
else:
level = max(1, level)
level = min(15, level)
break
else:
level = 1
self.new_game(level)
self.matrix_window.refresh()
self.hold_window.refresh()
self.next_window.refresh()
self.stats_window.refresh()
scheduler.repeat("time", 1, self.stats_window.refresh_time)
scheduler.repeat("input", self.AUTOSHIFT_DELAY, self.process_input)
try: try:
scheduler.run() scheduler.run()
except KeyboardInterrupt: except KeyboardInterrupt:
self.quit() self.quit()
def random_piece(self):
if not self.random_bag:
self.random_bag = list(self.TETROMINOES)
random.shuffle(self.random_bag)
return self.random_bag.pop()(self.matrix, Next.PIECE_POSITION)
def new_piece(self): def new_piece(self):
scheduler.cancel("lock") Tetris.new_piece(self)
if not self.matrix.piece: self.next_window.refresh()
self.matrix.piece, self.next.piece = self.next.piece, self.random_piece() self.matrix_window.refresh()
self.next.refresh()
self.matrix.piece.position = Matrix.PIECE_POSITION
if self.matrix.piece.move(Movement.DOWN):
scheduler.repeat("fall", Tetromino.fall_delay, self.matrix.piece.fall)
self.matrix.refresh()
else:
self.over()
def process_input(self): def process_input(self):
try: try:
@ -690,86 +417,76 @@ class Game:
pass pass
else: else:
action() action()
self.matrix_window.refresh()
def pause(self): def pause(self):
self.stats.time = time.time() - self.stats.time Tetris.pause(self)
self.paused = True self.paused = True
self.hold.refresh(paused=True) self.hold_window.refresh(paused=True)
self.matrix.refresh(paused=True) self.matrix_window.refresh(paused=True)
self.next.refresh(paused=True) self.next_window.refresh(paused=True)
self.scr.timeout(-1) self.scr.timeout(-1)
self.music.stop()
while True: while True:
key = self.scr.getkey() key = self.scr.getkey()
if key == self.controls["QUIT"]: if key == self.controls_window["QUIT"]:
self.quit() self.quit()
break break
elif key == self.controls["PAUSE"]: elif key == self.controls_window["PAUSE"]:
self.scr.timeout(0)
self.hold_window.refresh()
self.matrix_window.refresh()
self.next_window.refresh()
self.stats_window.time = time.time() - self.stats_window.time
break break
self.scr.timeout(0) def hold_piece(self):
self.hold.refresh() Tetris.hold_piece(self)
self.matrix.refresh() self.hold_window.refresh()
self.next.refresh()
self.stats.time = time.time() - self.stats.time
self.music.play()
def swap(self): def game_over(self):
if self.matrix.piece.hold_enabled: Tetris.game_over(self)
scheduler.cancel("fall") self.time = time.time() - self.time
scheduler.cancel("lock") self.matrix_window.refresh()
self.matrix.piece, self.hold.piece = self.hold.piece, self.matrix.piece
self.hold.piece.position = self.hold.PIECE_POSITION
self.hold.piece.minoes_positions = self.hold.piece.MINOES_POSITIONS
self.hold.piece.hold_enabled = False
self.hold.refresh()
self.new_piece()
def over(self):
self.stats.time = time.time() - self.stats.time
self.matrix.refresh()
if curses.has_colors(): if curses.has_colors():
for tetromino_class in self.TETROMINOES: for color in self.MINO_COLOR.values():
curses.init_pair(tetromino_class.COLOR, tetromino_class.COLOR, curses.COLOR_BLACK) curses.init_pair(color, color, curses.COLOR_BLACK)
for y, word in enumerate((("GA", "ME") ,("OV", "ER")), start=Matrix.NB_LINES//2): for y, word in enumerate((("GA", "ME") ,("OV", "ER")), start=Matrix.NB_LINES//2):
for x, syllable in enumerate(word, start=Matrix.NB_COLS//2-1): for x, syllable in enumerate(word, start=Matrix.NB_COLS//2-1):
color = self.matrix.cells[y][x] color = self.matrix[y][x]
if color is None: if color is None:
color = curses.COLOR_BLACK color = curses.COLOR_BLACK
else: else:
color |= curses.A_REVERSE color |= curses.A_REVERSE
self.matrix.window.addstr(y, x*2+1, syllable, color) self.matrix_window.window.addstr(y, x*2+1, syllable, color)
self.matrix.window.refresh() self.matrix_window.window.refresh()
curses.beep() curses.beep()
self.scr.timeout(-1) self.scr.timeout(-1)
while self.scr.getkey() != self.controls["QUIT"]: while self.scr.getkey() != self.controls_window["QUIT"]:
pass pass
self.stats.time = time.time() - self.stats.time self.time = time.time() - self.time
self.quit() self.quit()
def quit(self): def quit(self):
self.stats.save() self.stats_window.save()
t = time.localtime(time.time() - self.stats.time) t = time.localtime(time.time() - self.time)
self.music.stop()
sys.exit( sys.exit(
"SCORE\t{:n}\n".format(self.stats.score) + "SCORE\t{:n}\n".format(self.score) +
"HIGH\t{:n}\n".format(self.stats.high_score) + "HIGH\t{:n}\n".format(self.high_score) +
"TIME\t%02d:%02d:%02d\n" % (t.tm_hour-1, t.tm_min, t.tm_sec) + "TIME\t%02d:%02d:%02d\n" % (t.tm_hour-1, t.tm_min, t.tm_sec) +
"LEVEL\t%d\n" % self.stats.level + "LEVEL\t%d\n" % self.level
"LINES\t%d" % self.stats.lines_cleared
) )
def main(): def main():
if "--help" in sys.argv[1:] or "-h" in sys.argv[1:] or "/?" in sys.argv[1:]: if "--help" in sys.argv[1:] or "/?" in sys.argv[1:]:
print(HELP_MSG) print(HELP_MSG)
else: else:
if "--reset" in sys.argv[1:] or "-r" in sys.argv[1:]: if "--reset" in sys.argv[1:]:
controls = ControlsParser() controls = ControlsParser()
controls.reset() controls.reset()
controls.edit() controls.edit()
elif "--edit" in sys.argv[1:] or "-e" in sys.argv[1:]: elif "--edit" in sys.argv[1:]:
ControlsParser().edit() ControlsParser().edit()
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')