Giter Site home page Giter Site logo

i96751414 / pygame-menu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ppizarror/pygame-menu

0.0 2.0 0.0 152 KB

Menu for pygame, simple, lightweight and easy to use

Home Page: http://ppizarror.com/pygame-menu/

License: GNU General Public License v3.0

Python 100.00%

pygame-menu's Introduction

Pygame Menu

Pygame Menu

Menu for pygame, simple, lightweight and easy to use

@ppizarror License GPL v3 Python 2.7

Python library that can create a Menu on Pygame, supports:

  1. Textual menus
  2. Textual menus + buttons
  3. Lists of values (that i called Selectors) that can trigger functions when pressing Return or changing the value
  4. Button menus

Examples:

Normal Button menu

Text menu

Small submenu

Import

Import of this library is similar as pygame:

import pygameMenu                # This imports classes and other things
from pygameMenu.locals import *  # Import constants (like actions)

Obviously you need Pygame to run this.

Usage

Creating menus

  • Menu

    This class creates a Menu

    pygameMenu.Menu(surface, window_width, windw_height, font, title, *args) # -> Menu object

    Parameters are the following:

    Param Description Type
    surface Pygame surface object Surface
    window_width Window width size (px) int
    window_height Window height size (px) int
    font Font file direction str
    title Title of the menu (main title) str
    enabled Menu is enabled by default or not boo
    bgfun Background drawing function (only if menupause app) function
    color_selected Color of selected item tuple
    dopause Pause game bool
    draw_region_x Drawing position of element insie menu (x-axis) int
    draw_region_y Drawing position of element insie menu (y-axis) int
    draw_select Draw a rectangle around selected item(bool) bool
    font_color Color of font tuple
    font_size Font size int
    font_size_title Font size of the title int
    font_title Alternative font of the title (fil direction) str
    menu_alpha Alpha of background (0=tansparent, 100=opaque) int
    menu_centered Text centered menu bool
    menu_color Menu color tuple
    menu_color_title Background color of title tupl
    menu_height Height of menu (px) int
    menu_width Width of menu (px) int
    onclose Event that applies when closing menufunction, PymenuAction
    option_margin Margin of each element in menu(px) int
    option_shadow Indicate if a shadow is drawn on ech option bool
    rect_width Border with of rectangle around seleted item int
    title_offsetx Offset x-position of title (px)int
    title_offsety Offset y-position of title (px)int
  • TextMenu

    This class creates a textual menu

    pygameMenu.TextMenu(surface, window_width, window_height, font, title, *args) # -> TextMenu object

    This class inherites from Menu, so the parameters are the same, except the following extra parameters:

    Param Description Type
    text_centered Indicate if text is centered bool
    text_color Text color tuple
    text_fontsize Text font size int
    text_margin Line margin int
    draw_text_region_x X-Axis drawing region of the text int

Add options and entries to Menus

