-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (74 loc) · 2.87 KB
/
main.py
File metadata and controls
92 lines (74 loc) · 2.87 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
import pygame
from settings import *
from sprites import *
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
def new(self):
self.board = Board()
self.board.display_board()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.draw()
else:
self.end_screen()
def draw(self):
self.screen.fill(BGCOLOUR)
self.board.draw(self.screen)
pygame.display.flip()
def check_win(self):
for row in self.board.board_list:
for tile in row:
if tile.type != "X" and not tile.revealed:
return False
return True
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
mx, my = pygame.mouse.get_pos()
mx //= TILESIZE
my //= TILESIZE
if event.button == 1:
if not self.board.board_list[mx][my].flagged:
# dig and check if exploded
if not self.board.dig(mx, my):
# explode
for row in self.board.board_list:
for tile in row:
if tile.flagged and tile.type != "X":
tile.flagged = False
tile.revealed = True
tile.image = tile_not_mine
elif tile.type == "X":
tile.revealed = True
self.playing = False
if event.button == 3:
if not self.board.board_list[mx][my].revealed:
self.board.board_list[mx][my].flagged = not self.board.board_list[mx][my].flagged
if self.check_win():
self.win = True
self.playing = False
for row in self.board.board_list:
for tile in row:
if not tile.revealed:
tile.flagged = True
def end_screen(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
return
game = Game()
while True:
game.new()
game.run()