Skip to content

Commit 508815e

Browse files
authored
Merge pull request #3032 from makermelissa/main
Minesweeper improvements
2 parents f504199 + a30d244 commit 508815e

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

Metro/Metro_RP2350_Minesweeper/code.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,15 @@ def reset():
197197
def set_difficulty(diff):
198198
game_logic.difficulty = diff
199199
reset()
200+
difficulty_menu.select_item(DIFFICULTIES[diff]['label'].lower().replace(" ", "_"))
200201

201202
def hide_group(group):
202203
group.hidden = True
203204

204205
for i, difficulty in enumerate(DIFFICULTIES):
205206
# Create a button for each difficulty
206-
difficulty_menu.add_item((set_difficulty, i), difficulty['label'])
207+
selected = i == game_logic.difficulty
208+
difficulty_menu.add_item((set_difficulty, i), difficulty['label'], selected)
207209

208210
reset_menu.add_item(reset, "OK")
209211

Metro/Metro_RP2350_Minesweeper/gamelogic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ def reset(self):
6868
if (self.grid_width * 16 > self._display.width or
6969
self.grid_height * 16 > self._display.height - INFO_BAR_HEIGHT):
7070
raise ValueError("Grid size exceeds display size")
71-
self._board_data = bytearray(self.grid_width * self.grid_height)
7271
self._mine_count = DIFFICULTIES[self._difficulty]['mines']
72+
if self._mine_count > (self.grid_width - 1) * (self.grid_height - 1):
73+
raise ValueError("Too many mines for grid size")
74+
if self._mine_count < 10:
75+
raise ValueError("There must be at least 10 mines")
76+
self._board_data = bytearray(self.grid_width * self.grid_height)
7377
self._status = STATUS_NEWGAME
7478
self._start_time = None
7579
self._end_time = None
@@ -241,7 +245,6 @@ def elapsed_time(self):
241245
if self._start_time is None:
242246
return 0
243247
if self._end_time is None:
244-
print(ticks_ms() / 1000, self._start_time / 1000)
245248
return min(999, (ticks_ms() - self._start_time) // 1000)
246249
return min(999, (self._end_time - self._start_time) // 1000)
247250

Metro/Metro_RP2350_Minesweeper/menu.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,40 @@ def __init__(self, label, button_width, menu_width, x, y):
3636
self._menu_items = []
3737
self._root_button = None
3838

39-
def add_item(self, function, label):
39+
def add_item(self, function, label, selected=False):
40+
key = label.lower().replace(" ", "_")
4041
self._menu_items.append(
4142
{
43+
"key": key,
4244
"function": function,
4345
"label": label,
46+
"selected": selected,
4447
}
4548
)
4649
self._render()
4750

51+
def select_item(self, key):
52+
for item in self._menu_items:
53+
if item["key"] == key:
54+
item["selected"] = True
55+
else:
56+
item["selected"] = False
57+
self._render()
58+
4859
@staticmethod
49-
def _create_button(callback, label, width, x, y=0, border=True):
60+
def _create_button(callback, label, width, x, y=0, border=True, selected=False):
5061
if border:
5162
outline_color = 0x000000
5263
selected_outline = 0x333333
5364
else:
5465
outline_color = 0xEEEEEE
5566
selected_outline = 0xBBBBBB
5667

68+
if selected:
69+
selected_label = label_color = 0x008800
70+
else:
71+
selected_label = label_color = 0x333333
72+
5773
button = EventButton(
5874
callback,
5975
x=x,
@@ -64,13 +80,14 @@ def _create_button(callback, label, width, x, y=0, border=True):
6480
style=EventButton.RECT,
6581
fill_color=0xEEEEEE,
6682
outline_color=outline_color,
67-
label_color=0x333333,
83+
label_color=label_color,
6884
selected_fill=0xBBBBBB,
69-
selected_label=0x333333,
85+
selected_label=selected_label,
7086
selected_outline=selected_outline,
7187
)
7288
return button
7389

90+
7491
def _toggle_submenu(self):
7592
self._menu_items_group.hidden = not self._menu_items_group.hidden
7693

@@ -87,7 +104,7 @@ def _render(self):
87104
self._button_width,
88105
self._xpos,
89106
self._ypos,
90-
border=True,
107+
True,
91108
)
92109
self.append(self._root_button)
93110

@@ -113,7 +130,8 @@ def _render(self):
113130
self._menu_width - 2,
114131
self._xpos + 1,
115132
self._ypos + index * MENU_ITEM_HEIGHT + self._root_button.height,
116-
border=False,
133+
False,
134+
item["selected"],
117135
)
118136
self._menu_items_group.append(button)
119137

0 commit comments

Comments
 (0)