-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
466 lines (379 loc) · 15.8 KB
/
main.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import kivy
from random import Random
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty,StringProperty
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.core.audio import Sound, SoundLoader
from time import clock
import ast
import re
from kivy.storage.jsonstore import JsonStore
from kivy.core.window import Window
import copy
Rows_For_First_Level = 3
Max_Level = 15
'''
class BButton(Button):
def __init__(self,box,**kwargs):
super(BButton,self).__init__(**kwargs)
self.box = box
self.isMarking = False
def on_press(self):
self.press()
def press(self):
if self.box.root.gameover:
return
self.isMarking = self.box.root.status_bar.toggle_mark.state=='down'
if self.isMarking:
if self.image.opacity == 1:
self.MarkNormal()
else:
self.MarkB()
else:
self.box.Clear()
def MarkB(self):
self.image.opacity = 1
self.box.state=1
if self.box.root.mute == False:
self.box.root.sounds['state'].play()
if self.box.root.gameover == False:
self.box.root.bfound += 1
def MarkNormal(self):
self.image.opacity = 0
self.box.state=0
if self.box.root.mute == False:
self.box.root.sounds['state'].play()
if self.box.root.gameover == False:
self.box.root.bfound -= 1
class BLabel(Label):
pass
'''
class BBox():
BNumber = NumericProperty(0)
def __init__(self,root,**kwargs):
self.isBomb = False
self.BNumber = 0
self.root = root
self.isClear = False
self.row = 0
self.col = 0
self.state=0 # 0 means normal, 1 means marked as bomb, -1 means cleared already
self.pos = (0,0)
self.size = (0,0)
def Clear(self,isShowAll=False):
'''
triggered when user click the button and think it's empty or has a number
'''
if self.isBomb and self.state==0:
'''
A Bomb, game over
'''
if isShowAll == False:
self.root.GameOver()
self.Explode()
#self.bbutton.image.source='gameoverflag.png'
else:
self.MarkNumberOrEmpty()
self.root.CheckSucceed()
def Explode(self):
'''
Animation needed
'''
if self.root.mute == False:
self.root.sounds['bomb'].play()
self.root.ShowAll()
def MarkNumberOrEmpty(self):
'''
Not only mark itself as empty, also mark all around boxes if they are empty
'''
if self.state == 1:
return
if self.BNumber == 0 and self.isClear == False:
self.isClear = True
self.root.Clear(self.row - 1,self.col - 1)
self.root.Clear(self.row - 1,self.col)
self.root.Clear(self.row - 1,self.col + 1)
self.root.Clear(self.row,self.col - 1)
self.root.Clear(self.row,self.col + 1)
self.root.Clear(self.row + 1,self.col - 1)
self.root.Clear(self.row + 1,self.col)
self.root.Clear(self.row + 1,self.col + 1)
elif self.BNumber > 0 and self.isClear == False:
self.isClear = True
self.state = -1
if self.root.mute == False:
self.root.sounds['state'].play()
class PlayArea(Widget):
pass
class FindBWidget(BoxLayout):
level=NumericProperty(1)
bfound = NumericProperty(0)
bnumber = NumericProperty(0)
def __init__(self,**kwargs):
super(FindBWidget,self).__init__(**kwargs)
self.BBoxList=[]
self.store = JsonStore("findb.json")
self.gridSize_height = 0
self.gridSize_width = 0
self.bnumber = 0
self.badded = 0
self.bfound = 0
self.mute=True
self.gameover = False
self.custimize = False
self.custimize_height = 0
self.custimize_width = 0
self.custimize_brate = 0.2
self.sounds = {'bomb':SoundLoader.load('bomb.wav'),
'state':SoundLoader.load('state.wav'),
'upgrade':SoundLoader.load('upgrade.mp3')}
self.start_clock = clock()
self.config = None
def on_bfound(self,instance,value):
self.status_bar.label_left_bomb.text = '{}'.format(self.bnumber - self.bfound)
self.CheckSucceed()
def on_bnumber(self,instance,value):
self.status_bar.label_left_bomb.text = '{}'.format(self.bnumber - self.bfound)
def Restart(self):
self.custimize,self.custimize_height,self.custimize_width,self.custimize_brate = self.get_customize_info()
self.level,self.levels_info = self.get_level_info()
self.mute = self.get_mute_info()
self.BBoxList = []
if self.custimize == True:
self.status_bar.label_level.text = 'L:C'
self.gridSize_height = self.custimize_height
self.gridSize_width = self.custimize_width
if self.gridSize_height < 5:
self.gridSize_height = 5
if self.gridSize_width < 5:
self.gridSize_width = 5
if self.gridSize_height > 50:
self.gridSize_height = 50
if self.gridSize_width > 50:
self.gridSize_width = 50
else:
self.status_bar.label_level.text = 'L:{}'.format(self.level)
self.gridSize_width = self.level + Rows_For_First_Level
self.gridSize_height = self.level + Rows_For_First_Level
self.bnumber = 0
self.badded = 0
self.bfound = 0
self.start_clock = clock()
self.gameover = False
self.play_area.clear_widgets()
self.play_area.cols = self.gridSize_width
self.play_area.rows = self.gridSize_height
'''
for i in range(0,self.gridSize_height):
for j in range(0,self.gridSize_width):
b = BBox(root=self)
b.row=i
b.col=j
self.BBoxList.append(b)
self.play_area.add_widget(b)
'''
self._calculate_bombs()
self.status_bar.toggle_mark.disable = False
self.status_bar.toggle_mark.state = "normal"
self.status_bar.button_reset.image.source='smile.png'
def _calculate_bombs(self):
if self.custimize:
self.brate = self.custimize_brate/100.0
if self.brate < 0.05:
self.brate = 0.05
if self.brate > 0.8:
self.brate = 0.8
elif self.level < 6:
self.brate = 0.11
elif self.level < 11:
self.brate = 0.13
elif self.level < 20:
self.brate = 0.15
elif self.level < 30:
self.brate = 0.18
else:
self.brate = 0.2
self.bnumber = int(self.brate * self.gridSize_width * self.gridSize_height)
self.badded = 0
while True:
if self._add_bomb():
self.badded += 1
if self.badded >= self.bnumber:
break
def _add_bomb(self):
if len(self.BBoxList) <=0:
return
i = Random().randint(0,len(self.BBoxList) - 1)
if self.BBoxList[i].isBomb:
return False
else:
self.BBoxList[i].isBomb = True
#set bomb number of the around boxes
row = self.BBoxList[i].row
col = self.BBoxList[i].col
self._add_bomb_number(row - 1,col - 1)
self._add_bomb_number(row - 1,col)
self._add_bomb_number(row - 1,col + 1)
self._add_bomb_number(row,col - 1)
self._add_bomb_number(row,col + 1)
self._add_bomb_number(row + 1,col - 1)
self._add_bomb_number(row + 1,col)
self._add_bomb_number(row + 1,col + 1)
return True
def _add_bomb_number(self,row,col):
if row < 0 or row >= self.gridSize_width or col < 0 or col >= self.gridSize_height:
return
index = row*self.gridSize_width + col
if index < 0 or index >= len(self.BBoxList):
return
self.BBoxList[index].BNumber += 1
def Clear(self,row,col):
if row < 0 or row >= self.gridSize_width or col < 0 or col >= self.gridSize_height:
return
index = row*self.gridSize_width + col
if index < 0 or index >= len(self.BBoxList):
return
self.BBoxList[index].MarkNumberOrEmpty()
def ShowAll(self):
self.status_bar.toggle_mark.state='normal'
for i in range(0,len(self.BBoxList)):
if self.BBoxList[i].isBomb:
self.BBoxList[i].bbutton.MarkB()
else:
self.BBoxList[i].Clear()
def CheckSucceed(self):
if self.bfound > 0 and self.bfound == self.bnumber:
for i in range(0,len(self.BBoxList)):
if self.BBoxList[i].state == 0:
return False
#upgrade level
if self.mute == False:
self.sounds['upgrade'].play()
if self.custimize == False:
duration = round((clock() - self.start_clock),2)
if self.levels_info.has_key(self.level) and self.levels_info[self.level] > 0:
if self.levels_info[self.level] > duration:
self.levels_info[self.level] = duration
else:
self.levels_info[self.level] = duration
if self.level < Max_Level:
self.level += 1
self.store_user_data(self.level,self.levels_info)
self.Restart()
return True
else:
return False
def get_level_info(self):
if self.store.exists("UserData") == False:
self.store.put("UserData",CurrentLevel=1,Levels={})
return self.store.get("UserData")["CurrentLevel"],self.store.get("UserData")["Levels"]
def get_mute_info(self):
return self.config.get('Sounds','Mute') == '1'
def get_customize_info(self):
return self.config.get('Customization','Enable') == '1',self.config.getint('Customization','C_Height'),self.config.getint('Customization','C_Width'),self.config.getint('Customization','Rate')
def store_user_data(self,currentLevel,levels):
self.store.put("UserData",CurrentLevel=currentLevel,Levels=levels)
def GameOver(self):
self.status_bar.toggle_mark.disable = True
self.status_bar.button_reset.image.source='gameover.png'
self.gameover = True
class MinesApp(App):
use_kivy_settings = False
clearcolor = (0.2, 0.2, 0.2, 1)
title = 'Find Bombs'
def build(self):
findb = FindBWidget()
findb.config = self.config
findb.Restart()
return findb
def on_pause(self):
return True
def on_resume(self):
pass
def build_config(self, config):
try:
config.get('Sounds','Mute')
except:
config.setdefaults('Sounds',{
'Mute':True
}
)
try:
config.get('Customization','Rate')
except:
config.setdefaults('Customization',{
'Enable':False,
'C_Height':10,
'C_Width':10,
'Rate':20,
'FullScreen':False
}
)
def build_settings(self, settings):
jsondata = """[
{"type":"title",
"title":"Sounds"
},
{ "type": "bool",
"title": "Mute",
"desc": "Play without sound",
"section": "Sounds",
"key": "Mute"},
{"type":"title",
"title":"Customize Boxes"
},
{ "type": "bool",
"title": "Customize",
"desc": "Play your own customization",
"section": "Customization",
"key": "Enable"},
{ "type": "numeric",
"title": "Height",
"desc": "How many box you want as height",
"section": "Customization",
"key": "C_Height" },
{ "type": "numeric",
"title": "Width",
"desc": "How many box you want as width",
"section": "Customization",
"key": "C_Width" },
{ "type": "numeric",
"title": "Rate of Bomb",
"desc": "How many bomb you will have in percentage",
"section": "Customization",
"key": "Rate" },
{"type":"title",
"title":"Full Screen"
},
{ "type": "bool",
"title": "Full Screen",
"section": "Customization",
"key": "FullScreen"}
]"""
settings.add_json_panel('Find Bomb',self.config,data=jsondata)
def on_config_change(self, config, section, key, value):
if config is self.config:
token = (section, key)
if token == ('Sounds', 'Mute'):
self.root.mute = (value == '1')
elif token == ('Customization','Enable'):
self.root.customize = (value == '1')
elif token == ('Customization','C_Height'):
self.root.customize_height = value
elif token == ('Customization','C_Width'):
self.root.customize_width = value
elif token == ('Customization','Rate'):
self.root.customize_rate = value
elif token == ('Customization','FullScreen'):
Window.toggle_fullscreen()
if __name__ == '__main__':
mines = MinesApp()
mines.run()