Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
zmx0142857
GitHub Repository: zmx0142857/mini-games
Path: blob/master/py/game.py
363 views
1
class Console:
2
def __init__(self, line_msg=None, line_input=None):
3
self.line_msg = line_msg
4
self.line_input = line_input
5
6
def cursor_up(self, n):
7
print('\033[%dA' % n, end='')
8
9
def cursor_down(self, n):
10
print('\033[%dB' % n, end='')
11
12
def cursor_right(self, n):
13
print('\033[%dC' % n, end='')
14
15
def cursor_left(self, n):
16
print('\033[%dD' % n, end='')
17
18
def cursor_goto(self, l, c=0):
19
print('\033[%d;%dH' % (l, c), end='')
20
# or '\033%d;%df' % (l, c)
21
22
def screen_clear(self):
23
print('\033[2J', end='')
24
self.cursor_goto(0, 0)
25
26
def line_clear(self, l=None):
27
if l != None:
28
self.cursor_goto(l)
29
print('\033[K', end='')
30
31
def input(self, prompt):
32
if self.line_input != None:
33
self.line_clear(self.line_input)
34
s = input(prompt)
35
if self.line_msg != None:
36
self.line_clear(self.line_msg)
37
return s
38
39
def msg(self, *s):
40
if self.line_msg != None:
41
self.line_clear(self.line_msg)
42
print(*s)
43
44
def yes(self, prompt):
45
try:
46
self.line_clear(self.line_input)
47
s = input(prompt + ' (y/n) ')
48
except Exception:
49
return False
50
return len(s) != 0 and (s[0] == 'y' or s[0] == 'Y')
51
52