Skip to content

Minesweeper improvements #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Metro/Metro_RP2350_Minesweeper/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ def reset():
def set_difficulty(diff):
game_logic.difficulty = diff
reset()
difficulty_menu.select_item(DIFFICULTIES[diff]['label'].lower().replace(" ", "_"))

def hide_group(group):
group.hidden = True

for i, difficulty in enumerate(DIFFICULTIES):
# Create a button for each difficulty
difficulty_menu.add_item((set_difficulty, i), difficulty['label'])
selected = i == game_logic.difficulty
difficulty_menu.add_item((set_difficulty, i), difficulty['label'], selected)

reset_menu.add_item(reset, "OK")

Expand Down
6 changes: 5 additions & 1 deletion Metro/Metro_RP2350_Minesweeper/gamelogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ def reset(self):
if (self.grid_width * 16 > self._display.width or
self.grid_height * 16 > self._display.height - INFO_BAR_HEIGHT):
raise ValueError("Grid size exceeds display size")
self._board_data = bytearray(self.grid_width * self.grid_height)
self._mine_count = DIFFICULTIES[self._difficulty]['mines']
if self._mine_count > (self.grid_width - 1) * (self.grid_height - 1):
raise ValueError("Too many mines for grid size")
if self._mine_count < 10:
raise ValueError("There must be at least 10 mines")
self._board_data = bytearray(self.grid_width * self.grid_height)
self._status = STATUS_NEWGAME
self._start_time = None
self._end_time = None
Expand Down
29 changes: 24 additions & 5 deletions Metro/Metro_RP2350_Minesweeper/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,41 @@ def __init__(self, label, button_width, menu_width, x, y):
self._menu_items = []
self._root_button = None

def add_item(self, function, label):
def add_item(self, function, label, selected=False):
key = label.lower().replace(" ", "_")
self._menu_items.append(
{
"key": key,
"function": function,
"label": label,
"selected": selected,
}
)
self._render()

def select_item(self, key):
print(f"selecting {key}")
for item in self._menu_items:
if item["key"] == key:
item["selected"] = True
else:
item["selected"] = False
self._render()

@staticmethod
def _create_button(callback, label, width, x, y=0, border=True):
def _create_button(callback, label, width, x, y=0, border=True, selected=False):
if border:
outline_color = 0x000000
selected_outline = 0x333333
else:
outline_color = 0xEEEEEE
selected_outline = 0xBBBBBB

if selected:
label_color = 0x008800
else:
label_color = 0x333333

button = EventButton(
callback,
x=x,
Expand All @@ -64,13 +81,14 @@ def _create_button(callback, label, width, x, y=0, border=True):
style=EventButton.RECT,
fill_color=0xEEEEEE,
outline_color=outline_color,
label_color=0x333333,
label_color=label_color,
selected_fill=0xBBBBBB,
selected_label=0x333333,
selected_outline=selected_outline,
)
return button


def _toggle_submenu(self):
self._menu_items_group.hidden = not self._menu_items_group.hidden

Expand All @@ -87,7 +105,7 @@ def _render(self):
self._button_width,
self._xpos,
self._ypos,
border=True,
True,
)
self.append(self._root_button)

Expand All @@ -113,7 +131,8 @@ def _render(self):
self._menu_width - 2,
self._xpos + 1,
self._ypos + index * MENU_ITEM_HEIGHT + self._root_button.height,
border=False,
False,
item["selected"],
)
self._menu_items_group.append(button)

Expand Down