roguepy/terminal.py

44 lines
1023 B
Python

import curses
KEYCODES = {
curses.KEY_LEFT: "KEY_LEFT",
curses.KEY_RIGHT: "KEY_RIGHT",
curses.KEY_UP: "KEY_UP",
curses.KEY_DOWN: "KEY_DOWN"
}
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 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.addch(y, x, char)