-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
executable file
·42 lines (36 loc) · 1.41 KB
/
menu.py
File metadata and controls
executable file
·42 lines (36 loc) · 1.41 KB
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
import pygame
class Menu:
def __init__(self, algorithms, width, height=40):
"""
algorithms: list of strings (e.g., ['A*', 'Dijkstra', 'BFS'])
width: window width
height: menu bar height
"""
self.algorithms = algorithms
self.selected = algorithms[0] # default
self.height = height
self.width = width
self.font = pygame.font.SysFont(None, 24)
# compute button positions
self.buttons = []
btn_width = width // len(algorithms)
for i, alg in enumerate(algorithms):
rect = pygame.Rect(i*btn_width, 0, btn_width, height)
self.buttons.append((rect, alg))
def draw(self, win):
for rect, alg in self.buttons:
# color for selected
color = (100, 200, 255) if alg == self.selected else (180,180,180)
pygame.draw.rect(win, color, rect)
pygame.draw.rect(win, (0,0,0), rect, 2) # border
text_surf = self.font.render(alg, True, (0,0,0))
text_rect = text_surf.get_rect(center=rect.center)
win.blit(text_surf, text_rect)
def handle_click(self, pos):
for rect, alg in self.buttons:
if rect.collidepoint(pos):
self.selected = alg
return True
return False
def get_selected(self):
return self.selected