-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMine Blower.py
300 lines (246 loc) · 8.3 KB
/
Mine Blower.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import tkinter
import random
import time
import platform
#The emojis!
warning_expanded = u"\u26A0\uFE0F"
warning = "⚠️"
death_expanded = u"\u2620\uFE0F"
death = "☠️"
clock = "⏱"
def square_to_widget(row, column):
global main_frame
widget = main_frame.grid_slaves(row, column)[0]
return widget
def square_open(r, c, text, auto=True):
global first_mine
tk_square = square_to_widget(r, c)
if text == death:
square_text = tk_square.cget("text")
if auto == False:
bg = "red"
elif warning in square_text:
bg = "light blue"
else:
bg = "#808080"
else:
bg = "light blue"
if text == death and first_mine:
# Avoid tkinter (3.7.3) on windows bug
# when displaying skull with emoji variant selector.
# Displaying warning (with variant selector) first, avoids the issue.
first_mine = False
tk_square.config(bg = bg, text = warning, height=1, width=2)
tk_square.config(bg = bg, text = text, height=1, width=2)
def update_clock():
global timer_label
global time_played
global timer
if not gameOver:
timer_label.configure(text=clock +str(time_played))
time_played = time_played + 1
timer = window.after(1000, update_clock)
def init():
global gameOver
global time_played
global first_click
global first_mine
global timer
gameOver = False
time_played = 0
first_click = True
first_mine = True
timer = None
def start_bombdodger():
global window
window = tkinter.Tk()
window.title("Mine Blower")
play_bombdodger()
def play_bombdodger():
init()
create_bombfield()
layout_window(window)
window.mainloop()
def show_bombs_left():
global bombs_label
global bombs_left
global first_click
number = bombs_left
bombs_label.config(text=death + str(number))
def create_bombfield():
global squares_left
global bombfield
global bombs_left
rows = 10
cols = 10
bombs_left = int(rows*cols*.16) # 16% is a good default
squares_left = 0
bombfield = []
bombs = list(range(rows*cols))
random.shuffle(bombs)
bombs = bombs[0:bombs_left]
for row in range(0,rows):
rowList = []
for column in range(0,cols):
if row*cols + column in bombs:
rowList.append(1)
else:
rowList.append(0)
squares_left = squares_left + 1
bombfield.append(rowList)
#printfield(bombfield)
def printfield(bombfield):
for rowList in bombfield:
print(rowList)
def restart():
global timer
if timer:
window.after_cancel(timer)
main_frame.destroy()
top_frame.destroy()
play_bombdodger()
def layout_window(window):
global top_frame
global main_frame
top_frame = tkinter.Frame(window)
main_frame = tkinter.Frame(window)
top_frame.grid(row=0, sticky="")
main_frame.grid(row=1, sticky="nsew")
global timer_label
timer_label = tkinter.Label(top_frame, text=clock + "0", fg="black", font=("Phosphate", 18),
anchor="w", width=5, relief=tkinter.SUNKEN, borderwidth=3)
timer_label.grid(row=0, column=0)
restart_button = tkinter.Button(top_frame, text ="Retry", command = restart,
anchor="center", width=5)
restart_button.grid(row=0, column=1, padx=(30,30))
global bombs_label
bombs_label = tkinter.Label(top_frame, text="", fg="black", font=("Phosphate", 18),
anchor="w", width=5, relief=tkinter.SUNKEN, borderwidth=3)
bombs_label.grid(row=0, column=2)
show_bombs_left()
for rowNumber, rowList in enumerate(bombfield):
for columnNumber, columEntry in enumerate(rowList):
if random.randint(1,100) < 25:
bgcolor = "darkgreen"
elif random.randint(1,100) > 75:
bgcolor = "seagreen"
else:
bgcolor = "green"
square = tkinter.Label(main_frame, relief="raised", bg = bgcolor, height=1, width=2, font=("Arial", 20))
square.grid(row = rowNumber, column = columnNumber)
square.grid(row = rowNumber, column = columnNumber)
square.bind("<Button-1>", on_click)
square.bind("<Control-Button-1>", on_right_click)
if platform.system() == "Darwin":
square.bind("<Button-2>", on_right_click)
else:
square.bind("<Button-2>", on_click)
square.bind("<Button-3>", on_right_click)
def check_game_over():
global squares_left
global gameOver
if squares_left == 0:
gameOver = True
def surrounding_squares(row, column):
max_row = 9
max_col = 9
squares = []
if row < max_row:
squares.append((row+1, column))
if row > 0:
squares.append((row-1, column))
if column > 0:
squares.append((row, column-1))
if column < max_col:
squares.append((row, column+1))
if row > 0 and column > 0:
squares.append((row-1, column-1))
if row < max_row and column > 0:
squares.append((row+1, column-1))
if row > 0 and column < max_col:
squares.append((row-1, column+1))
if row < max_row and column < max_col:
squares.append((row+1, column+1))
return squares
def on_click(event):
square = event.widget
click(square)
def click(square, auto=False, click_flag=False):
global gameOver
global squares_left
global first_click
global main_frame
if gameOver:
return
row = int(square.grid_info()["row"])
column = int(square.grid_info()["column"])
currentText = square.cget("text")
if first_click:
#Ensure first click is never a bomb!
first_click = False
while bombfield[row][column] == 1:
create_bombfield()
show_bombs_left()
update_clock()
if warning in currentText:
if click_flag==True:
right_click(square)
click(square)
elif bombfield[row][column] == 1:
gameOver = True
red = "#FF6040"
for r in range(0,10):
for c in range(0,10):
if bombfield[r][c] == 1:
square_open(r, c, text=death, auto = (r != row or c != column))
elif currentText == "":
totalBombs = 0
squares_around = surrounding_squares(row, column)
for r,c in squares_around:
totalBombs += bombfield[r][c]
if totalBombs != 0:
num_text = str(totalBombs)
else:
num_text = ""
square_open(row, column, text = " " + num_text + " ", auto=False)
squares_left = squares_left - 1
check_game_over()
if totalBombs == 0:
for r,c in squares_around:
tk_square = square_to_widget(r, c)
click(tk_square, auto=True, click_flag=True)
elif auto == False and currentText != " ": # clicking on a number
squares_around = surrounding_squares(row, column)
flag_count = 0
for r,c in squares_around:
tk_square = square_to_widget(r, c)
square_text = tk_square.cget("text")
if warning in square_text:
flag_count = flag_count + 1
if int(currentText) == flag_count:
for r,c in squares_around:
tk_square = square_to_widget(r, c)
click(tk_square, auto=True)
def on_right_click(event):
square = event.widget
right_click(square)
def right_click(square):
global bombs_left
global squares_left
global first_click
if gameOver:
return
currentText = square.cget("text")
if warning in currentText:
square.config(bg = "green", text = "", height=1, width=2)
bombs_left = bombs_left + 1
squares_left = squares_left + 1
elif currentText == "" and not first_click and bombs_left > 0:
row = int(square.grid_info()["row"])
column = int(square.grid_info()["column"])
square.config(bg = "light blue", height=1, width=2, text = warning)
bombs_left = bombs_left - 1
squares_left = squares_left - 1
check_game_over()
show_bombs_left()
start_bombdodger()