import tkinter as tk import random class Minesweeper: def __init__(self, root): self.root = root self.root.title("踩地雷遊戲") self.rows = 10 self.columns = 10 self.mines = 15 self.buttons = {} self.create_widgets() self.place_mines() self.update_numbers() def create_widgets(self): for r in range(self.rows): for c in range(self.columns): button = tk.Button(self.root, width=3, height=1, command=lambda r=r, c=c: self.on_click(r, c)) button.bind("", lambda e, r=r, c=c: self.on_right_click(r, c)) button.grid(row=r, column=c) self.buttons[(r, c)] = {'button': button, 'is_mine': False, 'mines_around': 0, 'is_flagged': False} def place_mines(self): mines = self.mines while mines > 0: r = random.randint(0, self.rows - 1) c = random.randint(0, self.columns - 1) if not self.buttons[(r, c)]['is_mine']: self.buttons[(r, c)]['is_mine'] = True mines -= 1 def update_numbers(self): for r in range(self.rows): for c in range(self.columns): if self.buttons[(r, c)]['is_mine']: continue count = 0 for i in range(-1, 2): for j in range(-1, 2): if 0 <= r + i < self.rows and 0 <= c + j < self.columns: if self.buttons[(r + i, c + j)]['is_mine']: count += 1 self.buttons[(r, c)]['mines_around'] = count def on_click(self, r, c): if self.buttons[(r, c)]['is_flagged']: return if self.buttons[(r, c)]['is_mine']: self.game_over(False) return self.reveal(r, c) if self.check_win(): self.game_over(True) def on_right_click(self, r, c): if self.buttons[(r, c)]['is_flagged']: self.buttons[(r, c)]['button'].config(text="") self.buttons[(r, c)]['is_flagged'] = False else: self.buttons[(r, c)]['button'].config(text="⚑") self.buttons[(r, c)]['is_flagged'] = True def reveal(self, r, c): if self.buttons[(r, c)]['button']['state'] == "disabled": return self.buttons[(r, c)]['button'].config(text=str(self.buttons[(r, c)]['mines_around']), state="disabled") self.buttons[(r, c)]['button'].config(relief=tk.SUNKEN) if self.buttons[(r, c)]['mines_around'] == 0: for i in range(-1, 2): for j in range(-1, 2): if 0 <= r + i < self.rows and 0 <= c + j < self.columns: self.reveal(r + i, c + j) def check_win(self): for r in range(self.rows): for c in range(self.columns): if not self.buttons[(r, c)]['is_mine'] and self.buttons[(r, c)]['button']['state'] != "disabled": return False return True def game_over(self, won): for r in range(self.rows): for c in range(self.columns): if self.buttons[(r, c)]['is_mine']: self.buttons[(r, c)]['button'].config(text="*", fg="red") if won: result = "你贏了!" else: result = "你輸了!" self.end_game_message(result) def end_game_message(self, result): top = tk.Toplevel(self.root) top.title("遊戲結束") msg = tk.Label(top, text=result, font=("Arial", 16)) msg.pack(pady=20) button = tk.Button(top, text="再玩一次", command=self.restart_game) button.pack(pady=10) top.transient(self.root) top.grab_set() self.root.wait_window(top) def restart_game(self): for widget in self.root.winfo_children(): widget.destroy() self.__init__(self.root) if __name__ == "__main__": root = tk.Tk() game = Minesweeper(root) root.mainloop()