This commit is contained in:
adrienmalin 2019-02-10 17:23:40 +01:00
parent 80e790d631
commit 3dc062cc23
6 changed files with 877 additions and 739 deletions

108
.gitignore vendored Normal file
View File

@ -0,0 +1,108 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
dist/
develop-eggs/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
target/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
# poetry
poetry.lock

View File

@ -1,60 +1,24 @@
# Terminis # Terminis
Another Tetris clone... again... but for terminal. Ideal for servers without GUI! Tetris clone for terminal. Ideal for servers without GUI!
## Installation
## Screenshot
```bash ```bash
┌────────────HOLD───────────┐┌────────────────────┐┌────────────NEXT───────────┐ pip install --user terminis
│ ││██ ││ │
│ ██ ││██ ││ ██ │
│ ██████ ││██ ││ ██████ │
│ ││██ ││ │
└───────────────────────────┘│ ██ │└───────────────────────────┘
┌────────────STATS──────────┐│ ██████ │┌──────────CONTROLS─────────┐
│ ││ ██ ││ │
│ SCORE 1017 ││ ████ ││ LEFT MOVE LEFT │
│ HIGH 1017 ││ ████ ││ RIGHT MOVE RIGHT │
│ TIME 00:01:05 ││ ██████ ││ DOWN SOFT DROP │
│ LEVEL 1 ││ ████████ ││ SPACE HARD DROP │
│ GOAL 2 ││ ████ ████████████││ UP ROTATE COUNTER │
│ LINES 2 ││ ██████████████████││ * ROTATE CLOCKWISE │
│ ││ ██████████████████││ H HOLD │
│ ││ ██████████████████││ P PAUSE │
│ ││ ██████████████████││ Q QUIT │
│ ││ ██████████████████││ │
│ ││ ██████████████████││ │
│ ││ ██████████████████││ │
│ ││ ██████████████████││ │
└───────────────────────────┘└────────────────────┘└───────────────────────────┘
``` ```
## Usage ## Usage
```bash ```bash
python terminis.py [level] terminis [level]
``` ```
level: integer between 1 and 15 level: integer between 1 and 15
## Dependency
* Python
* Python module curses (native on linux)
Can be installed on windows with:
```batch
pip install --user windows-curses
```
## Controls edit ## Controls edit
Edit values of dictionary CONTROLS in the script:
```python You can change keys by editing:
CONTROLS = { * `%appdata%\Terminis\config.cfg` on Windows
"MOVE LEFT": "KEY_LEFT", * `~/.local/share/Terminis/config.cfg` on Linux
"MOVE RIGHT": "KEY_RIGHT",
"SOFT DROP": "KEY_DOWN", Acceptable values:
"HARD DROP": " ", * printable characters ('q', '*', ' '...)
"ROTATE COUNTER": "KEY_UP", * curses's constants name starting with "KEY_" (see [Python documentation](https://docs.python.org/3/library/curses.html?highlight=curses#constants))
"ROTATE CLOCKWISE": "*",
"HOLD": "h",
"PAUSE": "p",
"QUIT": "q"
}
```
Acceptable values are printable characters ('q', 'w'...) and curses's constants name starting with "KEY_" (see [Python documentation](https://docs.python.org/3/library/curses.html?highlight=curses#constants))

29
pyproject.toml Normal file
View File

@ -0,0 +1,29 @@
[tool.poetry]
name = "terminis"
version = "0.1.7"
description = "Tetris clone for terminal. Ideal for servers without GUI!"
authors = ["adrienmalin <41926238+adrienmalin@users.noreply.github.com>"]
license = "MIT"
repository = "https://github.com/adrienmalin/Terminis"
keywords = ["Tetris", "terminal", "curses"]
classifiers = [
"Environment :: Console :: Curses",
"Programming Language :: Python",
"Topic :: Games/Entertainment :: Puzzle Games",
"Topic :: Games/Entertainment :: Arcade",
"Operating System :: OS Independent",
"Topic :: Terminals",
"Topic :: System :: Systems Administration"
]
readme = "README.md"
[tool.poetry.dependencies]
python = ">2.6"
windows-curses = {version = "^1.0", platform = "win32"}
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
[tool.poetry.scripts]
terminis = 'terminis.terminis:main'

