-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathminesweeper.py
95 lines (62 loc) · 1.91 KB
/
minesweeper.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
import argparse
import pygame
from consts import GRIDPADDING
from consts import BLOCK_SIZE
parser = argparse.ArgumentParser()
parser.add_argument("-x", type=int, default=16)
parser.add_argument("-y", type=int, default=16)
parser.add_argument("-m", "--mines", type=int, default=40)
args = parser.parse_args()
BACKGROUND_COLOUR = (204, 204, 204)
FLAG_COUNT = 20
dx = (GRIDPADDING * 2) + (args.x * BLOCK_SIZE)
dy = (GRIDPADDING * 2) + (args.y * BLOCK_SIZE) + FLAG_COUNT
GUESS_TEXT_LENGTH = 40
fx = (dx / 2) - GUESS_TEXT_LENGTH
fy = dy - FLAG_COUNT
pygame.init()
win = pygame.display.set_mode((dx, dy))
import sprites
from board import Board
pygame.display.set_caption("Minesweeper")
font = pygame.font.SysFont("monospace", 15)
def _render_flg_count(mines: int, flags: int):
return font.render(
f"Mines: {mines - flags}", 1, (0, 0, 0)
)
board = Board(args.x, args.y, mines=args.mines)
flag_count = _render_flg_count(board.n_mines, len(board.flags))
def render():
win.fill(BACKGROUND_COLOUR)
board.render(win)
win.blit(flag_count, (fx, fy))
pygame.display.update()
safe = True
playing = True
while playing:
pos = None
event = pygame.event.wait()
if not safe:
board.show_mines()
if event.type == pygame.MOUSEBUTTONDOWN:
leftclick, _, rightclick = pygame.mouse.get_pressed()
pos = pygame.mouse.get_pos()
if rightclick and safe:
board.rightclick(pos)
flag_count = _render_flg_count(
board.n_mines, len(board.flags)
)
elif leftclick and safe:
safe = board.leftclick(pos)
elif leftclick:
playing = False
if board.check_win():
board.display_win()
flag_count = _render_flg_count(
board.n_mines, len(board.flags)
)
safe = False
if event.type == pygame.QUIT:
playing = False
render()
pygame.quit()