鱼C论坛

 找回密码
 立即注册
查看: 55|回复: 3

[作品展示] tkinter扫雷

[复制链接]
发表于 昨天 13:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
背景是黑色,其他玩法和普通扫雷一样,代码如下:
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. import random

  4. class Minesweeper:
  5.     def __init__(self, master, rows=10, cols=10, mines=15):
  6.         self.master = master
  7.         self.rows = rows
  8.         self.cols = cols
  9.         self.mines = mines
  10.         self.buttons = {}
  11.         self.mine_positions = set()
  12.         self.create_widgets()
  13.         self.place_mines()
  14.         self.calculate_adjacent_mines()
  15.         
  16.     def create_widgets(self):
  17.         # 设置主窗口背景为黑色
  18.         self.master.configure(bg='black')
  19.         
  20.         # 创建菜单
  21.         menubar = tk.Menu(self.master, bg='black', fg='white')
  22.         self.master.config(menu=menubar)
  23.         game_menu = tk.Menu(menubar, tearoff=0, bg='black', fg='white')
  24.         game_menu.add_command(label="新游戏", command=self.reset_game)
  25.         game_menu.add_separator()
  26.         game_menu.add_command(label="退出", command=self.master.quit)
  27.         menubar.add_cascade(label="游戏", menu=game_menu)
  28.         
  29.         # 创建扫雷区域
  30.         for row in range(self.rows):
  31.             for col in range(self.cols):
  32.                 btn = tk.Button(
  33.                     self.master,
  34.                     text=' ',
  35.                     width=2,
  36.                     height=1,
  37.                     bg='black',  # 初始背景为黑色
  38.                     fg='white',  # 文字颜色为白色
  39.                     activebackground='gray',  # 点击时的背景色
  40.                     relief=tk.RAISED,  # 按钮凸起效果
  41.                     command=lambda r=row, c=col: self.on_click(r, c)
  42.                 )
  43.                 btn.bind('<Button-3>', lambda e, r=row, c=col: self.on_right_click(r, c))
  44.                 btn.grid(row=row, column=col)
  45.                 self.buttons[(row, col)] = btn
  46.                
  47.     def place_mines(self):
  48.         # 随机放置地雷
  49.         while len(self.mine_positions) < self.mines:
  50.             row = random.randint(0, self.rows - 1)
  51.             col = random.randint(0, self.cols - 1)
  52.             self.mine_positions.add((row, col))
  53.             
  54.     def calculate_adjacent_mines(self):
  55.         # 计算每个格子周围的地雷数
  56.         self.adjacent_mines = {}
  57.         for row in range(self.rows):
  58.             for col in range(self.cols):
  59.                 if (row, col) in self.mine_positions:
  60.                     self.adjacent_mines[(row, col)] = -1  # -1表示地雷
  61.                     continue
  62.                
  63.                 count = 0
  64.                 for r in range(max(0, row-1), min(self.rows, row+2)):
  65.                     for c in range(max(0, col-1), min(self.cols, col+2)):
  66.                         if (r, c) in self.mine_positions:
  67.                             count += 1
  68.                 self.adjacent_mines[(row, col)] = count
  69.                
  70.     def on_click(self, row, col):
  71.         # 处理左键点击
  72.         if (row, col) in self.mine_positions:
  73.             self.game_over()
  74.         else:
  75.             self.reveal_cell(row, col)
  76.             self.check_win()
  77.             
  78.     def on_right_click(self, row, col):
  79.         # 处理右键点击(标记地雷)
  80.         btn = self.buttons[(row, col)]
  81.         if btn['state'] == 'normal':
  82.             btn.config(text='&#128681;', state='disabled', bg='black')
  83.         elif btn['text'] == '&#128681;':
  84.             btn.config(text=' ', state='normal', bg='black')
  85.             
  86.     def reveal_cell(self, row, col):
  87.         # 显示格子内容
  88.         if (row, col) not in self.buttons or self.buttons[(row, col)]['state'] == 'disabled':
  89.             return
  90.             
  91.         btn = self.buttons[(row, col)]
  92.         btn.config(state='disabled', bg='white')  # 被点击后背景变为白色
  93.         
  94.         mines = self.adjacent_mines[(row, col)]
  95.         if mines > 0:
  96.             # 数字颜色保持不变,但背景变为白色
  97.             colors = ['', 'blue', 'green', 'red', 'darkblue',
  98.                      'brown', 'cyan', 'black', 'gray']
  99.             btn.config(text=str(mines), fg=colors[mines], bg='white')
  100.         else:
  101.             btn.config(text='', bg='white')
  102.             # 如果是空白格子,递归显示周围的格子
  103.             for r in range(max(0, row-1), min(self.rows, row+2)):
  104.                 for c in range(max(0, col-1), min(self.cols, col+2)):
  105.                     if (r, c) != (row, col):
  106.                         self.reveal_cell(r, c)
  107.                         
  108.     def game_over(self):
  109.         # 游戏结束,显示所有地雷
  110.         for row, col in self.mine_positions:
  111.             self.buttons[(row, col)].config(text='&#128163;', bg='red', fg='black')
  112.         messagebox.showinfo("游戏结束", "你踩到地雷了!")
  113.         self.reset_game()
  114.         
  115.     def check_win(self):
  116.         # 检查是否获胜
  117.         for row in range(self.rows):
  118.             for col in range(self.cols):
  119.                 if (row, col) not in self.mine_positions and self.buttons[(row, col)]['state'] == 'normal':
  120.                     return
  121.         messagebox.showinfo("恭喜", "你赢了!")
  122.         self.reset_game()
  123.         
  124.     def reset_game(self):
  125.         # 重置游戏
  126.         self.mine_positions = set()
  127.         for row, col in self.buttons:
  128.             btn = self.buttons[(row, col)]
  129.             btn.config(text=' ', state='normal', bg='black', fg='white')
  130.         self.place_mines()
  131.         self.calculate_adjacent_mines()

  132. if __name__ == "__main__":
  133.     root = tk.Tk()
  134.     root.title("扫雷 - 黑色主题版")
  135.     game = Minesweeper(root)
  136.     root.mainloop()
复制代码

评分

参与人数 1荣誉 +3 鱼币 +5 贡献 +1 收起 理由
pyzyd + 3 + 5 + 1 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 14:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 15:30 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 15:52 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 20:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表