Hardware Reference
In-Depth Information
The background parameter refers to the surface on which the menu
has to be created
The buttonList parameter refers to the list of buttons that we want on
the screen
4. We will get started by declaring a menu object and lising all the opions we want in
the menu, namely:
menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen,
[('Start Game', 1, None),
('Score Board', 2, None),
('Exit', 3, None)])
5.
The menu needs to be centered on the surface of the screen:
menu.set_center(True, True)
6. The text of the menu butons also needs to be aligned:
menu.set_alignment('center', 'center')
7. In order to display and update the menu and selecion parameters, a simple state
machine is used:
state = 0
prev_state = 1
8. The pygame.event.wait() method detects any keyboard or mouse events and
updates the current state accordingly.
9. The menu is updated by the method (due to a key press event / return key press):
pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE,
key = 0))
10. When the return key is pressed to select an opion, the selected opion is printed to
the terminal and the menu is updated by highlighing the selected opion (in red) by
rect_list, state = menu.update(e, state) :
while True:
if prev_state != state:
pygame.event.post(pygame.event.Event
(EVENT_CHANGE_STATE, key = 0))
prev_state = state
e = pygame.event.wait()
if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
if state == 0:
rect_list, state = menu.update(e, state)
 
Search WWH ::




Custom Search