Menu and TextMenu have the next functions:

  • add_option(element_name, element, *args)

    Adds an option to the Menu.

    Param Description Type
    element_name String on menu entry str
    element_name Menu object (Menu, function or Menu-Event) supported PymenuAction, function, Menu
    *args Additional arguments -

    Example:

    help_menu = pygameMenu.TextMenu(surface,window_width=W_SIZE, window_height=H_SIZE,
                                    font=pygameMenu.fonts.FONT_FRANCHISE,
                                    onclose=PYGAME_MENU_DISABLE_CLOSE,
                                    title='Help', dopause=False,                        
                                    menu_color_title=(120, 45, 30),
                                    menu_color=(30, 50, 107))
    help_menu.add_option('Return to Menu', PYGAME_MENU_BACK) # Add option

    Another example:

    menu = pygameMenu.Menu(surface,window_width=W_SIZE, window_height=H_SIZE,
                           font=pygameMenu.fonts.FONT_NEVIS,
                           title='Main Menu', title_offsety=5,
                           menu_alpha=90, enabled=False,
                           bgfun=mainmenu_background)
    menu.add_option(timer_menu.get_title(), timer_menu)  # Add timer submenu
    menu.add_option(help_menu.get_title(), help_menu)    # Add help submenu
    menu.add_option(about_menu.get_title(), about_menu)  # Add about submenu
    menu.add_option('Exit', PYGAME_MENU_EXIT)            # Add exit function
  • add_selector(title, values, onchange, onreturn, **kwargs)

    • add_selector_change(title, values, ochangen, **kwargs)
    • add_selector_return(title, values, onreturn, **kwargs)

    Add a selector to menu: several options with values and two functions that execute when changing the selector (left/right) and pressing Return key on the element.

    Param Description Type
    title String on menu entry str
    values Value list, list of tuples list
    onchange Function that executes when change the value of selector function
    onreturn Function that executes when pressing return button on selected item function
    **kwargs Additional arguments -

    Example:

    def change_color_bg(c, **kwargs):
        """
        Change background color
        
        :param c: Color tuple
        """
        if c == (-1, -1, -1):  # If random color
            c = (randrange(0, 255), randrange(0, 255), randrange(0, 255))
        if kwargs['write_on_console']:
            print('New bg color: ({0},{1},{2})'.format(*c))
        COLOR_BACKGROUND[0] = c[0]
        COLOR_BACKGROUND[1] = c[1]
        COLOR_BACKGROUND[2] = c[2]
        
    timer_menu = pygameMenu.Menu(...)
    
    # Add selector
    timer_menu.add_selector('Change bgcolor',
                            # Values of selector, call to change_color_bg
                            [('Random', (-1, -1, -1)),  # Random color
                             ('Default', (128, 0, 128)),
                             ('Black', (0, 0, 0)),
                             ('Blue', COLOR_BLUE)],
                            None, # onchange
                            change_color_bg, # onreturn
                            write_on_console=True # Optional change_color_bg param)
                            
    timer_menu.add_option('Reset timer', reset_timer)
    timer_menu.add_option('Return to Menu', PYGAME_MENU_BACK)
    timer_menu.add_option('Close Menu', PYGAME_MENU_CLOSE)
  • add_line(text)

    Adds a new line on TextMenu object.

    Example:

    HELP = ['Press ESC to enable/disable Menu',
            'Press ENTER to access a Sub-Menu or use an option',
            'Press UP/DOWN to move through Menu',
            'Press LEFT/RIGHT to move through Selectors']
            
    menu_help = pygameMenu.Menu(...)
    for line in HELP:
        menu_help.add_line(line) # Add line
    menu_help.add_option('Return to Menu', PYGAME_MENU_BACK)
  • mainloop(events)

    Main loop of menu, on this function Menu can handle exceptions and draw. If parameter dopause is enabled then Menu pauses aplication and checks Events.

    menu = pygameMenu.Menu(...)
    
    # Main aplication
    while True:
    
        # Application events
        events = pygame.event.get()
    
        # Menu loop (If onpause is enabled then a infinite-loop is triggered on this line)
        menu.mainloop(events)
  • disable()

    Disable Menu (doest check events and draw on surface).

    menu = pygameMenu.Menu(...)
    menu.disable()
  • draw()

    Draw Menu on surface.

    menu = pygameMenu.Menu(...)
    menu.disable()
  • enable()

    Enable Menu (can check events and draw).

    menu = pygameMenu.Menu(...)
    menu.enable()
  • get_title()

    Enable Menu (can check events and draw).

    menu = pygameMenu.Menu(..., title='Menu title', ...)
    menu.get_title() # -> 'Menu title'
  • is_enabled()

    Check if menu is enabled.

    menu = pygameMenu.Menu(...)
    menu.disable()
    menu.is_enabled() # -> False
  • is_disabled()

    Check if menu is disabled.

    menu = pygameMenu.Menu(...)
    menu.disable()
    menu.is_disabled() # -> True

Menu events

Supported events are the same:

Event Description
PYGAME_MENU_BACK Go back on menu
PYGAME_MENU_CLOSE Close menu
PYGAME_MENU_EXIT Close application
PYGAME_MENU_DISABLE_CLOSE Disable close menu
PYGAME_MENU_RESET Reset menu

This events are imported on from pygameMenu.locals import * line.

Using fonts

Also this library has some fonts to use, to load a font run this code:

import pygameMenu

fontdir = pygameMenu.fonts.FONT_NAME

Avaiable fonts (FONT_NAME): 8BIT, BEBAS, FRANCHISE, MUNRO and NEVIS.

Configurations

Default parameters of Menu and TextMenu are stored on the following files:

File Description
config_controls.py Configure default key-events of Menus
config_menu.py Configure default parameter of Menu class
config_textmenu.py Configure default parameter of TextMenu class

License

This project is licensed under GPLv3 [https://www.gnu.org/licenses/gpl-3.0.html]

Author

Pablo Pizarro R. | 2017-2018

pygame-menu's People

Contributors

asierrayk avatar i96751414 avatar maditnerd avatar ppizarror avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.