console_snake/frame.py

77 lines
2.2 KiB
Python
Raw Normal View History

2022-10-26 21:57:29 +03:00
from settings import *
2022-10-26 22:40:18 +03:00
from sys import platform
if platform == "win32":
from ctypes import *
STD_OUTPUT_HANDLE = -11
STDHANDLE = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
class COORDSET(Structure):
_fields_ = [("X", c_long), ("Y", c_long)]
2022-10-27 15:29:28 +03:00
def set_cursor_position(x: int, y: int) -> None:
2022-10-26 22:40:18 +03:00
windll.kernel32.SetConsoleCursorPosition(STDHANDLE, COORDSET(x, y))
else:
2022-10-27 15:29:28 +03:00
def set_cursor_position(x: int, y: int) -> None:
2022-10-26 22:40:18 +03:00
print(f"\033[{x};{y}H")
2022-10-26 21:57:29 +03:00
class Frame:
def __init__(self, width: int, height: int) -> None:
self.width = width
self.height = height
self.matrix = [[SPACE] * width for line in range(height)]
def __str__(self) -> str:
2022-10-29 04:25:09 +03:00
return (
f"Width:\n {self.width}\n"
f"Height:\n {self.height}"
)
2022-10-26 21:57:29 +03:00
def draw(
2022-10-29 04:25:09 +03:00
self,
x: int,
y: int,
2022-10-26 21:57:29 +03:00
value: int = WALL,
2022-10-29 04:25:09 +03:00
width: int = 1,
height: int = 1
2022-10-26 21:57:29 +03:00
) -> None:
if not isinstance(value, int):
raise TypeError("Value must be int")
for line in range(height):
for column in range(width):
self.matrix[y + line][x + column] = value
def show(self) -> None:
2022-10-27 15:29:28 +03:00
set_cursor_position(0, 0)
2022-10-26 21:57:29 +03:00
2022-10-29 04:25:09 +03:00
out_string = f"{WALL_COLOR}{'' * (self.width * 2)}{RESET_COLOR}\n"
2022-10-26 21:57:29 +03:00
for line in self.matrix:
to_str = ''
2022-10-29 04:25:09 +03:00
to_str += f'{WALL_COLOR}{RESET_COLOR}'
2022-10-26 21:57:29 +03:00
for elem in line:
if elem == SPACE:
2022-10-27 17:03:11 +03:00
to_str += TT_SPACE
2022-10-26 21:57:29 +03:00
elif elem == WALL:
2022-10-27 17:03:11 +03:00
to_str += TT_WALL
2022-10-26 21:57:29 +03:00
elif elem == FOOD:
2022-10-27 17:03:11 +03:00
to_str += TT_FOOD
2022-10-26 21:57:29 +03:00
elif elem == WALL_FOOD:
2022-10-27 17:03:11 +03:00
to_str += TT_WALL_FOOD
elif elem == HEAD:
to_str += TT_HEAD
2022-10-29 04:25:09 +03:00
to_str += f'{WALL_COLOR}{RESET_COLOR}\n'
2022-10-26 21:57:29 +03:00
out_string += to_str
2022-10-29 04:25:09 +03:00
out_string += f"{WALL_COLOR}{'' * (self.width * 2)}{RESET_COLOR}"
2022-10-26 21:57:29 +03:00
print(out_string)
def see(self, x, y) -> str:
return self.matrix[y][x]