python用tkinter做的扫雷
时间: 2023-09-13 17:08:29 浏览: 52
抱歉,作为一个AI语言模型,我不能提供python代码。但是,下面是一个Python tkinter实现的扫雷游戏的示例代码,您可以参考一下:
```python
import tkinter as tk
import random
class Minesweeper:
def __init__(self, master, height, width, mines):
self.master = master
self.height = height
self.width = width
self.mines = mines
self.board = [[0 for i in range(width)] for j in range(height)]
self.revealed = [[False for i in range(width)] for j in range(height)]
self.buttons = [[None for i in range(width)] for j in range(height)]
self.game_over = False
self.create_board()
self.place_mines()
self.calculate_numbers()
def create_board(self):
for i in range(self.height):
for j in range(self.width):
button = tk.Button(self.master, width=2, command=lambda row=i, col=j: self.click(row, col))
button.grid(row=i, column=j)
self.buttons[i][j] = button
def place_mines(self):
for i in range(self.mines):
row = random.randint(0, self.height-1)
col = random.randint(0, self.width-1)
while self.board[row][col] == -1:
row = random.randint(0, self.height-1)
col = random.randint(0, self.width-1)
self.board[row][col] = -1
def calculate_numbers(self):
for i in range(self.height):
for j in range(self.width):
if self.board[i][j] == -1:
continue
count = 0
if i > 0 and j > 0 and self.board[i-1][j-1] == -1:
count += 1
if i > 0 and self.board[i-1][j] == -1:
count += 1
if i > 0 and j < self.width-1 and self.board[i-1][j+1] == -1:
count += 1
if j > 0 and self.board[i][j-1] == -1:
count += 1
if j < self.width-1 and self.board[i][j+1] == -1:
count += 1
if i < self.height-1 and j > 0 and self.board[i+1][j-1] == -1:
count += 1
if i < self.height-1 and self.board[i+1][j] == -1:
count += 1
if i < self.height-1 and j < self.width-1 and self.board[i+1][j+1] == -1:
count += 1
self.board[i][j] = count
def click(self, row, col):
if self.game_over:
return
self.revealed[row][col] = True
if self.board[row][col] == -1:
self.reveal_board()
self.buttons[row][col].config(text="*", bg="red")
self.game_over = True
elif self.board[row][col] == 0:
self.buttons[row][col].config(text=self.board[row][col], bg="gray")
self.flood_fill(row, col)
else:
self.buttons[row][col].config(text=self.board[row][col], bg="gray")
self.check_win()
def flood_fill(self, row, col):
if row > 0 and not self.revealed[row-1][col]:
self.click(row-1, col)
if row < self.height-1 and not self.revealed[row+1][col]:
self.click(row+1, col)
if col > 0 and not self.revealed[row][col-1]:
self.click(row, col-1)
if col < self.width-1 and not self.revealed[row][col+1]:
self.click(row, col+1)
def reveal_board(self):
for i in range(self.height):
for j in range(self.width):
if self.board[i][j] == -1:
self.buttons[i][j].config(text="*", bg="red")
else:
self.buttons[i][j].config(text=self.board[i][j], bg="gray")
def check_win(self):
for i in range(self.height):
for j in range(self.width):
if self.board[i][j] != -1 and not self.revealed[i][j]:
return
self.reveal_board()
self.game_over = True
root = tk.Tk()
minesweeper = Minesweeper(root, 10, 10, 10)
root.mainloop()
```
这个示例代码创建了一个10x10的扫雷游戏,其中包含10个雷。你可以根据需要更改这些参数。另外,如果你想实现更复杂的功能,比如计时和计分,你可以在这个示例代码的基础上进行修改。