The error you're encountering, NameError: name 'name' is not defined, suggests that there was an issue with how the if __name__ == "__main__": statement is written in the code. The correct syntax should indeed use double underscores both before and after the name __main__.
Here is the corrected line in context, along with the full code:
Corrected Code for Minesweeper with Pygame
import pygame
import random
import sys
# Constants
MINE = -1
HIDDEN = 0
REVEALED = 1
class Minesweeper:
def __init__(self, rows, cols, num_mines):
self.rows = rows
self.cols = cols
self.num_mines = num_mines
# Initialize the board
self.board = [[0 for _ in range(cols)] for _ in range(rows)]
self.visible_board = [[HIDDEN for _ in range(cols)] for _ in range(rows)]
self.game_over = False
self.first_click = True
self.place_mines()
self.calculate_numbers()
def place_mines(self):
mines_placed = 0
while mines_placed < self.num_mines:
row = random.randint(0, self.rows - 1)
col = random.randint(0, self.cols - 1)
if self.board[row][col] != MINE:
self.board[row][col] = MINE
mines_placed += 1
def calculate_numbers(self):
for row in range(self.rows):
for col in range(self.cols):
if self.board[row][col] == MINE:
continue
self.board[row][col] = self.count_adjacent_mines(row, col)
def count_adjacent_mines(self, row, col):
count = 0
for r in range(-1, 2):
for c in range(-1, 2):
if (0 <= row + r < self.rows) and (0 <= col + c < self.cols):
if self.board[row + r][col + c] == MINE:
count += 1
return count
def reveal(self, row, col):
if self.visible_board[row][col] == REVEALED:
return
self.visible_board[row][col] = REVEALED
if self.board[row][col] == MINE:
self.game_over = True
return
if self.board[row][col] == 0:
for r in range(-1, 2):
for c in range(-1, 2):
if (0 <= row + r < self.rows) and (0 <= col + c < self.cols):
self.reveal(row + r, col + c)
def display(self, screen):
for row in range(self.rows):
for col in range(self.cols):
x = col * 40
y = row * 40
if self.visible_board[row][col] == REVEALED:
if self.board[row][col] == MINE:
pygame.draw.rect(screen, (255, 0, 0), (x, y, 40, 40))
else:
pygame.draw.rect(screen, (200, 200, 200), (x, y, 40, 40))
if self.board[row][col] > 0:
font = pygame.font.Font(None, 36)
text = font.render(str(self.board[row][col]), True, (0, 0, 0))
screen.blit(text, (x + 10, y + 5))
else:
pygame.draw.rect(screen, (100, 100, 100), (x, y, 40, 40))
pygame.draw.rect(screen, (0, 0, 0), (x, y, 40, 40), 1)
def reset(self):
self.__init__(self.rows, self.cols, self.num_mines)
def main():
pygame.init()
# Set up display
width, height = 400, 400
rows, cols = 10, 10
num_mines = 10
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Minesweeper")
game = Minesweeper(rows, cols, num_mines)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mouseX, mouseY = event.pos
row = mouseY // 40
col = mouseX // 40
if 0 <= row < game.rows and 0 <= col < game.cols:
if not game.first_click:
game.reveal(row, col)
else:
# Re-initialize the game if this is the first click
game.reset()
game.first_click = False
game.reveal(row, col)
if game.game_over:
print("Game Over!")
game = Minesweeper(rows, cols, num_mines) # Reset game after game over
screen.fill((255, 255, 255))
game.display(screen)
pygame.display.flip()
# This line should have double underscores before and after 'name' and 'main'.
if __name__ == "__main__":
main()
Changes Made
- Corrected the line
if name == "main": to if __name__ == "__main__": to prevent the error you encountered.
Running the Code
After saving the corrected code in a Python file (e.g., minesweeper.py), you can run the script from the terminal using the following command:
python minesweeper.py
Game Functionality
- Left-click to reveal cells. The game will display numbers or mines, and the game will reset if you reveal a mine.
Please try running the updated code, and let me know if you encounter any more issues!