console_tetris/screen.py

45 lines
1.2 KiB
Python
Raw Permalink Normal View History

from typing import List, Any
from sys import platform
if platform == "win32":
from ctypes import windll, c_long, Structure
STD_OUTPUT_HANDLE = -11
STDHANDLE = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
class COORDSET(Structure):
_fields_ = [("X", c_long), ("Y", c_long)]
def set_cursor_position(x: int, y: int) -> None:
windll.kernel32.SetConsoleCursorPosition(STDHANDLE, COORDSET(x, y))
else:
def set_cursor_position(x: int, y: int) -> None:
print(f"\033[{x};{y}H")
class Screen:
def __init__(
self,
width: int,
height: int,
fill_object: Any = None
) -> None:
self.width: int = width
self.height: int = height
self.frame: List[List[Any]] = \
[[fill_object] * width for line in range(height)]
def __getitem__(self, key: int) -> List[Any]:
return self.frame[key]
def fill(self, fill_object: Any = None) -> None:
self.frame: List[List[Any]] = \
[[fill_object] * width for line in range(height)]
def print(self) -> None:
set_cursor_position(0, 0)
for line in self.frame:
for point in line:
print(point, end='')
print()