Pages

joi, 1 august 2019

PyGame : First interface - part 11.

This is python module named pygameMenu can help us to develop another step into the interface issue. This python module is simple, lightweight and easy to use and the last release from a day ago comes with the version is 2.0.1. The install step is easy with the pip tool:
pip install pygame-menu==2.0.1
Collecting pygame-menu==2.0.1
...
      Successfully uninstalled pygame-menu-1.96.1
Successfully installed pygame-menu-2.0.1 pyperclip-1.7.0
This python module are supported by currently python 2.7+ and 3.4+ (3.4, 3.5, 3.6, 3.7). You need to have the pygame python module install it on your operating system. If not you will get this error then reinstall the pygame with pip3 tool:
...
    from pygame.base import *
ModuleNotFoundError: No module named 'pygame.base'
You cam find more here. Let's see one simple example:
import sys
import pygame
import pygameMenu
from random import randint
from random import randrange
from pygame.locals import *
from pygameMenu.locals import*
#  create the menu 
class GameMenu():
    # this will used on click's 
    def test(self):
        print('test')
    # this will create the menu with some features
    def my_menu_game(self):
        self.my_menu_game = pygameMenu.Menu(get_display,
                                   font=pygameMenu.font.FONT_BEBAS,
                                   dopause=False,
                                   menu_color=(0, 10, 176),  # Background color
                                   menu_color_title=(0, 76, 76),
                                   menu_height= 240,
                                   menu_width=320,
                                   onclose=pygameMenu.events.DISABLE_CLOSE,
                                   option_shadow=True,
                                   option_shadow_position=pygameMenu.locals.POSITION_SOUTHEAST,

                                   title='Help',
                                   window_height=480,
                                   window_width=640
                                        )
        # add some items on menu 
        self.my_menu_game.add_option('Test!', self.test)
        self.my_menu_game.add_selector('Select', [('eazy', 'EASY'),
                                                     ('medium', 'MEDIUM'),
                                                     ('hard', 'HARD')],
                                onreturn = False,
                                onchange = self.test)
        self.my_menu_game.add_option('Exit', self.test)



        # Loop the game and get menu events
        while True:
            # clock count
            clock.tick(60)
            # events for menu 
            events = pygame.event.get()
            for event in events:
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()

            # create menu
            self.my_menu_game.mainloop(events)

            # the flip display function 
            pygame.display.flip()


# init the pygame 
pygame.init()

# default pygame init
get_display=pygame.display.set_mode((640,480))
pygame.display.set_caption("My Menu")
clock = pygame.time.Clock()

# create the menu
game_menu = GameMenu()
game_menu.my_menu_game() 
The result of this source code can be see into the next screenshot: