-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
197 lines (148 loc) · 6.03 KB
/
main.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import pygame, sys
from pygame.locals import *
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
DARKGRAY = (169, 169, 169)
YELLOW = (222, 178, 0)
PINK = (225, 96, 253)
BLUE = (0, 0, 255)
BROWN = (139, 69, 19)
ORANGE = (255, 99, 71)
GRAY = (119, 136, 153)
LIGHTORANGE = (255, 176, 56)
INTERMEDIARYORANGE = (255, 154, 0)
LIGHTBLUE = (60, 170, 255)
DARKBLUE = (0, 101, 178)
BEIGE = (178, 168, 152)
WIDTH = 800
HEIGHT = WIDTH
SCREEN_SIZE = (WIDTH, HEIGHT)
RECT_QTT = 50
LEFT_CLICK = 1
SCROLL_CLICK = 4
RIGHT_CLICK = 3
class Game():
def __init__(self):
try:
pygame.init()
except:
print('The pygame module did not start successfully')
self.background = pygame.display.set_mode(SCREEN_SIZE)
pygame.mouse.set_cursor(*pygame.cursors.broken_x)
pygame.display.set_caption("Paint")
self.option_draw = False
self.brush_color = GREEN
self.brush_size = WIDTH // RECT_QTT
def run(self):
exit = False
while not exit:
self.mouse_position = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT:
exit = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_TAB:
self.option_draw = not self.option_draw
# Change pencil color
elif event.key == K_r:
self.brush_color = RED
elif event.key == K_b:
self.brush_color = BLUE
elif event.key == K_o:
self.brush_color = ORANGE
elif event.key == K_g:
self.brush_color = GREEN
elif event.key == K_y:
self.brush_color = YELLOW
# eraser
elif event.key == K_e:
self.brush_color = BLACK
elif event.type == MOUSEBUTTONDOWN:
if(event.button == RIGHT_CLICK):
# fill using bfs algorithm
print("Running BFS")
self.option_draw = False
if self.background.get_at(self.mouse_position) != self.brush_color:
self.bfs(self.mouse_position, self.background.get_at(self.mouse_position))
print("End BFS")
elif(event.button == LEFT_CLICK):
# fill using dfs algorithm
# brush min size = 20 (because recursion qtt)
print("Running DFS")
self.option_draw = False
if self.background.get_at(self.mouse_position) != self.brush_color:
self.dfs(self.mouse_position, self.background.get_at(self.mouse_position))
print("End DFS")
# Drawing dot when mousebuttondown
if self.option_draw:
self.draw_rect_in_position(self.mouse_position)
pygame.display.update()
pygame.quit()
sys.exit(0)
def bfs(self, s, color):
queue = []
neighbors = []
self.draw_rect_in_position(s)
queue.append(s)
while queue:
neighbors.clear()
s = queue.pop()
x, y = s
# neighbors.append((x + self.brush_size + 1, y + self.brush_size + 1))
# neighbors.append((x - self.brush_size - 1, y + self.brush_size + 1))
# neighbors.append((x + self.brush_size + 1, y - self.brush_size - 1))
# neighbors.append((x - self.brush_size - 1, y - self.brush_size - 1))
neighbors.append((x + self.brush_size, y))
neighbors.append((x - self.brush_size, y))
neighbors.append((x, y - self.brush_size))
neighbors.append((x, y + self.brush_size))
for neighbor in neighbors:
if self.verifyMargin(neighbor) and self.getColor(neighbor) == color:
queue.append(neighbor)
self.draw_rect_in_position(neighbor)
def dfs(self, s, color):
if self.getColor(s) == color:
stack = []
neighbors = []
stack.append(s)
while (stack):
node = stack.pop()
x, y = node
neighbors.clear()
# neighbors.append((x + self.brush_size + 1, y + self.brush_size + 1))
# neighbors.append((x - self.brush_size, y + self.brush_size + 1))
# neighbors.append((x + self.brush_size + 1, y - self.brush_size))
# neighbors.append((x - self.brush_size, y - self.brush_size))
neighbors.append((x + self.brush_size, y))
neighbors.append((x - self.brush_size, y))
neighbors.append((x, y - self.brush_size))
neighbors.append((x, y + self.brush_size))
self.draw_rect_in_position(node)
for neighbor in neighbors:
if self.verifyMargin(neighbor) and self.getColor(neighbor) == color and neighbor:
stack.append(neighbor)
def draw_rect_in_position(self, s):
x, y = s
position_x = self.brush_size * (x // self.brush_size)
position_y = self.brush_size * (y // self.brush_size)
rect = pygame.Rect(position_x, position_y, self.brush_size, self.brush_size)
pygame.draw.rect(self.background, self.brush_color, rect)
def getColor(self, position):
x, y = position
return self.background.get_at((x, y))
def verifyMargin(self, s):
x, y = s
return x >= 0 and x < WIDTH and y >= 0 and y < HEIGHT
def main():
mygame = Game()
mygame.run()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interruption')