Making games using
pygame is really cool, but most games (or applications)
require end-user configuration. Creating complex GUI objects to display a menu can
be painful. That why
pygame-menu was designed.
Here is a simple example of how to create a menu with
(the code is available in pygame_menu.examples.simple.py):
Import the required libraries
import pygame import pygame_menu
pygame.init() surface = pygame.display.set_mode((600, 400))
Make your menu
def set_difficulty(value, difficulty): # Do the job here ! pass def start_the_game(): # Do the job here ! pass menu = pygame_menu.Menu(300, 400, 'Welcome', theme=pygame_menu.themes.THEME_BLUE) menu.add.text_input('Name :', default='John Doe') menu.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty) menu.add.button('Play', start_the_game) menu.add.button('Quit', pygame_menu.events.EXIT)
Run your menu
Interested in going deeper into menu design ?
This chapter define rules and advanced tips and tricks to develop extensions for
pygame-menu. The main addressed topics are:
A menu is in fact a list of widgets arranged on the same surface. Access to a widget in a menu can easily be done with two methods:
widget = menu.get_widget('MyWidgetID') selected = menu.get_selected_widget()
pygame_menu widget and its behaviors are defined in a
class. The currently existing classes are:
For advanced programmers, those classes can be used to design custom menus or windows.