1
terminis/__init__.py Normal file
View File

@ -0,0 +1 @@
__version__ = '0.1.0'

7
terminis/__main__.py Normal file
View File

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from . import terminis
if __name__ == "__main__":
terminis.main()

View File

@ -12,24 +12,17 @@ import random
import sched import sched
import time import time
import os import os
import configparser
# You can change controls here. WIN_DIR = "~/Appdata/Roaming/Terminis/"
# Acceptable values are printable characters ('q', 'w'...) and curses's constants name starting with "KEY_" LINUX_DIR = "~/.local/share/"
# See https://docs.python.org/3/library/curses.html?highlight=curses#constants
CONTROLS = {
"MOVE LEFT": "KEY_LEFT",
"MOVE RIGHT": "KEY_RIGHT",
"SOFT DROP": "KEY_DOWN",
"HARD DROP": " ",
"ROTATE COUNTER": "KEY_UP",
"ROTATE CLOCKWISE": "*",
"HOLD": "h",
"PAUSE": "p",
"QUIT": "q"
}
FILE = os.path.expanduser(os.path.join('~', ".terminis"))
if sys.platform == "win32":
dir_path = os.path.expanduser(WIN_DIR)
else:
dir_path = os.path.expanduser(LINUX_DIR)
class Rotation: class Rotation:
@ -312,6 +305,7 @@ class Window:
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()
@ -345,12 +339,12 @@ class Matrix(Window):
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
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y)
self.game = game 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)
] ]
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y)
def refresh(self, paused=False): def refresh(self, paused=False):
self.draw_border() self.draw_border()
@ -386,35 +380,26 @@ class Matrix(Window):
self.game.new_piece() self.game.new_piece()
class Hold(Window): class HoldNext(Window):
HEIGHT = 6
PIECE_POSITION = Point(6, 3)
def __init__(self, width, begin_x, begin_y):
Window.__init__(self, 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):
TITLE = "HOLD" TITLE = "HOLD"
HEIGHT = 6
PIECE_POSITION = Point(6, 3)
def __init__(self, width, begin_x, begin_y):
Window.__init__(self, 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 Next(Window): class Next(HoldNext):
TITLE = "NEXT" TITLE = "NEXT"
HEIGHT = 6
PIECE_POSITION = Point(6, 3)
def __init__(self, width, begin_x, begin_y):
Window.__init__(self, width, self.HEIGHT, begin_x, begin_y)
self.window = curses.newwin(self.HEIGHT, width, begin_y, begin_x)
def refresh(self, paused=False):
self.draw_border()
if not paused:
self.draw_piece()
self.window.refresh()
class Stats(Window): class Stats(Window):
@ -427,23 +412,28 @@ class Stats(Window):
) )
LINES_CLEARED_NAMES = ("", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS") LINES_CLEARED_NAMES = ("", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS")
TITLE = "STATS" TITLE = "STATS"
FILE_NAME = ".high_score"
file_path = os.path.join(dir_path, FILE_NAME)
def __init__(self, game, width, height, begin_x, begin_y, level): def __init__(self, game, width, height, begin_x, begin_y, level):
Window.__init__(self, width, height, begin_x, begin_y)
self.game = game self.game = game
self.width = width self.width = width
self.height = height self.height = height
self.level = level - 1 self.level = level - 1
self.goal = 0 self.goal = 0
self.score = 0 self.score = 0
try: self.load()
with open(FILE, "r") as f:
self.high_score = int(f.read())
except:
self.high_score = 0
self.time = time.time() self.time = time.time()
self.lines_cleared = 0 self.lines_cleared = 0
self.clock_timer = None self.clock_timer = None
Window.__init__(self, width, height, begin_x, begin_y)
def load(self):
try:
with open(self.file_path, "r") as f:
self.high_score = int(f.read())
except:
self.high_score = 0
def refresh(self): def refresh(self):
self.draw_border() self.draw_border()
@ -509,24 +499,60 @@ class Stats(Window):
def save(self): def save(self):
try: try:
with open(FILE, mode='w') as f: with open(self.file_path, mode='w') as f:
f.write(str(self.high_score)) f.write(str(self.high_score))
except: except Exception as e:
pass print("High score could not be saved:")
print(e)
class Controls(Window): class Config(Window, configparser.SafeConfigParser):
TITLE = "CONTROLS" TITLE = "CONTROLS"
FILE_NAME = "config.cfg"
file_path = os.path.join(dir_path, FILE_NAME)
def __init__(self, width, height, begin_x, begin_y):
configparser.SafeConfigParser.__init__(self)
self.optionxform = str
self.add_section("CONTROLS")
self.set("CONTROLS", "MOVE LEFT", "KEY_LEFT")
self.set("CONTROLS", "MOVE RIGHT", "KEY_RIGHT")
self.set("CONTROLS", "SOFT DROP", "KEY_DOWN")
self.set("CONTROLS", "HARD DROP", " ")
self.set("CONTROLS", "ROTATE COUNTER", "KEY_UP")
self.set("CONTROLS", "ROTATE CLOCKWISE", "*")
self.set("CONTROLS", "HOLD", "h")
self.set("CONTROLS", "PAUSE", "p")
self.set("CONTROLS", "QUIT", "q")
if os.path.exists(self.file_path):
self.read(self.file_path)
for action, key in self.items("CONTROLS"):
if key == "":
self.set("CONTROLS", action, " ")
else:
try:
with open(self.file_path, 'w') as f:
f.write(
"""# Acceptable values are printable characters ("q", "*"...) and curses's constants name starting with "KEY_"
# See https://docs.python.org/3/library/curses.html?highlight=curses#constants
"""
)
self.write(f)
except Exception as e:
print("Configuration could not be saved:")
print(e)
Window.__init__(self, width, height, begin_x, begin_y)
def refresh(self): def refresh(self):
self.draw_border() self.draw_border()
for y, (action, key) in enumerate(CONTROLS.items(), start=2): for y, (action, key) in enumerate(self.items("CONTROLS"), start=2):
if key == " ": if key == " ":
key = "SPACE" key = "SPACE"
else: else:
key = key.replace("KEY_", "") key = key.replace("KEY_", "")
key = key.upper() key = key.upper()
self.window.addstr(y, 2, "%s\t%s" % (key, action)) self.window.addstr(y, 2, "%s\t%s" % (key, action.upper()))
self.window.refresh() self.window.refresh()
class Game: class Game:
@ -548,17 +574,12 @@ class Game:
self.next = Next(side_width, right_x, top_y) self.next = Next(side_width, right_x, top_y)
self.next.piece = self.random_piece()(self.matrix, Next.PIECE_POSITION) self.next.piece = self.random_piece()(self.matrix, Next.PIECE_POSITION)
self.stats = Stats(self, side_width, side_height, left_x, bottom_y, level) self.stats = Stats(self, side_width, side_height, left_x, bottom_y, level)
self.controls = Controls(side_width, side_height, right_x, bottom_y) self.config = Config(side_width, side_height, right_x, bottom_y)
self.playing = True self.playing = True
self.paused = False self.paused = False
self.hold.refresh()
self.matrix.refresh()
self.next.refresh()
self.stats.refresh()
self.controls.refresh()
self.stats.new_level()
self.new_piece() self.new_piece()
def random_piece(self): def random_piece(self):
if not self.random_bag: if not self.random_bag:
self.random_bag = [O, I, T, L, J, S, Z] self.random_bag = [O, I, T, L, J, S, Z]
@ -582,29 +603,31 @@ class Game:
while self.playing: while self.playing:
self.scheduler.run() self.scheduler.run()
def process_input(self, _): def process_input(self, delay):
end = time.time() + delay
while self.playing and time.time() < end:
try: try:
key = self.scr.getkey() key = self.scr.getkey()
except curses.error: except curses.error:
return return
else: else:
if key == CONTROLS["QUIT"]: if key == self.config["CONTROLS"]["QUIT"]:
self.quit() self.quit()
elif key == CONTROLS["PAUSE"]: elif key == self.config["CONTROLS"]["PAUSE"]:
self.pause() self.pause()
elif key == CONTROLS["HOLD"]: elif key == self.config["CONTROLS"]["HOLD"]:
self.swap() self.swap()
elif key == CONTROLS["MOVE LEFT"]: elif key == self.config["CONTROLS"]["MOVE LEFT"]:
self.matrix.piece.move(Movement.LEFT) self.matrix.piece.move(Movement.LEFT)
elif key == CONTROLS["MOVE RIGHT"]: elif key == self.config["CONTROLS"]["MOVE RIGHT"]:
self.matrix.piece.move(Movement.RIGHT) self.matrix.piece.move(Movement.RIGHT)
elif key == CONTROLS["SOFT DROP"]: elif key == self.config["CONTROLS"]["SOFT DROP"]:
self.matrix.piece.soft_drop() self.matrix.piece.soft_drop()
elif key == CONTROLS["ROTATE COUNTER"]: elif key == self.config["CONTROLS"]["ROTATE COUNTER"]:
self.matrix.piece.rotate(Rotation.COUNTERCLOCKWISE) self.matrix.piece.rotate(Rotation.COUNTERCLOCKWISE)
elif key == CONTROLS["ROTATE CLOCKWISE"]: elif key == self.config["CONTROLS"]["ROTATE CLOCKWISE"]:
self.matrix.piece.rotate(Rotation.CLOCKWISE) self.matrix.piece.rotate(Rotation.CLOCKWISE)
elif key == CONTROLS["HARD DROP"]: elif key == self.config["CONTROLS"]["HARD DROP"]:
self.matrix.piece.hard_drop() self.matrix.piece.hard_drop()
def pause(self): def pause(self):
@ -616,10 +639,10 @@ class Game:
self.scr.nodelay(False) self.scr.nodelay(False)
while True: while True:
key = self.scr.getkey() key = self.scr.getkey()
if key == CONTROLS["QUIT"]: if key == self.config["CONTROLS"]["QUIT"]:
self.quit() self.quit()
break break
elif key == CONTROLS["PAUSE"]: elif key == self.config["CONTROLS"]["PAUSE"]:
self.scr.nodelay(True) self.scr.nodelay(True)
self.hold.refresh() self.hold.refresh()
self.matrix.refresh() self.matrix.refresh()
@ -649,9 +672,9 @@ class Game:
self.matrix.window.addstr(11, 9, "OVER", curses.A_BOLD) self.matrix.window.addstr(11, 9, "OVER", curses.A_BOLD)
self.matrix.window.refresh() self.matrix.window.refresh()
self.scr.nodelay(False) self.scr.nodelay(False)
while self.scr.getkey() != CONTROLS["QUIT"]: while self.scr.getkey() != self.config["CONTROLS"]["QUIT"]:
pass pass
quit() self.quit()
def quit(self): def quit(self):
self.playing = False self.playing = False
@ -664,6 +687,7 @@ class Game:
if self.stats.clock_timer: if self.stats.clock_timer:
self.scheduler.cancel(self.stats.clock_timer) self.scheduler.cancel(self.stats.clock_timer)
self.stats.clock_timer = None self.stats.clock_timer = None
self.stats.save()
def main(): def main():
@ -680,10 +704,15 @@ def main():
level = min(15, level) level = min(15, level)
else: else:
level = 1 level = 1
try:
os.mkdir(dir_path)
except FileExistsError:
pass
with Screen() as scr: with Screen() as scr:
game = Game(scr, level) game = Game(scr, level)
game.play() game.play()
game.stats.save()
if __name__ == "__main__": if __name__ == "__main__":