-
Notifications
You must be signed in to change notification settings - Fork 1
/
kivy-mines.py
346 lines (294 loc) · 12.8 KB
/
kivy-mines.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
from datetime import datetime
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, SlideTransition
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.utils import get_color_from_hex
from kivy.clock import Clock
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.base import EventLoop
from kivy.properties import (NumericProperty, ListProperty, BooleanProperty, StringProperty, ObjectProperty)
from config import HOVER, NORMAL, RED, COLOR_PALETTE, DB, DEF_USER
from mine import Mine
EventLoop.ensure_window()
class CustomPopup(Popup):
score_board = []
editable_text = ObjectProperty()
def __init__(self, horizontal='', vertical='', spend='', level='', **kwargs):
super(CustomPopup, self).__init__(**kwargs)
self.board_h = horizontal
self.board_v = vertical
self.level = level
self.spend = spend
self.separator_height = 0
self.background = ""
grid = self.children[0]
grid.padding = (0, 0, 0, 0)
box = None
title = None
for ch in grid.children:
if str(ch).find('Label') != -1:
ch.color = 0, 0, 0, 1
ch.bold = True
title = CustomLabel(text=ch.text, color=ch.color, bold=ch.bold,
size_hint=(1, None), height=20, padding=(5, 0))
color = filter(lambda x: str(x).find('Color') != -1, title.canvas.before.children)[0]
color.rgba = (1, 1, 1, 1)
grid.remove_widget(ch)
elif str(ch).find('Widget') != -1:
ch.size = (0, 0)
elif str(ch).find('BoxLayout') != -1:
grid.remove_widget(ch)
box = ch
else:
grid.remove_widget(ch)
if title:
seperator = CustomLabel(size_hint=(1, None), height=2)
color = filter(lambda x: str(x).find('Color') != -1, seperator.canvas.before.children)[0]
color.rgba = HOVER
grid.add_widget(title)
grid.add_widget(seperator)
if box:
box.clear_widgets()
try:
board = DB.store_get('%sx%s-%s' % (self.board_h, self.board_v, self.level))
except KeyError:
board = []
board.append({'name': DEF_USER, 'spend': self.spend, 'new': True})
board = sorted(board, key=lambda x: x['spend'])
scroll = ScrollView()
pre_box = GridLayout(cols=1, spacing=2, padding=(2, 2, 2, 0), size_hint_y=None)
pre_box.bind(minimum_height=pre_box.setter('height'))
for val in board:
tmp_box = BoxLayout(orientation='horizontal', size_hint=(1, None), height=20, pos_hint={'top': 1})
if 'new' in val:
label_name = TextInput(text=DEF_USER, focus=True, cursor_blink=True)
label_name.bind(on_text_validate=self.dismiss)
self.editable_text = label_name
else:
label_name = CustomLabel(text=val['name'], color=(0, 0, 0, 1))
label_spend = CustomLabel(text=':%s' % val['spend'], color=(0, 0, 0, 1))
tmp_box.add_widget(label_name)
tmp_box.add_widget(label_spend)
pre_box.add_widget(tmp_box)
scroll.add_widget(pre_box)
box.add_widget(scroll)
box.padding = (0, 0, 0, 6)
grid.add_widget(box)
self.score_board = board
def on_dismiss(self, *args):
for val in self.score_board:
if 'new' in val:
text = self.editable_text.text.strip()
val['name'] = text and text or DEF_USER
val.pop('new')
DB.store_put('%sx%s-%s' % (self.board_h, self.board_v, self.level), self.score_board)
DB.store_sync()
super(CustomPopup, self).on_dismiss(*args)
class CustomLabel(Label):
pass
class BoardButton(Button):
explode_image = "assets/mine_explode.png"
exploded_image = "assets/mine_exploded.png"
flag_image = "assets/flag.png"
trans_image = "assets/trans.png"
flagged = False
pressed = False
def __init__(self, hidden, line_index, col_index, **kwargs):
super(BoardButton, self).__init__(**kwargs)
self.hidden = hidden
self.line_index = line_index
self.col_index = col_index
def get_neighbours(self):
line_index, col_index = self.line_index, self.col_index
top_left = line_index - 1, col_index - 1
top = line_index - 1, col_index
top_right = line_index - 1, col_index + 1
left = line_index, col_index - 1
right = line_index, col_index + 1
bot_left = line_index + 1, col_index - 1
bot = line_index + 1, col_index
bot_right = line_index + 1, col_index + 1
return [top_left, top, top_right,
left, right,
bot_left, bot, bot_right]
def clear_flag(self):
self.flagged = False
img = self.img
img.source = self.trans_image
self.clear_widgets()
self.add_widget(img)
class KivyMines(ScreenManager):
board = ListProperty()
horizontal = NumericProperty()
vertical = NumericProperty()
level = NumericProperty(5)
bomb_count = NumericProperty(0)
found_bombs = NumericProperty(0)
game_on = BooleanProperty(False)
game_since = StringProperty("0:00:00")
game_at = None
popup = None
def hover(self, *args):
mouse_position = args[1]
if self.current == "board_selection":
obj = self.board_selection
else:
obj = self.board_screen.board
for but in filter(lambda x: not x.pressed and not x.disabled, obj.children):
x1, x2 = but.pos[0], but.pos[0] + but.width
y1, y2 = but.pos[1], but.pos[1] + but.height
if x1 < mouse_position[0] < x2 and y1 < mouse_position[1] < y2:
but.background_color = HOVER
else:
but.background_color = NORMAL
def set_level(self, level):
self.level = level
def bomb_all(self):
board = self.current_screen.board
for cell in board.children:
if cell.hidden == -1 and not cell.pressed and not cell.flagged:
cell.clear_flag()
cell.img.source = cell.exploded_image
cell.disabled = True
self.game_on = False
def counter(self):
if self.game_on and self.game_at:
self.game_since = str(datetime.now() - self.game_at).rsplit(".", 1)[0]
Clock.schedule_once(lambda dt: self.counter(), .5)
def disable_buttons(self, button):
if button.hidden == 0:
button.text = ""
else:
button.text = "[b][color=%s]%s[/color][/b]" % (COLOR_PALETTE[button.hidden], button.hidden)
button.background_color = HOVER
button.pressed = True
positions = button.get_neighbours()
for line, col in positions:
if -1 < line < self.horizontal and -1 < col < self.vertical:
button = \
filter(lambda x: x.line_index == line and x.col_index == col, self.current_screen.board.children)[0]
if int(button.hidden) == 0 and not button.pressed:
button.clear_flag()
self.disable_buttons(button)
elif int(button.hidden) > 0:
button.clear_flag()
button.text = "[b][color=%s]%s[/color][/b]" % (COLOR_PALETTE[button.hidden], button.hidden)
button.background_color = HOVER
button.pressed = True
def lock_buttons(self):
buttons = self.current_screen.board.children
for but in buttons:
if not but.pressed:
self.board_click(but, check=False)
but.disabled = True
def check_complete(self):
flagged = len(filter(lambda x: x.flagged, self.current_screen.board.children))
count = len(filter(lambda x: x.flagged and x.hidden == -1, self.current_screen.board.children))
if count == self.bomb_count == flagged:
self.lock_buttons()
self.game_on = False
if not self.popup:
label = CustomLabel(text='', font_size=40)
self.popup = CustomPopup(content=label, title="Congrats...", spend=self.game_since,
horizontal=self.horizontal, vertical=self.vertical,
level=self.level, size_hint=(None, None), size=(250, 300))
self.popup.open()
def board_click(self, *args, **kwargs):
button = args[0]
check = kwargs.get('check', True)
auto = kwargs.get('auto', False)
if not self.game_on and check:
self.game_on = True
self.game_at = datetime.now()
self.counter()
if button.pressed and button.hidden > 0:
positions = button.get_neighbours()
neighbours = []
for line, col in positions:
if -1 < line < self.horizontal and -1 < col < self.vertical:
neighbour = \
filter(lambda x: x.line_index == line and x.col_index == col,
self.current_screen.board.children)[0]
neighbours.append(neighbour)
if len(filter(lambda x: x.flagged, neighbours)) == button.hidden:
for but in filter(lambda x: not x.flagged and not x.pressed, neighbours):
self.board_click(but, auto=True)
else:
if hasattr(button.last_touch, 'multitouch_sim') and check and not auto and not button.pressed:
if button.flagged:
button.clear_flag()
self.found_bombs -= 1
else:
button.text = ""
button.img.source = button.flag_image
button.flagged = True
self.found_bombs += 1
else:
if button.flagged:
pass
elif button.hidden == -1:
button.img.source = button.explode_image
button.background_color = RED
button.pressed = True
self.bomb_all()
elif button.hidden == 0:
self.disable_buttons(button)
else:
button.text = "[b][color=%s]%s[/color][/b]" % (COLOR_PALETTE[button.hidden], button.hidden)
button.background_color = HOVER
button.pressed = True
if hasattr(button.last_touch, 'multitouch_sim'):
button.last_touch.multitouch_sim = False
if check:
self.check_complete()
def switch_screen(self, screen, direction='left'):
self.popup = None
self.found_bombs = 0
self.game_on = False
self.game_at = None
self.game_since = "0:00:01"
self.transition = SlideTransition(direction=direction)
self.current = screen
def prepare_board(self, index=0):
screen_board = self.current_screen.board
if index >= len(self.board):
for cell in screen_board.children:
cell.disabled = False
else:
line_index = index / self.vertical
col_index = index % self.vertical
button = BoardButton(hidden=self.board[index],
line_index=line_index,
col_index=col_index,
disabled=True)
button.bind(on_press=self.board_click)
screen_board.add_widget(button)
Clock.schedule_once(lambda dt: self.prepare_board(index=index+1), .0000001)
def board_select(self, *args):
self.horizontal, self.vertical = map(int, args)
mine = Mine(self.horizontal, self.vertical, self.level)
self.board = map(int, mine.board.reshape(1, self.horizontal * self.vertical)[0])
self.bomb_count = len(filter(lambda x: x == -1, self.board))
self.switch_screen(screen='board_screen')
self.current_screen.board.clear_widgets()
self.prepare_board()
class KivyMinesApp(App):
def __init__(self, **kwargs):
super(KivyMinesApp, self).__init__(**kwargs)
Builder.load_file('assets/mines.kv')
self.title = 'Kivy Mines'
self.icon = 'assets/mine.png'
def build(self):
mines = KivyMines()
Window.bind(mouse_pos=mines.hover)
return mines
if __name__ == '__main__':
Window.clearcolor = (get_color_from_hex('F0F0F0'))
KivyMinesApp().run()