Add game_started variable to Board class

This commit is contained in:
filifa 2024-03-19 20:38:20 -05:00
parent 877ea522a0
commit 0f9e3814f6
2 changed files with 14 additions and 16 deletions

View File

@ -11,6 +11,7 @@ class Board:
self.game_won = False
self.total_flags = 0
self.coords = (0,50)
self.game_started = False
self.unclicked_squares = size[0] * size[1]
self.total_mines = round(.2*self.unclicked_squares)
@ -89,7 +90,7 @@ class Board:
def place_mines(self, mousepos):
square = self.get_clicked_square(mousepos)
if square is None:
return True
return
adj_squares = self.squares_adjacent_to(square)
free_spaces = self.unclicked_squares-self.total_mines-1-len(adj_squares)
is_mine = ([True]*self.total_mines + [False]*free_spaces)
@ -103,7 +104,7 @@ class Board:
adj_squares = self.squares_adjacent_to(s)
total_adj_mines = sum([i.is_mine for i in adj_squares])
s.mines_touching = total_adj_mines
return False
self.game_started = True
def squares_adjacent_to(self, s):
x, y = self.get_index(s)

View File

@ -19,7 +19,6 @@ board = Board((16, 31), 40, DISPLAYSURF)
# Prevents mouse movement from counting as an event, reducing CPU usage
pygame.event.set_blocked(MOUSEMOTION)
first_left_click = True
while True:
board.update_mine_counter()
board.check_for_win()
@ -33,16 +32,14 @@ while True:
print("You win")
break
# Program waits here until a non-mouse movement event is read
event = pygame.event.wait()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONUP and event.button == 1:
# FIXME: Need a check to make sure a square was actually clicked
if first_left_click:
first_left_click = board.place_mines(event.pos)
if not board.game_started:
board.place_mines(event.pos)
board.left_click(event.pos)
elif event.type == MOUSEBUTTONUP and event.button == 3:
board.right_click(event.pos)