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
Another Tetris clone... again... but for terminal. Ideal for servers without GUI!
Tetris clone for terminal. Ideal for servers without GUI!
## Installation
## Screenshot
```bash
┌────────────HOLD───────────┐┌────────────────────┐┌────────────NEXT───────────┐
│ ││██ ││ │
│ ██ ││██ ││ ██ │
│ ██████ ││██ ││ ██████ │
│ ││██ ││ │
└───────────────────────────┘│ ██ │└───────────────────────────┘
┌────────────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 │
│ ││ ██████████████████││ │
│ ││ ██████████████████││ │
│ ││ ██████████████████││ │
│ ││ ██████████████████││ │
└───────────────────────────┘└────────────────────┘└───────────────────────────┘
pip install --user terminis
```
## Usage
```bash
python terminis.py [level]
terminis [level]
```
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
Edit values of dictionary CONTROLS in the script:
```python
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"
}
```
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))
You can change keys by editing:
* `%appdata%\Terminis\config.cfg` on Windows
* `~/.local/share/Terminis/config.cfg` on Linux
Acceptable values:
* printable characters ('q', '*', ' '...)
* 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 time
import os
import configparser
# You can change controls here.
# Acceptable values are printable characters ('q', 'w'...) and curses's constants name starting with "KEY_"
# 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"
}
WIN_DIR = "~/Appdata/Roaming/Terminis/"
LINUX_DIR = "~/.local/share/"
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:
@ -312,6 +305,7 @@ class Window:
if self.TITLE:
self.title_begin_x = (width-len(self.TITLE)) // 2 + 1
self.piece = None
self.refresh()
def draw_border(self):
self.window.erase()
@ -345,12 +339,12 @@ class Matrix(Window):
def __init__(self, game, begin_x, begin_y):
begin_x += (game.WIDTH - self.WIDTH) // 2
begin_y += (game.HEIGHT - self.HEIGHT) // 2
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y)
self.game = game
self.cells = [
[None for x in range(self.NB_COLS)]
for y in range(self.NB_LINES)
]
Window.__init__(self, self.WIDTH, self.HEIGHT, begin_x, begin_y)
def refresh(self, paused=False):
self.draw_border()
@ -386,35 +380,26 @@ class Matrix(Window):
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"
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"
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):
@ -427,23 +412,28 @@ class Stats(Window):
)
LINES_CLEARED_NAMES = ("", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS")
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):
Window.__init__(self, width, height, begin_x, begin_y)
self.game = game
self.width = width
self.height = height
self.level = level - 1
self.goal = 0
self.score = 0
try:
with open(FILE, "r") as f:
self.high_score = int(f.read())
except:
self.high_score = 0
self.load()
self.time = time.time()
self.lines_cleared = 0
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):
self.draw_border()
@ -509,24 +499,60 @@ class Stats(Window):
def save(self):
try:
with open(FILE, mode='w') as f:
with open(self.file_path, mode='w') as f:
f.write(str(self.high_score))
except:
pass
except Exception as e:
print("High score could not be saved:")
print(e)
class Controls(Window):
class Config(Window, configparser.SafeConfigParser):
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):
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 == " ":
key = "SPACE"
else:
key = key.replace("KEY_", "")
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()
class Game:
@ -548,17 +574,12 @@ class Game:
self.next = Next(side_width, right_x, top_y)
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.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.paused = False
self.hold.refresh()
self.matrix.refresh()
self.next.refresh()
self.stats.refresh()
self.controls.refresh()
self.stats.new_level()
self.new_piece()
def random_piece(self):
if not self.random_bag:
self.random_bag = [O, I, T, L, J, S, Z]
@ -582,30 +603,32 @@ class Game:
while self.playing:
self.scheduler.run()
def process_input(self, _):
try:
key = self.scr.getkey()
except curses.error:
return
else:
if key == CONTROLS["QUIT"]:
self.quit()
elif key == CONTROLS["PAUSE"]:
self.pause()
elif key == CONTROLS["HOLD"]:
self.swap()
elif key == CONTROLS["MOVE LEFT"]:
self.matrix.piece.move(Movement.LEFT)
elif key == CONTROLS["MOVE RIGHT"]:
self.matrix.piece.move(Movement.RIGHT)
elif key == CONTROLS["SOFT DROP"]:
self.matrix.piece.soft_drop()
elif key == CONTROLS["ROTATE COUNTER"]:
self.matrix.piece.rotate(Rotation.COUNTERCLOCKWISE)
elif key == CONTROLS["ROTATE CLOCKWISE"]:
self.matrix.piece.rotate(Rotation.CLOCKWISE)
elif key == CONTROLS["HARD DROP"]:
self.matrix.piece.hard_drop()
def process_input(self, delay):
end = time.time() + delay
while self.playing and time.time() < end:
try:
key = self.scr.getkey()
except curses.error:
return
else:
if key == self.config["CONTROLS"]["QUIT"]:
self.quit()
elif key == self.config["CONTROLS"]["PAUSE"]:
self.pause()
elif key == self.config["CONTROLS"]["HOLD"]:
self.swap()
elif key == self.config["CONTROLS"]["MOVE LEFT"]:
self.matrix.piece.move(Movement.LEFT)
elif key == self.config["CONTROLS"]["MOVE RIGHT"]:
self.matrix.piece.move(Movement.RIGHT)
elif key == self.config["CONTROLS"]["SOFT DROP"]:
self.matrix.piece.soft_drop()
elif key == self.config["CONTROLS"]["ROTATE COUNTER"]:
self.matrix.piece.rotate(Rotation.COUNTERCLOCKWISE)
elif key == self.config["CONTROLS"]["ROTATE CLOCKWISE"]:
self.matrix.piece.rotate(Rotation.CLOCKWISE)
elif key == self.config["CONTROLS"]["HARD DROP"]:
self.matrix.piece.hard_drop()
def pause(self):
pause_time = time.time()
@ -616,10 +639,10 @@ class Game:
self.scr.nodelay(False)
while True:
key = self.scr.getkey()
if key == CONTROLS["QUIT"]:
if key == self.config["CONTROLS"]["QUIT"]:
self.quit()
break
elif key == CONTROLS["PAUSE"]:
elif key == self.config["CONTROLS"]["PAUSE"]:
self.scr.nodelay(True)
self.hold.refresh()
self.matrix.refresh()
@ -649,9 +672,9 @@ class Game:
self.matrix.window.addstr(11, 9, "OVER", curses.A_BOLD)
self.matrix.window.refresh()
self.scr.nodelay(False)
while self.scr.getkey() != CONTROLS["QUIT"]:
while self.scr.getkey() != self.config["CONTROLS"]["QUIT"]:
pass
quit()
self.quit()
def quit(self):
self.playing = False
@ -664,6 +687,7 @@ class Game:
if self.stats.clock_timer:
self.scheduler.cancel(self.stats.clock_timer)
self.stats.clock_timer = None
self.stats.save()
def main():
@ -680,10 +704,15 @@ def main():
level = min(15, level)
else:
level = 1
try:
os.mkdir(dir_path)
except FileExistsError:
pass
with Screen() as scr:
game = Game(scr, level)
game.play()
game.stats.save()
if __name__ == "__main__":