Add game_started variable to Board class
This commit is contained in:
		
							parent
							
								
									877ea522a0
								
							
						
					
					
						commit
						0f9e3814f6
					
				
							
								
								
									
										5
									
								
								board.py
								
								
								
								
							
							
						
						
									
										5
									
								
								board.py
								
								
								
								
							| 
						 | 
					@ -11,6 +11,7 @@ class Board:
 | 
				
			||||||
        self.game_won = False
 | 
					        self.game_won = False
 | 
				
			||||||
        self.total_flags = 0
 | 
					        self.total_flags = 0
 | 
				
			||||||
        self.coords = (0,50)
 | 
					        self.coords = (0,50)
 | 
				
			||||||
 | 
					        self.game_started = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.unclicked_squares = size[0] * size[1]
 | 
					        self.unclicked_squares = size[0] * size[1]
 | 
				
			||||||
        self.total_mines = round(.2*self.unclicked_squares)
 | 
					        self.total_mines = round(.2*self.unclicked_squares)
 | 
				
			||||||
| 
						 | 
					@ -89,7 +90,7 @@ class Board:
 | 
				
			||||||
    def place_mines(self, mousepos):
 | 
					    def place_mines(self, mousepos):
 | 
				
			||||||
        square = self.get_clicked_square(mousepos)
 | 
					        square = self.get_clicked_square(mousepos)
 | 
				
			||||||
        if square is None:
 | 
					        if square is None:
 | 
				
			||||||
            return True
 | 
					            return
 | 
				
			||||||
        adj_squares = self.squares_adjacent_to(square)
 | 
					        adj_squares = self.squares_adjacent_to(square)
 | 
				
			||||||
        free_spaces = self.unclicked_squares-self.total_mines-1-len(adj_squares)
 | 
					        free_spaces = self.unclicked_squares-self.total_mines-1-len(adj_squares)
 | 
				
			||||||
        is_mine = ([True]*self.total_mines + [False]*free_spaces)
 | 
					        is_mine = ([True]*self.total_mines + [False]*free_spaces)
 | 
				
			||||||
| 
						 | 
					@ -103,7 +104,7 @@ class Board:
 | 
				
			||||||
                adj_squares = self.squares_adjacent_to(s)
 | 
					                adj_squares = self.squares_adjacent_to(s)
 | 
				
			||||||
                total_adj_mines = sum([i.is_mine for i in adj_squares])
 | 
					                total_adj_mines = sum([i.is_mine for i in adj_squares])
 | 
				
			||||||
                s.mines_touching = total_adj_mines
 | 
					                s.mines_touching = total_adj_mines
 | 
				
			||||||
        return False
 | 
					        self.game_started = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def squares_adjacent_to(self, s):
 | 
					    def squares_adjacent_to(self, s):
 | 
				
			||||||
        x, y = self.get_index(s)
 | 
					        x, y = self.get_index(s)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										9
									
								
								main.py
								
								
								
								
							
							
						
						
									
										9
									
								
								main.py
								
								
								
								
							| 
						 | 
					@ -19,7 +19,6 @@ board = Board((16, 31), 40, DISPLAYSURF)
 | 
				
			||||||
# Prevents mouse movement from counting as an event, reducing CPU usage
 | 
					# Prevents mouse movement from counting as an event, reducing CPU usage
 | 
				
			||||||
pygame.event.set_blocked(MOUSEMOTION)
 | 
					pygame.event.set_blocked(MOUSEMOTION)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
first_left_click = True
 | 
					 | 
				
			||||||
while True:
 | 
					while True:
 | 
				
			||||||
    board.update_mine_counter()
 | 
					    board.update_mine_counter()
 | 
				
			||||||
    board.check_for_win()
 | 
					    board.check_for_win()
 | 
				
			||||||
| 
						 | 
					@ -33,16 +32,14 @@ while True:
 | 
				
			||||||
        print("You win")
 | 
					        print("You win")
 | 
				
			||||||
        break
 | 
					        break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Program waits here until a non-mouse movement event is read
 | 
					    for event in pygame.event.get():
 | 
				
			||||||
    event = pygame.event.wait()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if event.type == QUIT:
 | 
					        if event.type == QUIT:
 | 
				
			||||||
            pygame.quit()
 | 
					            pygame.quit()
 | 
				
			||||||
            sys.exit()
 | 
					            sys.exit()
 | 
				
			||||||
        elif event.type == MOUSEBUTTONUP and event.button == 1:
 | 
					        elif event.type == MOUSEBUTTONUP and event.button == 1:
 | 
				
			||||||
            # FIXME: Need a check to make sure a square was actually clicked
 | 
					            # FIXME: Need a check to make sure a square was actually clicked
 | 
				
			||||||
        if first_left_click:
 | 
					            if not board.game_started:
 | 
				
			||||||
            first_left_click = board.place_mines(event.pos)
 | 
					                board.place_mines(event.pos)
 | 
				
			||||||
            board.left_click(event.pos)
 | 
					            board.left_click(event.pos)
 | 
				
			||||||
        elif event.type == MOUSEBUTTONUP and event.button == 3:
 | 
					        elif event.type == MOUSEBUTTONUP and event.button == 3:
 | 
				
			||||||
            board.right_click(event.pos)
 | 
					            board.right_click(event.pos)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue