From af39a30368f4676eb446023a8019e7e599f61747 Mon Sep 17 00:00:00 2001 From: ERA Date: Sat, 16 Nov 2024 21:06:27 -0500 Subject: [PATCH] =?UTF-8?q?=E6=8E=83=E5=9C=B0=E9=9B=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 8196 bytes .../\346\216\203\345\234\260\351\233\267.md" | 181 ++++++++++++++++++ "\346\216\203\345\234\260\351\233\267.py" | 127 ++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 "Prompts/\346\216\203\345\234\260\351\233\267.md" create mode 100644 "\346\216\203\345\234\260\351\233\267.py" diff --git a/.DS_Store b/.DS_Store index 873328158d2645c2b2467d1ec670695b1e7860c9..f87c81c13f9eb38215ab12af8024a797cd4e4764 100644 GIT binary patch delta 348 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD6%nNH}hr%jz7$c**Q2SHn1>? zOy*%ZSTDek!;sIA&XCEF%n%Qx6B$YvQW=UF^cXULtXzg1&z$_^q@4UDkYPY8egZMy ze=q=wFfed3_%S#Fu?sF8OzCJkM1l4d0PV;F+Lkl<7E8XZ2-waNh9aOViWzjluE+(d zP+-ttFl5lgWiq2I$mGd(tk)C*bE5+?P6HJdFjS&BxErKsGY8vt#?9+Q!k8yE@CY)4 mUCtoE4WwN`VYFG0<2&', lambda e, row=i, col=j: self.click(row, col)) + button.bind('', lambda e, row=i, col=j: self.flag(row, col)) + row.append(button) + self.buttons.append(row) + + def place_mines(self): + # 隨機放置地雷 + mine_positions = [] + while len(mine_positions) < self.mines: + pos = (random.randint(0, self.rows-1), random.randint(0, self.cols-1)) + if pos not in mine_positions: + mine_positions.append(pos) + self.mines_positions = mine_positions + + def count_adjacent_mines(self, row, col): + # 計算周圍地雷數量 + count = 0 + for i in range(-1, 2): + for j in range(-1, 2): + r, c = row + i, col + j + if (r, c) in self.mines_positions: + count += 1 + return count + + def click(self, row, col): + if self.game_over: + return + + button = self.buttons[row][col] + + # 檢查是否點到地雷 + if (row, col) in self.mines_positions: + button.config(text="💣", bg='red') + self.game_over = True + messagebox.showinfo("遊戲結束", "踩到地雷了!") + self.reveal_all_mines() + else: + # 顯示周圍地雷數量 + mines = self.count_adjacent_mines(row, col) + button.config(text=str(mines), state='disabled') + if mines == 0: + self.reveal_empty_cells(row, col) + + def flag(self, row, col): + if self.game_over: + return + + button = self.buttons[row][col] + if button['text'] == '': + button.config(text='🚩') + elif button['text'] == '🚩': + button.config(text='') + + def reveal_empty_cells(self, row, col): + # 遞迴顯示空白格子 + for i in range(-1, 2): + for j in range(-1, 2): + r, c = row + i, col + j + if (0 <= r < self.rows and + 0 <= c < self.cols and + self.buttons[r][c]['state'] != 'disabled'): + mines = self.count_adjacent_mines(r, c) + self.buttons[r][c].config(text=str(mines), state='disabled') + if mines == 0: + self.reveal_empty_cells(r, c) + + def reveal_all_mines(self): + # 遊戲結束時顯示所有地雷 + for row, col in self.mines_positions: + self.buttons[row][col].config(text="💣", bg='red') + +def main(): + root = tk.Tk() + game = MineSweeper(root) + root.mainloop() + +if __name__ == '__main__': + main() + + +add restart dialog + + +// ... existing code ... + +class MineSweeper: + def __init__(self, master): + # ... existing init code ... + + # 添加重新開始按鈕 + self.restart_button = tk.Button(self.master, text="重新開始", command=self.restart_game) + self.restart_button.grid(row=self.rows, columnspan=self.cols, pady=10) + + def click(self, row, col): + if self.game_over: + return + + button = self.buttons[row][col] + + # 檢查是否點到地雷 + if (row, col) in self.mines_positions: + button.config(text="💣", bg='red') + self.game_over = True + self.reveal_all_mines() + # 修改遊戲結束對話框,添加重新開始選項 + if messagebox.askyesno("遊戲結束", "踩到地雷了!\n要重新開始嗎?"): + self.restart_game() + else: + # ... existing click code ... + + def restart_game(self): + # 重置所有按鈕 + for i in range(self.rows): + for j in range(self.cols): + self.buttons[i][j].config( + text="", + state="normal", + bg='SystemButtonFace' # 恢復默認背景色 + ) + + # 重置遊戲狀態 + self.game_over = False + + # 重新放置地雷 + self.place_mines() + +// ... existing code ... + + +right clikc for flag + +def create_board(self): + for i in range(self.rows): + row = [] + for j in range(self.cols): + button = tk.Button(self.master, width=2, height=1) + button.grid(row=i, column=j) + # 綁定右鍵事件到flag方法 + button.bind('', lambda e, row=i, col=j: self.flag(row, col)) + row.append(button) + self.buttons.append(row) + +def flag(self, row, col): + if self.game_over: + return + + button = self.buttons[row][col] + # 切換旗幟狀態 + if button['text'] == '': + button.config(text='🚩') + elif button['text'] == '🚩': + button.config(text='') \ No newline at end of file diff --git "a/\346\216\203\345\234\260\351\233\267.py" "b/\346\216\203\345\234\260\351\233\267.py" new file mode 100644 index 00000000..50e7cca4 --- /dev/null +++ "b/\346\216\203\345\234\260\351\233\267.py" @@ -0,0 +1,127 @@ +import tkinter as tk +from tkinter import messagebox +import random + +class MineSweeper: + def __init__(self, master): + self.master = master + self.master.title('掃雷遊戲') + self.rows = 9 + self.cols = 9 + self.mines = 10 + self.buttons = [] + self.mines_positions = [] + self.game_over = False + + # 創建遊戲區域 + self.create_board() + # 放置地雷 + self.place_mines() + + # 添加重新開始按鈕 + self.restart_button = tk.Button(self.master, text="重新開始", command=self.restart_game) + self.restart_button.grid(row=self.rows, columnspan=self.cols, pady=10) + + def create_board(self): + # 創建按鈕網格 + for i in range(self.rows): + row = [] + for j in range(self.cols): + button = tk.Button(self.master, width=2, height=1) + button.grid(row=i, column=j) + button.bind('', lambda e, row=i, col=j: self.click(row, col)) + button.bind('', lambda e, row=i, col=j: self.flag(row, col)) + row.append(button) + self.buttons.append(row) + + def place_mines(self): + # 隨機放置地雷 + mine_positions = [] + while len(mine_positions) < self.mines: + pos = (random.randint(0, self.rows-1), random.randint(0, self.cols-1)) + if pos not in mine_positions: + mine_positions.append(pos) + self.mines_positions = mine_positions + + def count_adjacent_mines(self, row, col): + # 計算周圍地雷數量 + count = 0 + for i in range(-1, 2): + for j in range(-1, 2): + r, c = row + i, col + j + if (r, c) in self.mines_positions: + count += 1 + return count + + def click(self, row, col): + if self.game_over: + return + + button = self.buttons[row][col] + + # 檢查是否點到地雷 + if (row, col) in self.mines_positions: + button.config(text="💣", bg='red') + self.game_over = True + self.reveal_all_mines() + # 修改遊戲結束對話框,添加重新開始選項 + if messagebox.askyesno("遊戲結束", "踩到地雷了!\n要重新開始嗎?"): + self.restart_game() + else: + # 顯示周圍地雷數量 + mines = self.count_adjacent_mines(row, col) + button.config(text=str(mines), state='disabled') + if mines == 0: + self.reveal_empty_cells(row, col) + + def flag(self, row, col): + if self.game_over: + return + + button = self.buttons[row][col] + if button['text'] == '': + button.config(text='🚩') + elif button['text'] == '🚩': + button.config(text='') + + def reveal_empty_cells(self, row, col): + # 遞迴顯示空白格子 + for i in range(-1, 2): + for j in range(-1, 2): + r, c = row + i, col + j + if (0 <= r < self.rows and + 0 <= c < self.cols and + self.buttons[r][c]['state'] != 'disabled'): + mines = self.count_adjacent_mines(r, c) + self.buttons[r][c].config(text=str(mines), state='disabled') + if mines == 0: + self.reveal_empty_cells(r, c) + + def reveal_all_mines(self): + # 遊戲結束時顯示所有地雷 + for row, col in self.mines_positions: + self.buttons[row][col].config(text="💣", bg='red') + + def restart_game(self): + # 重置所有按鈕 + for i in range(self.rows): + for j in range(self.cols): + self.buttons[i][j].config( + text="", + state="normal", + bg='SystemButtonFace' # 恢復默認背景色 + ) + + # 重置遊戲狀態 + self.game_over = False + + # 重新放置地雷 + self.place_mines() + +def main(): + root = tk.Tk() + game = MineSweeper(root) + root.mainloop() + +if __name__ == '__main__': + main()