大神论坛

找回密码
快速注册
查看: 174 | 回复: 0

[源码] 用python实现扫雷 附源码

主题

帖子

0

积分

初入江湖

UID
668
积分
0
精华
威望
0 点
违规
大神币
68 枚
注册时间
2023-10-14 10:49
发表于 2023-12-17 15:36
本帖最后由 弑神者91511 于 2023-12-17 15:36 编辑

用python实现扫雷 附源码

import random
import tkinter as tk
from tkinter import messagebox
import pygame
import os

class Minesweeper:
def __init__(self, master, rows, cols, mines):
self.master = master
self.rows = rows
self.cols = cols
self.mines = mines
self.remaining_mines = mines
self.board = [[0 for _ in range(cols)] for _ in range(rows)]
self.revealed = [[False for _ in range(cols)] for _ in range(rows)]
self.create_widgets()
self.load_sounds()
def load_sounds(self):
# 初始化音效
pygame.mixer.init()
self.click_sound = pygame.mixer.Sound("j.mp3")
self.win_sound = pygame.mixer.Sound("xxj.mp3")
self.lose_sound = pygame.mixer.Sound("yhhmgn.mp3")

def create_widgets(self):
# 创建按钮
self.buttons = [[tk.Button(self.master, width=2, height=1, command=lambda r=row, c=col: self.click(r, c))
for col in range(self.cols)] for row in range(self.rows)]

for row in range(self.rows):
for col in range(self.cols):
self.buttons[row][col].grid(row=row, column=col, sticky="nsew")
self.buttons[row][col].bind("<Button-3>", lambda event, r=row, c=col: self.mark_mine(event, r, c))

# 放置地雷
self.place_mines()

def place_mines(self):
mine_count = 0
while mine_count < self.mines:
row, col = random.randint(0, self.rows - 1), random.randint(0, self.cols - 1)
if self.board[row][col] != -1:
self.board[row][col] = -1
mine_count += 1
self.update_adjacent_cells(row, col)

def update_adjacent_cells(self, row, col):
# 更新相邻的单元格
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
if 0 <= r < self.rows and 0 <= c < self.cols and self.board[r][c] != -1:
self.board[r][c] += 1

def click(self, row, col):
# 处理点击事件
self.click_sound.play()
if self.board[row][col] == -1:
self.game_over()
else:
self.reveal_cell(row, col)
if self.check_win():
self.game_win()

def reveal_cell(self, row, col):
# 揭示单元格
if not self.revealed[row][col]:
self.revealed[row][col] = True
if self.board[row][col] == 0:
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
if 0 <= r < self.rows and 0 <= c < self.cols:
self.reveal_cell(r, c)

# 将显示无雷的“0”替换成“鸡”
self.buttons[row][col].config(text="鸡", state=tk.DISABLED)
else:
self.buttons[row][col].config(text=str(self.board[row][col]), state=tk.DISABLED)

def mark_mine(self, event, row, col):
# 标记地雷
if not self.revealed[row][col]:
if self.buttons[row][col]["text"] == "M":
self.buttons[row][col].config(text="", fg="black")
self.remaining_mines += 1
else:
self.buttons[row][col].config(text="M", fg="red")
self.remaining_mines -= 1
if self.check_win():
self.game_win()

def check_win(self):
# 检查是否获胜
for row in range(self.rows):
for col in range(self.cols):
if self.board[row][col] == -1 and self.buttons[row][col]["text"] != "M":
return False
elif self.board[row][col] != -1 and not self.revealed[row][col]:
return False
return True

def game_over(self):
# 游戏结束处理
for row in range(self.rows):
for col in range(self.cols):
if self.board[row][col] == -1:
self.buttons[row][col].config(text='X', state=tk.DISABLED)
else:
self.buttons[row][col].config(state=tk.DISABLED)
self.lose_sound.play()
messagebox.showinfo("Game Over", "你被坤坤鸡败了!")
self.reset_game()

def game_win(self):
# 游戏获胜处理
for row in range(self.rows):
for col in range(self.cols):
self.buttons[row][col].config(state=tk.DISABLED)
self.win_sound.play()
messagebox.showinfo("Congratulations!", "你打败坤坤了!")
self.reset_game()

def reset_game(self):
# 重置游戏
self.master.destroy()
new_game = tk.Tk()
new_game.title("Minesweeper")
minesweeper_game = Minesweeper(new_game, rows=self.rows, cols=self.cols, mines=self.mines)
new_game.mainloop()

if __name__ == "__main__":
root = tk.Tk()
root.title("Minesweeper")
minesweeper_game = Minesweeper(root, rows=8, cols=8, mines=10)
root.mainloop()


注:若转载请注明大神论坛来源(本贴地址)与作者信息。

返回顶部