python扫雷代码有ui
时间: 2024-01-13 20:04:12 浏览: 166
当然有,以下是一个简单的 Python 扫雷游戏的代码,带有简单的图形用户界面(GUI):
```python
import tkinter as tk
from tkinter import messagebox
from random import randint
class Minesweeper:
def __init__(self, rows, cols, mines):
self.rows = rows
self.cols = cols
self.mines = mines
self.grid = [[0 for i in range(cols)] for j in range(rows)]
self.visible = [[False for i in range(cols)] for j in range(rows)]
self.flags = [[False for i in range(cols)] for j in range(rows)]
self.remaining = mines
self.create_mines()
self.calculate_numbers()
def create_mines(self):
for i in range(self.mines):
while True:
row = randint(0, self.rows - 1)
col = randint(0, self.cols - 1)
if self.grid[row][col] == 0:
self.grid[row][col] = -1
break
def calculate_numbers(self):
for i in range(self.rows):
for j in range(self.cols):
if self.grid[i][j] == -1:
continue
count = 0
for x in range(-1, 2):
for y in range(-1, 2):
if i + x < 0 or i + x >= self.rows or j + y < 0 or j + y >= self.cols:
continue
if self.grid[i + x][j + y] == -1:
count += 1
self.grid[i][j] = count
def reveal(self, row, col):
if not self.visible[row][col]:
self.visible[row][col] = True
if self.grid[row][col] == -1:
return False
if self.grid[row][col] == 0:
for x in range(-1, 2):
for y in range(-1, 2):
if row + x < 0 or row + x >= self.rows or col + y < 0 or col + y >= self.cols:
continue
self.reveal(row + x, col + y)
return True
def flag(self, row, col):
if self.visible[row][col]:
return
self.flags[row][col] = not self.flags[row][col])
if self.flags[row][col]:
self.remaining -= 1
else:
self.remaining += 1
def check_win(self):
for i in range(self.rows):
for j in range(self.cols):
if self.grid[i][j] != -1 and not self.visible[i][j]:
return False
return True
def get_state(self, row, col):
if not self.visible[row][col]:
if self.flags[row][col]:
return "F"
else:
return "?"
else:
if self.grid[row][col] == -1:
return "X"
else:
return str(self.grid[row][col])
class MinesweeperGUI:
def __init__(self, rows, cols, mines):
self.game = Minesweeper(rows, cols, mines)
self.window = tk.Tk()
self.window.title("Minesweeper")
self.button_grid = []
for i in range(rows):
row = []
for j in range(cols):
button = tk.Button(self.window, text = "", width = 2, height = 1, command = lambda row = i, col = j: self.click(row, col))
button.grid(row = i, column = j)
row.append(button)
self.button_grid.append(row)
self.update_buttons()
self.remaining_label = tk.Label(self.window, text = "Mines remaining: " + str(self.game.remaining))
self.remaining_label.grid(row = rows, column = 0, columnspan = cols)
def click(self, row, col):
if self.game.reveal(row, col):
self.update_buttons()
if self.game.check_win():
messagebox.showinfo("Minesweeper", "You win!")
self.window.destroy()
else:
messagebox.showinfo("Minesweeper", "You lose!")
self.window.destroy()
def right_click(self, row, col):
self.game.flag(row, col)
self.remaining_label.config(text = "Mines remaining: " + str(self.game.remaining))
self.update_buttons()
def update_buttons(self):
for i in range(len(self.button_grid)):
for j in range(len(self.button_grid[i])):
state = self.game.get_state(i, j)
if state == "?":
self.button_grid[i][j].config(text = "", bg = "SystemButtonFace")
elif state == "F":
self.button_grid[i][j].config(text = "F", bg = "yellow")
elif state == "X":
self.button_grid[i][j].config(text = "X", bg = "red")
else:
self.button_grid[i][j].config(text = state, bg = "white")
self.button_grid[i][j].config(state = "normal" if not self.game.visible[i][j] else "disabled")
self.button_grid[i][j].bind("<Button-3>", lambda event, row = i, col = j: self.right_click(row, col))
def run(self):
self.window.mainloop()
game = MinesweeperGUI(10, 10, 10)
game.run()
```
这个代码使用了 Python 的 tkinter 模块来创建 GUI,实现了一个简单的扫雷游戏。
阅读全文