-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.py
More file actions
393 lines (346 loc) · 13 KB
/
minesweeper.py
File metadata and controls
393 lines (346 loc) · 13 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import pygame
import random
import time
import threading
# Короче создать окно и прочее
pygame.init()
screen = pygame.display.set_mode((500, 600))
clock = pygame.time.Clock()
# Данные об нажатии на мышку (Для того чтобы небыло так что ты зажал и код выполнялся 100000 раз)
class TapData:
def __init__(self):
self.value = False
self.data = None
def generategrid():
global grid, mines
grid = [] # Создать сетку
for i in range(100):
grid.append(Tile()) # Заполнить пустыми клетками
mines = 10
# Поставить мины
for tile in random.sample(grid, 10):
tile.content = "m"
# Поставить цифры
for i, tile in enumerate(grid):
if grid[i].content == "m":
continue
grid[i].content = checksurround(i)
def reset_tapped():
global tapped
# Сброс данных о нажатии
tapped = [TapData(), TapData()]
# Перезапуск игры
def startgame():
global gameover, tick, tapped, state, running
generategrid()
gameover = None
tick = 0
reset_tapped()
state = "smile_neutral"
running = True
# Загрузить шрифты, спрайты и звуки
lcdfont = pygame.font.Font("fonts/digital-7.ttf", 48)
font = pygame.font.SysFont("Comic Sans MS", 45)
sprites = {
"tile": pygame.image.load("graphics/tile.png"),
"mine": pygame.image.load("graphics/mine.png"),
"DebugDraw": pygame.image.load("graphics/DebugDraw.png"),
"flag": pygame.image.load("graphics/flag.png"),
"dugtile": pygame.image.load("graphics/dugtile.png"),
"smile_neutral": pygame.image.load("graphics/smile_neutral.png"),
"smile_click": pygame.image.load("graphics/smile_tap.png"),
"smile_win": pygame.image.load("graphics/smile_win.png"),
"smile_dead": pygame.image.load("graphics/smile_dead.png"),
"smile_sad": pygame.image.load("graphics/smile_sad.png")
}
sounds = {
"Expl": pygame.mixer.Sound("sounds/Expl.mp3"),
"dig": pygame.mixer.Sound("sounds/dig.ogg"),
"flag": pygame.mixer.Sound("sounds/flag.ogg")
}
class Sprite:
def __init__(self, norm):
self.norm = norm
self.darkened = None
def darken(self):
dark_img = self.norm.copy()
dark_img.fill((170, 170, 170, 255), special_flags=pygame.BLEND_RGBA_ADD)
self.darkened = dark_img
# Преобразовать спрайты в объекты класса Sprite и изменить размер
for sprite in sprites.keys():
sprites[sprite] = Sprite(pygame.transform.scale(sprites[sprite], (50, 50)))
#sprites[sprite].darken()
# Класс клетки
class Tile:
def __init__(self):
self.content = 0
self.unlocked = False
self.pathfound = False
self.dug = False
self.flagged = False
# Направления для счёта мин
dirlookup = {
"up": -10,
"down" : 10,
"left": -1,
"right": 1,
"upleft": -11,
"upright": -9,
"downleft": 9,
"downright": 11
}
# Урезанная версия направлений для поиска пути
pathfind_dirs = {
"up": -10,
"down" : 10,
"left": -1,
"right": 1,
}
# Цветовая схема
colors = [(0,0,255),
(0,255,0),
(255,0,0),
(64, 64, 255),
(255, 64, 64),
(64, 64, 64)]
# Двинуть индекс в направлении
def moveindex(i, dir):
work_i = i + dirlookup[dir]
row = int(i / 10)
column = i % 10
# Правила выхода за границы
if row == 0:
if dir == "up" or dir == "upleft" or dir == "upright":
return None
if row == 9:
if dir == "down" or dir == "downleft" or dir == "downright":
return None
if column == 0:
if dir == "left" or dir == "downleft" or dir == "upleft":
return None
if column == 9:
if dir == "right" or dir == "downright" or dir == "upright":
return None
return work_i
# Проверить наличие мины в направлении
def checkdir(i, dir):
work_i = moveindex(i, dir)
if work_i == None:
return 0
try:
if grid[work_i].content == "m":
return 1
else:
return 0
except IndexError:
return 0
# Проверить наличие мин вокруг клетки
def checksurround(i):
total_mines = 0
for dir in dirlookup.keys():
total_mines += checkdir(i, dir)
return total_mines
branches = []
# Класс ветки для поиска пути
class Branch:
def __init__(self, i, dir):
self.dir = dir
self.i = i
branchout(moveindex(self.i, self.dir))
# Разветвление пути
def branchout(i):
for dir in pathfind_dirs:
work_i = moveindex(i, dir)
if work_i == None:
continue
if grid[work_i].content > 0:
grid[work_i].unlocked = True
grid[work_i].dug = True
continue
if grid[work_i].pathfound:
continue
grid[work_i].dug = True
grid[work_i].pathfound = True
branches.append(Branch(i, dir))
# Поиск пути
def pathfind(start):
global gameover
if grid[start].pathfound:
return
if grid[start].content == "m":
gameover = "mine"
reset_tapped()
sounds["Expl"].play()
print("BOMB")
return
if grid[start].content > 0:
grid[start].unlocked = True
grid[start].dug = True
sounds["dig"].play()
return
sounds["dig"].play()
branchout(start)
# Плейсхолдеры для LCD дисплеев
lcdplaceholders = (lcdfont.render("000", False, "red"), lcdfont.render("010", False, "red"))
# Отрисовка LCD дисплея
def lcd(surf: pygame.Surface,text: any, pos: tuple, data: str):
if not type(text) == str:
text = str(text)
if int(text) < 0:
text = text.removeprefix("-")
for i in range(2 - len(text)):
text = "0" + text
text = "-" + text
if len(text) < 3:
for i in range(3 - len(text)):
text = "0" + text
text_surf = lcdfont.render(text, False, "red")
if data == "clock":
text_rect = lcdplaceholders[0].get_rect()
else:
text_rect = lcdplaceholders[1].get_rect()
text_rect.center = pos
background = text_rect.copy()
background.width += 25
background.center = text_rect.center
pygame.draw.rect(surf, "black", background, border_radius=7)
surf.blit(text_surf, text_rect)
# Рисовать смайлик
def drawsmile(surf: pygame.Surface, state: str):
global smilerect
smilerect = pygame.Rect(0,0,50,50)
smilerect.center = (surf.get_width() / 2, 50)
surf.blit(sprites[state].norm, smilerect)
# Проверить поле на правильность флагов
def checkfield(minecount: int):
correct = 0
for tile in grid:
if tile.flagged:
if tile.content == "m":
correct += 1
else:
return False
for tile in grid:
if not tile.unlocked:
if not tile == "m":
tile.unlocked = True
if type(tile.content) == int:
if not tile.content > 0:
if not tile.dug:
tile.dug = True
if correct == minecount:
return True
# Главный цикл игры
def mainloop():
global running, mines, tick, state, gameover
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("lightgray")
lcd(screen, mines, (150, 50), "mines")
lcd(screen, int(tick / 1000), (350, 50), "clock")
drawsmile(screen, state)
# Если мин не осталось, проверить правильность флагов
if mines == 0:
if checkfield(10):
gameover = "win"
reset_tapped()
else:
gameover = "incorrect"
reset_tapped()
# Отрисовка клеток
for i in range(10):
for j in range(10):
index = i + j * 10
rect = pygame.Rect(0, 0, 50, 50)
rect.center = (i*50 + 25, j*50 + 125)
if rect.collidepoint(pygame.mouse.get_pos()):
if not gameover:
if pygame.mouse.get_pressed()[0]:
state = "smile_click"
if not tapped[0].value or not tapped[0].data == index:
tapped[0].value = True
tapped[0].data = index
threading.Thread(target=pathfind, args=(index,), daemon=True).start()
else:
tapped[0].value = False
if pygame.mouse.get_pressed()[2]:
state = "smile_click"
if not tapped[1].value or not tapped[1].data == index:
tapped[1].value = True
tapped[1].data = index
if grid[index].flagged:
grid[index].flagged = False
mines += 1
condition = True
# Если уже флаг стоит, снять его
else:
# Проверить можно ли поставить флаг
if type(grid[index].content) == int:
condition = grid[index].content > 0 and grid[index].unlocked or grid[index].dug
else:
condition = False
if not condition:
# Поставить флаг
grid[index].flagged = True
sounds["flag"].play()
mines -= 1
else:
tapped[1].value = False
if not pygame.mouse.get_pressed()[0] and not pygame.mouse.get_pressed()[2]:
state = "smile_neutral"
else:
if smilerect.collidepoint(pygame.mouse.get_pos()):
if pygame.mouse.get_pressed()[0]:
if tapped[0].value == False:
tapped[0].value = True
else:
if tapped[0].value == True:
# Если нажал на смайлик, перезапустить игру
running = False
print("PUK")
startgame()
mainloop()
if gameover == "mine":
state = "smile_dead"
elif gameover == "incorrect":
state = "smile_sad"
elif gameover == "win":
state = "smile_win"
darken = index % 2 == 0
if j % 2 == 0:
darken = not darken
if grid[index].dug:
#if darken:
#screen.blit(sprites["dugtile"].darkened, rect)
#else:
#screen.blit(sprites["dugtile"].norm, rect)
screen.blit(sprites["dugtile"].norm, rect)
else:
#if darken:
#screen.blit(sprites["tile"].darkened, rect)
#else:
#screen.blit(sprites["tile"].norm, rect)
screen.blit(sprites["tile"].norm, rect)
#if grid[index].pathfound:
#screen.blit(debugdraw, rect)
if grid[index].flagged:
screen.blit(sprites["flag"].norm, rect)
if grid[index].content == "m":
if gameover == "mine" or gameover == "incorrect":
screen.blit(sprites["mine"].norm, rect)
else:
continue
elif grid[index].content > 0 and grid[index].unlocked:
text_surf = font.render(str(grid[index].content), False, colors[grid[index].content - 1])
text_rect = text_surf.get_rect()
text_rect.center = (i*50 + 25, j*50 + 125)
screen.blit(text_surf, text_rect)
pygame.display.flip()
ftime = clock.tick(60)
if not gameover:
tick += ftime
startgame()
mainloop()
pygame.quit()