用python制作扫雷
本帖最后由 苏紫方璇 于 2023-12-17 13:40 编辑这是一个扫雷模板,j.mp3, xxj.mp3, yhhmgn.mp3为音频素材
```
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 = [ for _ in range(rows)]
self.revealed = [ 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.grid(row=row, column=col, sticky="nsew")
self.buttons.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 != -1:
self.board = -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 != -1:
self.board += 1
def click(self, row, col):
# 处理点击事件
self.click_sound.play()
if self.board == -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:
self.revealed = True
if self.board == 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.config(text="鸡", state=tk.DISABLED)
else:
self.buttons.config(text=str(self.board), state=tk.DISABLED)
def mark_mine(self, event, row, col):
# 标记地雷
if not self.revealed:
if self.buttons["text"] == "M":
self.buttons.config(text="", fg="black")
self.remaining_mines += 1
else:
self.buttons.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 == -1 and self.buttons["text"] != "M":
return False
elif self.board != -1 and not self.revealed:
return False
return True
def game_over(self):
# 游戏结束处理
for row in range(self.rows):
for col in range(self.cols):
if self.board == -1:
self.buttons.config(text='X', state=tk.DISABLED)
else:
self.buttons.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.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()
``` 开庭带上程序源码 代码的格式有点小问题)另外,我对 lz 的音效很感兴趣{:301_998:} 感觉很好玩,,,, 正在学习Python,感觉不错 谢谢分享 厉害了,学习一下 开始学习 这个有意思{:1_918:} 学习一下!