mirror of https://git.sr.ht/~michalr/menu
Added quit button
This commit is contained in:
parent
7084a51b0a
commit
0ab21fabc0
11
menu.py
11
menu.py
|
@ -11,6 +11,7 @@ import pygame.freetype
|
||||||
|
|
||||||
class ButtonDef(TypedDict):
|
class ButtonDef(TypedDict):
|
||||||
text: str
|
text: str
|
||||||
|
cbk: Callable[[], None]
|
||||||
|
|
||||||
|
|
||||||
class IBoard:
|
class IBoard:
|
||||||
|
@ -68,7 +69,7 @@ class MenuBoard(IBoard):
|
||||||
button_positions = self.generate_button_positions(screen_rect)
|
button_positions = self.generate_button_positions(screen_rect)
|
||||||
self.buttons = list(map(lambda d: Button(next(button_positions),
|
self.buttons = list(map(lambda d: Button(next(button_positions),
|
||||||
f"Button {d['text']}",
|
f"Button {d['text']}",
|
||||||
lambda: print(d['text'])), buttons))
|
d['cbk']), buttons))
|
||||||
|
|
||||||
def generate_button_positions(self, screen_rect: pygame.Rect):
|
def generate_button_positions(self, screen_rect: pygame.Rect):
|
||||||
current_button_row = 0
|
current_button_row = 0
|
||||||
|
@ -102,8 +103,9 @@ class App:
|
||||||
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
|
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
self.running = False
|
self.running = False
|
||||||
self.board: IBoard = MenuBoard(pygame.Rect(0, 0, info.current_w, info.current_h),
|
buttons = list(map(lambda t: ButtonDef(text=f"{t}", cbk=lambda: print(t)), list(range(btns))))
|
||||||
list(map(lambda t: ButtonDef(text=f"{t}"), list(range(btns)))))
|
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
|
||||||
|
self.board: IBoard = MenuBoard(pygame.Rect(0, 0, info.current_w, info.current_h), buttons)
|
||||||
|
|
||||||
def loop(self):
|
def loop(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
|
@ -122,6 +124,9 @@ class App:
|
||||||
self.clock.tick(30)
|
self.clock.tick(30)
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
def quit(self):
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = App(int(sys.argv[1]))
|
app = App(int(sys.argv[1]))
|
||||||
|
|
Loading…
Reference in New Issue