-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_ascii_art.py
88 lines (71 loc) · 3.1 KB
/
make_ascii_art.py
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
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame as pg
BLACK = chr(9608) # █
WHITE = chr(9617) # ░
class Board:
def __init__(self, size):
self.size = size
self.set_board()
def set_board(self):
self.board = []
for row in range(self.size[0]):
row = []
for col in range(self.size[1]):
row.append(1)
self.board.append(row)
def get_size(self):
return self.size
def get_value(self, index):
return self.board[index[0]][index[1]]
def handle_click(self, index, black, white):
if white:
self.board[index[0]][index[1]] = 1
if black:
self.board[index[0]][index[1]] = 0
def save_board(self):
with open("ascii_art.txt", "w", encoding="utf-8") as file:
for i in range(self.size[0]):
line = "".join(list(map(str, self.board[i]))) + "\n"
file.write(line.replace("0", BLACK).replace("1", WHITE))
class Interface:
def __init__(self, board, screen_size):
self.board = board
self.piece_size = screen_size[0] // self.board.get_size()[1], screen_size[1] // self.board.get_size()[0]
self.screen_size = (self.piece_size[0] * self.board.get_size()[1], self.piece_size[1] * self.board.get_size()[0])
def run(self):
pg.init()
self.screen = pg.display.set_mode(self.screen_size)
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if pg.mouse.get_pressed()[0] or pg.mouse.get_pressed()[2]:
position = pg.mouse.get_pos()
left_click, middle_click, right_click = pg.mouse.get_pressed(3)
index = self.get_index_of_position(position)
if index:
self.handle_click(index, left_click, right_click)
self.draw()
pg.display.flip()
self.board.save_board()
pg.quit()
def draw(self):
top_left = (0, 0)
for row in range(self.board.get_size()[0]):
for col in range(self.board.get_size()[1]):
color = "white" if self.board.get_value((row, col)) else "black"
pg.draw.rect(self.screen, color, pg.Rect(top_left[0], top_left[1], self.piece_size[0], self.piece_size[1]))
top_left = top_left[0] + self.piece_size[0], top_left[1]
top_left = 0, top_left[1] + self.piece_size[1]
def get_index_of_position(self, position):
index = position[1] // self.piece_size[1], position[0] // self.piece_size[0]
if index[1] >= self.board.get_size()[1] or index[1] < 0 or index[0] >= self.board.get_size()[0] or index[0] < 0:
return None
return index
def handle_click(self, index, left_click, right_click):
self.board.handle_click(index, left_click, right_click)
if __name__ == "__main__":
window = Interface(Board((20, 46)), (600, 600)) # max board width (second number): 46
window.run()