-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetchingDrawer.py
More file actions
137 lines (115 loc) · 5.19 KB
/
etchingDrawer.py
File metadata and controls
137 lines (115 loc) · 5.19 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""An art program that draws a continuous line around the screen using the
WASD keys. Inspired by Etch A Sketch toys.
Try drawing a Hilbert Curve fractal with:
RUN SDWDDSASDSAAWASSDSASSDWDSDWWAWDDDSASSDWDSDWWAWDWWASAAWDWAWDDSDW
Larger hilbert curv fractal with:
DDSAASSDDWDDSDDWWAAWDDDDSDDWDDDDSAASDDSAAAAWAASSSDDWDDDDSAASDDSAAAAWA
ASAAAAWDDWWAASAAWAASSDDSAASSDDWDDDDSAASDDSAAAAWAASSDDSAASSDDWDDSDDWWA
AWDDDDDDSAASSDDWDDSDDWWAAWDDWWAASAAAAWDDWAAWDDDDSDDWDDSDDWDDDDSAASDDS
AAAAWAASSDDSAASSDDWDDSDDWWAAWDDDDDDSAASSDDWDDSDDWWAAWDDWWAASAAAAWDDWA
AWDDDDSDDWWAAWDDWWAASAAWAASSDDSAAAAWAASAAAAWDDWAAWDDDDSDDWWWAASAAAAWD
DWAAWDDDDSDDWDDDDSAASSDDWDDSDDWWAAWDD
"""
import shutil, sys
# ─── Line Characters ────────────────────────────────────────────────────────────
UP_DOWN_CHAR = chr(9474) # │
LEFT_RIGHT_CHAR = chr(9472) # ─
DOWN_RIGHT_CHAR = chr(9484) # ┌
DOWN_LEFT_CHAR = chr(9488) # ┐
UP_RIGHT_CHAR = chr(9492) # └
UP_LEFT_CHAR = chr(9496) # ┘
UP_DOWN_RIGHT_CHAR = chr(9500) # ├
UP_DOWN_LEFT_CHAR = chr(9508) # ┤
DOWN_LEFT_RIGHT_CHAR = chr(9516) # ┬
UP_LEFT_RIGHT_CHAR = chr(9524) # ┴
CROSS_CHAR = chr(9532) # ┼
REVERSE_DIRECTION = {'W': 'S', 'S': 'W', 'A': 'D', 'D': 'A'}
#using frozenset([]): as a dictionary key for mapping direction sets to characters
LINE_CHARS = {
frozenset(['W', 'S']): UP_DOWN_CHAR,
frozenset(['A', 'D']): LEFT_RIGHT_CHAR,
frozenset(['S', 'D']): DOWN_RIGHT_CHAR,
frozenset(['A', 'S']): DOWN_LEFT_CHAR,
frozenset(['W', 'D']): UP_RIGHT_CHAR,
frozenset(['W', 'A']): UP_LEFT_CHAR,
frozenset(['W', 'S', 'D']): UP_DOWN_RIGHT_CHAR,
frozenset(['W', 'S', 'A']): UP_DOWN_LEFT_CHAR,
frozenset(['A', 'S', 'D']): DOWN_LEFT_RIGHT_CHAR,
frozenset(['W', 'A', 'D']): UP_LEFT_RIGHT_CHAR,
frozenset(['W', 'A', 'S', 'D']): CROSS_CHAR,
}
# ─── Canvas Setup ───────────────────────────────────────────────────────────────
CANVAS_WIDTH, CANVAS_HEIGHT = shutil.get_terminal_size()
CANVAS_WIDTH -= 1
CANVAS_HEIGHT -= 5
canvas = {}
cursorX = 0
cursorY = 0
moves = []
# ─── Drawing Logic ──────────────────────────────────────────────────────────────
def getCanvasString(canvasData, cx, cy):
canvasStr = ''
for rowNum in range(CANVAS_HEIGHT):
for columnNum in range(CANVAS_WIDTH):
if columnNum == cx and rowNum == cy:
canvasStr += '#'
else:
cell = canvasData.get((columnNum, rowNum))
canvasStr += LINE_CHARS.get(frozenset(cell) if cell else frozenset(), ' ')
canvasStr += '\n'
return canvasStr
def moveCursor(command):
global cursorX, cursorY
if command not in REVERSE_DIRECTION:
return
moves.append(command)
if not canvas:
canvas[(cursorX, cursorY)] = {command, REVERSE_DIRECTION[command]}
canvas[(cursorX, cursorY)].add(command)
if command == 'W' and cursorY > 0:
cursorY -= 1
elif command == 'S' and cursorY < CANVAS_HEIGHT - 1:
cursorY += 1
elif command == 'A' and cursorX > 0:
cursorX -= 1
elif command == 'D' and cursorX < CANVAS_WIDTH - 1:
cursorX += 1
else:
return
canvas.setdefault((cursorX, cursorY), set()).add(REVERSE_DIRECTION[command])
# ─── Main Loop ──────────────────────────────────────────────────────────────────
while True:
print(getCanvasString(canvas, cursorX, cursorY))
print('WASD to move, H for help, C to clear, F to save, RUN <moves>, or QUIT.')
response = input('> ').upper()
if response == 'QUIT':
print('Thanks for playing!')
sys.exit()
elif response == 'H':
print('Use W, A, S, D to move and draw. Example: DDD draws right.')
print('Use RUN followed by a string to auto-draw. Example:')
print('RUN SDWDDSASDSAAWASSDSASSDWDSDWWAWDDDSASSDWDSDWWAWDWWASAAWDWAWDDSDW')
print('Use F to save your drawing to a file.')
input('Press Enter to continue...')
continue
elif response == 'C':
canvas = {}
moves.append('C')
elif response == 'F':
try:
print('Enter filename to save:')
filename = input('> ')
if not filename.endswith('.txt'):
filename += '.txt'
with open(filename, 'w', encoding='utf-8') as file:
file.write(''.join(moves) + '\n')
file.write(getCanvasString(canvas, None, None))
except Exception as e:
print(f'ERROR: Could not save file. {e}')
elif response.startswith('RUN '):
for command in response[4:]:
moveCursor(command)
continue
else:
for command in response:
moveCursor(command)