-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanels.py
76 lines (67 loc) · 2.59 KB
/
panels.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
# panels.py
import pygame
from loguru import logger
from game import Bomberdude
class Mainmenu:
def __init__(self, screen, args):
self.screen = screen
self.args = args
self.options = ["Start", "Setup", "Quit"]
self.selected_option = 0
self.font = pygame.font.Font(None, 36)
self.running = True
self.option_rects = []
def draw(self):
self.screen.fill((0, 0, 0))
self.option_rects = []
for i, option in enumerate(self.options):
color = (255, 0, 0) if i == self.selected_option else (255, 255, 255)
text = self.font.render(option, True, color)
rect = text.get_rect(center=(self.screen.get_width() // 2, 150 + i * 50))
self.screen.blit(text, rect)
self.option_rects.append(rect)
pygame.display.flip()
def handle_input(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.selected_option = (self.selected_option - 1) % len(self.options)
elif event.key == pygame.K_DOWN:
self.selected_option = (self.selected_option + 1) % len(self.options)
elif event.key == pygame.K_RETURN:
return self.select_option()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
return self.handle_mouse_click(event.pos)
return None
def handle_mouse_click(self, mouse_pos):
for i, rect in enumerate(self.option_rects):
if rect.collidepoint(mouse_pos):
self.selected_option = i
return self.select_option()
return None
def select_option(self):
if self.options[self.selected_option] == "Start":
return "start"
elif self.options[self.selected_option] == "Setup":
return "setup"
elif self.options[self.selected_option] == "Quit":
self.running = False
return None
def run(self):
while self.running:
self.draw()
action = self.handle_input()
if action:
return action
return None
class Panel:
def __init__(self, screen, position, size, color):
self.screen = screen
self.position = position
self.size = size
self.color = color
def draw(self):
pygame.draw.rect(self.screen, self.color, (*self.position, *self.size))