-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
352 lines (295 loc) · 11.4 KB
/
utilities.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import itertools
import random
import tkinter as tk
from tkinter import filedialog, ttk
from typing import List, Optional, Tuple
from PIL import Image # type: ignore
from sprites import Sprites, blit_sprite, game_offset, pg, screen
sprites = Sprites().sprites
digit_to_str = {
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine"
}
class Block:
def __init__(self) -> None:
self.is_bomb = False
self.is_exploded = False
self.is_flagged = False
self.is_question_mark = False
self.is_revealed = False
self.number = 0
self.x = 0
self.y = 0
def draw(self) -> None:
wall = pg.surface.Surface((32, 32))
wall.fill((30, 30, 30))
screen.blit(wall, (self.x, self.y))
wall = pg.transform.scale(wall, (30, 30))
wall.fill((170, 170, 170))
screen.blit(wall, (self.x + 1, self.y + 1))
if self.is_revealed:
wall.fill((85, 85, 85))
screen.blit(wall, (self.x + 1, self.y + 1))
if self.is_exploded:
screen.blit(sprites["bomb_explode"], self.get_position())
elif self.is_bomb:
if self.is_flagged:
screen.blit(sprites["flag"], self.get_position())
else:
screen.blit(sprites["bomb"], self.get_position())
elif self.is_flagged:
screen.blit(sprites["bomb_no"], self.get_position())
elif self.number is not None and self.number != 0:
blit_sprite(sprites[digit_to_str[self.number]],
(30, 30, 30),
self.get_position(),
screen)
elif self.is_flagged:
screen.blit(sprites["flag"], self.get_position())
elif self.is_question_mark:
screen.blit(sprites["question_mark"], self.get_position())
else:
screen.blit(sprites["tile"], self.get_position())
def increase_number(self) -> None:
self.number += 1
def set_position(self, x: int, y: int) -> None:
self.x = x
self.y = y
def get_position(self) -> Tuple[int, int]:
return (self.x, self.y)
class Board:
def __init__(self, width: int, height: int, bomb_count: int) -> None:
self.board: List[List[Block]] = []
self.empty_tiles: List[Tuple[int, int]] = []
self.width = width
self.height = height
self.bomb_count = bomb_count
self.left_offset: int = game_offset
self.top_offset: int = game_offset
for i, y in enumerate(range(self.top_offset,
self.top_offset + self.height*32,
32)):
self.board.append([])
for j, x in enumerate(range(self.left_offset,
self.left_offset + self.width*32,
32)):
self.board[i].append(Block())
self.board[i][j].set_position(x, y)
for y in range(self.height):
self.empty_tiles.extend((x, y) for x in range(self.width))
self.generate_bombs()
self.generate_numbers()
self.game_over: Optional[str] = None
def generate_bombs(self) -> None:
bomb_count = self.bomb_count
if bomb_count > self.width * self.height:
self.bomb_count = self.width * self.height - 1
bomb_count = self.bomb_count
while bomb_count > 0:
x = random.randint(0, self.width - 1)
y = random.randint(0, self.height - 1)
if not self.board[y][x].is_bomb:
self.board[y][x].is_bomb = True
self.empty_tiles.remove((x, y))
bomb_count -= 1
def generate_numbers(self) -> None:
for y, x in itertools.product(range(self.height), range(self.width)):
if not self.board[y][x].is_bomb:
self.board[y][x].number = self.count_bombs(x, y)
def count_bombs(self, x: int, y: int) -> int:
return sum(bool((
0 <= x + i < self.width
and 0 <= y + j < self.height
and self.board[y + j][x + i].is_bomb
)) for i, j in itertools.product(range(-1, 2), range(-1, 2)))
def move_bomb(self, x: int, y: int) -> None:
self.board[y][x].is_bomb = False
for i, j in itertools.product(range(-1, 2), range(-1, 2)):
if (
0 <= x + i < self.width
and 0 <= y + j < self.height
):
self.board[y + j][x + i].number -= 1
self.board[y][x].number += 1
i = random.randint(0, len(self.empty_tiles) - 1)
x = self.empty_tiles[i][0]
y = self.empty_tiles[i][1]
self.board[y][x].is_bomb = True
for i, j in itertools.product(range(-1, 2), range(-1, 2)):
if (
0 <= x + i < self.width
and 0 <= y + j < self.height
):
self.board[y + j][x + i].number += 1
def draw(self) -> None:
for y, x in itertools.product(range(self.height), range(self.width)):
self.board[y][x].draw()
def reveal(self, x: int, y: int) -> None:
if self.board[y][x].is_bomb:
self.board[y][x].is_exploded = True
for x, y in itertools.product(range(self.width),
range(self.height)):
self.board[y][x].is_revealed = True
self.game_over = "LOSE"
self.board[y][x].is_revealed = True
if self.board[y][x].number == 0:
for i, j in itertools.product(range(-1, 2), range(-1, 2)):
if (
0 <= x + i < self.width
and 0 <= y + j < self.height
and not self.board[y + j][x + i].is_revealed
and not self.board[y + j][x + i].is_flagged
):
self.reveal(x + i, y + j)
def check_win(self) -> bool:
for y, x in itertools.product(range(self.height), range(self.width)):
if not self.board[y][x].is_revealed \
and not self.board[y][x].is_bomb:
return False
if self.game_over is None:
self.game_over = "WIN"
for x, y in itertools.product(range(self.width),
range(self.height)):
self.board[y][x].is_revealed = True
return True
return False
def window_resize(self) -> None:
for (i, y), (j, x) in itertools.product(
enumerate(range(self.top_offset,
self.top_offset + self.height*32,
32)),
enumerate(range(self.left_offset,
self.left_offset + self.width*32,
32))):
self.board[i][j].set_position(x, y)
class Smiley:
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
self.is_in_awe = False
self.is_dead = False
self.is_cool = False
self.sprite: Optional[pg.surface.Surface] = None
def draw(self, screen: pg.surface.Surface) -> None:
screen.blit(pg.transform.scale(sprites["tile"], (64, 64)),
self.get_position())
if self.is_in_awe:
self.sprite = sprites["smiley_wow"]
elif self.is_dead:
self.sprite = sprites["smiley_rip"]
elif self.is_cool:
self.sprite = sprites["smiley_yeah"]
else:
self.sprite = sprites["smiley"]
if self.sprite:
self.sprite = pg.transform.scale(self.sprite, (64, 64))
screen.blit(self.sprite, (self.x, self.y))
def set_reset(self) -> None:
self.is_dead = False
self.is_in_awe = False
self.is_cool = False
def set_in_awe(self) -> None:
self.is_in_awe = True
self.is_dead = False
self.is_cool = False
def set_dead(self) -> None:
self.is_in_awe = False
self.is_dead = True
self.is_cool = False
def set_cool(self) -> None:
self.is_in_awe = False
self.is_dead = False
self.is_cool = True
def set_position(self, x: int, y: int) -> None:
self.x = x
self.y = y
def get_position(self) -> Tuple[int, int]:
return (self.x, self.y)
class Timer:
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
self.number = 0
self.sprite: Optional[pg.surface.Surface] = None
def draw(self, screen: pg.surface.Surface) -> None:
wall = pg.surface.Surface((32, 64))
wall.fill((30, 30, 30))
screen.blit(wall, (self.x, self.y))
wall = pg.transform.scale(wall, (30, 62))
wall.fill((170, 170, 170))
screen.blit(wall, (self.x + 1, self.y + 1))
if self.number is not None:
self.sprite = sprites[f"clock_{digit_to_str[self.number]}"]
if self.sprite:
self.sprite = pg.transform.scale(self.sprite, (32, 64))
screen.blit(self.sprite, (self.x, self.y))
def set_position(self, x: int, y: int) -> None:
self.x = x
self.y = y
def get_position(self) -> Tuple[int, int]:
return (self.x, self.y)
class Button:
def __init__(self, x: int, y: int, width: int, height: int) -> None:
self.x = x
self.y = y
self.width = width
self.height = height
self.button_clicked = 0
def draw(self,
screen: pg.surface.Surface,
text: str,
text_color: Tuple[int, int, int],
button_color: Tuple[int, int, int],
font: pg.font.Font) -> None:
button_text = font.render(text, True, text_color)
pg.draw.rect(screen,
(150, 50, 30),
[self.x - 2, self.y - 2,
self.width + 4, self.height + 4])
pg.draw.rect(screen,
button_color,
[self.x, self.y,
self.width, self.height])
screen.blit(button_text,
(self.x + self.width//2 - button_text.get_width()//2,
self.y + self.height//2 - button_text.get_height()//2))
def is_mouse_over(self, mouse_pos: Tuple[int, int]) -> bool:
return (mouse_pos[0] >= self.x and
mouse_pos[0] <= self.x + self.width and
mouse_pos[1] >= self.y and
mouse_pos[1] <= self.y + self.height)
def set_position(self, x: int, y: int) -> None:
self.x = x
self.y = y
def get_position(self) -> Tuple[int, int]:
return (self.x, self.y)
def prompt_file(icon_path: Optional[str] = None) -> str:
top = tk.Tk()
top.withdraw() # hide window
top.option_add("*foreground", "black")
style = ttk.Style(top)
if icon_path:
top.iconbitmap(tk.PhotoImage(icon_path))
else:
top.iconwindow()
file_name = filedialog.askopenfilename(parent=top, filetypes=[("JSON", "*.json")])
top.destroy()
return file_name
def png_to_ico(inp: str = "textures/icon.png", out: str = "icon.ico") -> None:
img = Image.open(inp)
icon_sizes = [(16, 16), (24, 24), (32, 32),
(48, 48), (64, 64), (128, 128),
(255, 255)]
img.save(out, sizes=icon_sizes)
def main() -> None:
pass
if __name__ == "__main__":
main()