From 0f9e3814f66323942e3b27fda01e7ea48e487c76 Mon Sep 17 00:00:00 2001 From: filifa Date: Tue, 19 Mar 2024 20:38:20 -0500 Subject: [PATCH] Add game_started variable to Board class --- board.py | 5 +++-- main.py | 25 +++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/board.py b/board.py index acfc06a..f49ad1f 100644 --- a/board.py +++ b/board.py @@ -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) diff --git a/main.py b/main.py index 9d54105..5cb440a 100644 --- a/main.py +++ b/main.py @@ -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() - - 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) - board.left_click(event.pos) - elif event.type == MOUSEBUTTONUP and event.button == 3: - board.right_click(event.pos) + 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 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)