-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
393 lines (293 loc) · 12.2 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
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
# main.py
import sys
import math
import pygame as pg
from msdraw import draw_border, swap_color, render_cell
from msgui import NumberDisplay, SmileButton
from board import Board
from conf import *
class App:
""" The main class application, contains methods to manage player
interactions with the board, and methods to display the current state of
the board """
def __init__(self, board_size, mines):
# Initialize pygame
pg.init()
# Increases the maximum recursion limit for cases where the map is
# too large and the digg recursion exceeds its normal depth limit
sys.setrecursionlimit(2000)
self.screen_size = (
CELL_SIZE * board_size[0] + 25,
CELL_SIZE * board_size[1] + 64
)
self.offset = 13, 56
self.window = pg.display.set_mode(self.screen_size)
pg.display.set_caption("Mine Sweeper")
self.board = Board(board_size, mines)
self.flags_display = NumberDisplay(self.board.mines_remaining())
self.clock_display = NumberDisplay(0)
self.smile_button = SmileButton(
self, (self.screen_size[0] // 2 - 13, 16),
self.restart
)
self.clock = pg.time.Clock()
self.start_time = None
self.background = self.render_background()
self.cell_symbols = self.render_symbols()
self.left_click = False
self.right_click = False
self.chord_mode = False
# To mark if the player is able to continue interacting with the board
self.alive = True
self.won = False
def render_background(self):
""" Generates a pygame surface representing the background """
w, h = self.screen_size
surf = pg.Surface((w, h))
bw, bh = self.board.size[0] * CELL_SIZE, self.board.size[1] * CELL_SIZE
# Main screen border
draw_border(
surf, (0, 0), (w + 3, h + 3), 3,
C_LIGHT_GRAY, C_WHITE, C_GRAY, C_LIGHT_GRAY
)
# Top bar border
draw_border(
surf, (10, 10), (bw + 6, 37), 2,
C_LIGHT_GRAY, C_GRAY, C_WHITE, C_LIGHT_GRAY
)
# Mines remaining display border
draw_border(
surf, (17, 16), (41, 25), 1,
C_LIGHT_GRAY, C_GRAY, C_WHITE, C_BLACK
)
# Time passed display border
draw_border(
surf, (bw + 6 - 40, 16), (41, 25), 1,
C_LIGHT_GRAY, C_GRAY, C_WHITE, C_BLACK
)
# Board border
draw_border(
surf, (10, 53), (bw + 6, bh + 6), 3,
C_LIGHT_GRAY, C_GRAY, C_WHITE, C_LIGHT_GRAY
)
return surf
def render_unexplored_cell(self):
""" Generates a pygame surface representing an unexplored cell """
size = CELL_SIZE
surf = pg.Surface((size, size))
surf.fill(C_LIGHT_GRAY)
draw_border(
surf, (0, 0), (size, size), 2,
C_LIGHT_GRAY, C_WHITE, C_GRAY, C_LIGHT_GRAY
)
return surf
def render_explored_cell(self):
""" Generates a pygame surface representing an explored cell """
surf = pg.Surface((CELL_SIZE, CELL_SIZE))
surf.fill(C_GRAY)
pg.draw.rect(surf, C_LIGHT_GRAY, (1, 1, CELL_SIZE - 1, CELL_SIZE - 1))
return surf
def render_symbols(self):
""" Loads all the symbols and cache them in custom generated
surfaces to be used in self.render() """
symbols = {}
# Render the base cells
explored_cell = self.render_explored_cell()
unexplored_cell = self.render_unexplored_cell()
# Render all numbers
for i in range(0, 10):
symbols[i] = render_cell(
explored_cell, i, (C_WHITE, NUMBER_COLORS[i])
)
# Store the surfaces
symbols[UNTOUCHED_MINE_CELL] = render_cell(
explored_cell, 10
)
symbols[INCORRECT_MINE_CELL] = render_cell(
symbols[UNTOUCHED_MINE_CELL], 12, (C_WHITE, C_RED)
)
symbols[DETONED_MINE_CELL] = swap_color(
render_cell(explored_cell, 10), C_LIGHT_GRAY, C_RED
)
symbols[FLAG_CELL] = render_cell(
unexplored_cell, 11, (C_WHITE, C_RED)
)
symbols[UNEXPLORED_CELL] = unexplored_cell
symbols[EXPLORED_CELL] = explored_cell
return symbols
def restart(self, board_size=None, mines=None):
""" Method to restart the game. Called when the player press 'r'
or clicks the smile button """
board_size = board_size if board_size else self.board.size
mines = mines if mines else self.board.mines
oldboard_size = self.board.size
self.board.__init__(board_size, mines)
self.start_time = None
self.alive = True
self.won = False
self.clock_display.set_value(0)
if board_size == oldboard_size:
return
self.screen_size = (
CELL_SIZE * board_size[0] + 25,
CELL_SIZE * board_size[1] + 64
)
self.smile_button.pos = self.screen_size[0] // 2 - 13, 16
self.window = pg.display.set_mode(self.screen_size)
self.background = self.render_background()
def on_success_dig(self):
self.won = self.board.win()
digg_map = self.board.digg_map
if not self.start_time and \
len(digg_map[digg_map == -1]) < BOARD_SIZE[0] * BOARD_SIZE[1]:
self.start_time = self.get_time()
def check_events(self):
""" Method to manage player events:
If the player press 'ESC', exit the game.
If the player press 'r', restart the game.
If the player 'press' left click, digg in a cell.
If the player press right click, place/remove a flag on a cell.
"""
self.left_click, _, self.right_click = pg.mouse.get_pressed()
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
# Exit the game and shutdown the program
if event.key == pg.K_ESCAPE:
pg.quit()
sys.exit()
# Reset the current game
if event.key == pg.K_r:
self.restart()
# Starts a new game with beginner difficulty
if event.key == pg.K_1:
self.restart((9, 9), 10)
# Starts a new game with intermediate difficulty
if event.key == pg.K_2:
self.restart((16, 16), 40)
# Starts a new game with expert difficulty
if event.key == pg.K_3:
self.restart((30, 16), 99)
# If the player is not alive, skip.
if not self.alive or self.won:
continue
# Event for when the player clicked to place a flag
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == RIGHT and not self.left_click:
self.board.place_flag(self.cell_pos(event.pos))
# Update flag display value
self.flags_display.set_value(self.board.mines_remaining())
# Event for when the player clicked to digg a place
if event.type == pg.MOUSEBUTTONUP:
# If the chord technique is not active, digg a single cell
if event.button == LEFT and not self.chord_mode:
# Digg the clicked place
if not self.board.digg(self.cell_pos(event.pos)):
self.end_game()
continue
self.on_success_dig()
# If the chord technique is active, digg all the
# surrounding cells
if (event.button == LEFT and self.right_click)\
or (event.button == RIGHT and self.left_click):
# If the chording fails, means that the player wrong placed
# a flag and the chording technique digged a mine.
if not self.board.chord(self.cell_pos(event.pos)):
self.end_game()
continue
self.on_success_dig()
if self.won:
# Places a flag in all unexplored cells
digg_map = self.board.digg_map
digg_map[digg_map == UNEXPLORED_CELL] = FLAG_CELL
def cell_pos(self, pos):
""" Calculates and returns the cell position from a screen position """
off_x, off_y = self.offset
return (pos[0] - off_x) // CELL_SIZE, (pos[1] - off_y) // CELL_SIZE
def end_game(self):
""" Finish the game, called when the player stepped on a mine """
self.board.reveal_mines()
self.alive = False
def render_field(self):
""" Displays the entire field map and also displays """
board = self.board
w, h = board.size
off_x, off_y = self.offset
# Render the digg_map status of the board
for i in range(w):
for j in range(h):
surf = self.cell_symbols[board.digg_map[i, j]]
self.window.blit(
surf, (i * CELL_SIZE + off_x, j * CELL_SIZE + off_y)
)
def render_click_effects(self):
""" Displays effects for when the player is holding click to digg
a cell and displays the effect of the player holding both clicks to
make a chord technique """
board = self.board
w, h = board.size
off_x, off_y = self.offset
# If the player isn't alive, stop proccess
if not self.alive or self.won:
return
# If the mouse position isn't inside the board, skip the next actions
x, y = self.cell_pos(pg.mouse.get_pos())
if not board.inside_board((x, y)):
return
# Turn on chord mode if both clicks are pressing
if self.left_click and self.right_click:
self.chord_mode = True
# Replace all surrounding cells from (x, y) with explored_cell
for i in range(-1, 2):
for j in range(-1, 2):
if not board.inside_board((x + i, y + j)):
continue
if board.digg_map[x + i, y + j] != -1:
continue
self.window.blit(
self.cell_symbols[EXPLORED_CELL],
((x + i)*CELL_SIZE + off_x, (y + j)*CELL_SIZE + off_y)
)
# Turn off chord mode if clicks are no longer holding
if not self.left_click and not self.right_click:
self.chord_mode = False
# Single cell dig mode
if self.left_click and not self.chord_mode:
if board.digg_map[x, y] == -1:
self.window.blit(
self.cell_symbols[EXPLORED_CELL],
(x * CELL_SIZE + off_x, y * CELL_SIZE + off_y)
)
def render_displays(self):
bw, bh = self.board.size[0] * CELL_SIZE, self.board.size[1] * CELL_SIZE
# Update and render remaining mines display
self.flags_display.set_value(self.board.mines_remaining())
self.window.blit(self.flags_display.surf, (18, 17))
# Update and render clock display
if self.start_time and not self.won and self.alive:
time = int(self.get_time() - self.start_time)
self.clock_display.set_value(time)
self.window.blit(self.clock_display.surf, (bw - 33, 17))
def render(self):
""" Contains render methods to display the game """
self.window.blit(self.background, (0, 0))
self.render_field()
self.render_click_effects()
self.render_displays()
self.smile_button.draw(self.window)
pg.display.flip()
def get_time(self):
return pg.time.get_ticks() * 0.001
def start(self):
""" Starts the main loop of the game """
while True:
self.check_events()
self.render()
self.clock.tick(GAME_FPS)
def main():
app = App(BOARD_SIZE, MINES)
app.start()
if __name__ == '__main__':
main()