import curses from typing import Tuple KEYCODES = { curses.KEY_LEFT: "←", curses.KEY_RIGHT: "→", curses.KEY_UP: "↑", curses.KEY_DOWN: "↓", curses.KEY_RESIZE: "KEY_RESIZE" } class Terminal: def __init__(self): """ Perfoms some initial steps to set the terminal ready for the game """ self.scr = curses.initscr() curses.noecho() curses.cbreak() curses.curs_set(0) self.scr.keypad(True) def __del__(self): """ Perfoms some cleanup to restore the behaviour of the terminal """ curses.nocbreak() self.scr.keypad(False) curses.echo() curses.endwin() def clear(self): self.scr.erase() def get_key(self) -> str: """ Blocks until a key is read """ ch = self.scr.getch() if ch in KEYCODES: return KEYCODES[ch] else: return chr(ch) def put_char(self, x, y, char): """ Prints a char at the specified position """ self.scr.insch(y, x, char) def put_string(self, x, y, string): """ Prints a string from the specified position """ self.scr.addstr(y, x, string) def get_dimensions(self) -> Tuple[int, int]: """ Returns the screen dimensions as (width, height) """ return self.scr.getmaxyx()[::-1]