roguepy/terminal.py

61 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-03-01 19:47:24 +00:00
import curses
2023-03-21 18:36:19 +00:00
from typing import Tuple
2023-03-01 19:47:24 +00:00
KEYCODES = {
2023-03-22 17:17:44 +00:00
curses.KEY_LEFT: "",
curses.KEY_RIGHT: "",
curses.KEY_UP: "",
curses.KEY_DOWN: "",
curses.KEY_RESIZE: "KEY_RESIZE"
2023-03-01 19:47:24 +00:00
}
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()
2023-03-21 18:36:19 +00:00
def clear(self):
self.scr.erase()
2023-03-01 19:47:24 +00:00
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
"""
2023-03-21 18:36:19 +00:00
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]