Repository navigation
Expand file tree
/
Copy pathborder_ext.py
More file actions
executable file
·110 lines (80 loc) · 2.15 KB
/
Copy pathborder_ext.py
File metadata and controls
executable file
·110 lines (80 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Testing out the curses lib.
"""
import curses
def debug(msg):
"""
Ease debugging with curses by writing a message to file.
"""
with open("debug.txt", "a") as f:
f.write(msg + "\n")
def dump(scr, startX, startY, endX, endY):
"""
Dump content of part of screen, do not output empty content.
"""
with open("output.txt", "w") as f:
for y in range(startY, endY):
for x in range(startX, endX):
# Get the character from screen
ch = scr.inch(y, x)
debug(str(x) + " - " + str(y) + ": " + str(ch))
# Output only if not space
if ch != 32:
ch = chr(ch & 0x00ff)
f.write(ch)
def main(scr):
"""
Draw a border around the screen, move around using the cursor and leave a mark
of the latest pressed character on the keyboard.
Perhaps you could make a nice painting using asciart?
Quit using 'q'.
"""
# Clear the screen of any output
scr.clear()
# Get screen dimensions
y1, x1 = scr.getmaxyx()
y1 -= 1
x1 -= 1
y0, x0 = 0, 0
# Get center position
yc, xc = (y1-y0)//2, (x1-x0)//2
# Draw a border
scr.border()
# Move cursor to center
scr.move(yc, xc)
# Refresh to draw out
scr.refresh()
# Main loop
x = xc
y = yc
ch = 'o'
while True:
key = scr.getkey()
if key == 'q':
break
elif key == 'o':
dump(scr, 1, 1, x1, y1)
break
elif key == 'KEY_UP':
y -= 1
elif key == 'KEY_DOWN':
y += 1
elif key == 'KEY_LEFT':
x -= 1
elif key == 'KEY_RIGHT':
x += 1
else:
ch = key
# Draw out the char at cursor positino
scr.addstr(ch)
# Move cursor to new position
scr.move(y, x)
# Redraw all items on the screen
scr.refresh()
if __name__ == "__main__":
print(__doc__)
print(main.__doc__)
input("Press enter to begin playing...")
curses.wrapper(main)