-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path掃地雷.py
127 lines (109 loc) · 4.19 KB
/
掃地雷.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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('<Button-1>', lambda e, row=i, col=j: self.click(row, col))
button.bind('<Button-3>', 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()