Search code examples
pythonpython-3.xpygamemenupygame-menu

How to link those two pygame-menus


I have created two menus using Pygame-menu. The first one is the start menu (when you open the program).

import pygame
import pygame_menu
from pygame import mixer

pygame.init()

#Mida i nom de la finestra
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")

font = pygame_menu.font.FONT_8BIT
font1 = pygame_menu.font.FONT_NEVIS

menu = pygame_menu.Menu('Projecte MatZanfe', 600, 400,
                       theme=pygame_menu.themes.THEME_SOLARIZED)

user_input = menu.add.text_input('User: ', font_name = font1, font_color = 'blue')
age_input = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black')
menu.add.button('Start', font_name = font, font_color = 'green')
menu.add.button('Exit', pygame_menu.events.EXIT, font_name = font,font_color = 'red')
menu.mainloop(surface)

Then, the other one is a level selector.

import pygame
import pygame_menu
from pygame import mixer

pygame.init()

#Mida i nom de la finestra
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
#Logotip de la finestra
logotip = pygame.image.load("calculator.png")
pygame.display.set_icon(logotip)

font = pygame_menu.font.FONT_8BIT
font1 = pygame_menu.font.FONT_NEVIS
font2 = pygame_menu.font.FONT_BEBAS

menu = pygame_menu.Menu('Selecció dels mòduls', 600, 400,
                       theme=pygame_menu.themes.THEME_SOLARIZED)

mixer.music.load('LevelSelector.wav')
mixer.music.play(-1)
menu.add.button('Sums', font_name = font2, font_color = 'green')
menu.add.button('Subtracts', font_name = font2, font_color = 'blue')
menu.add.button('Multiplications', font_name = font2,font_color = 'red')
menu.add.button('Divisions', font_name = font2, font_color = 'purple' )
menu.add.button("Back", font_name = font1, font_color = 'black')

menu.mainloop(surface)

How could I merge them, so when I press the "Start" button from the first menu, the whole surface displays the second menu but if I press the button "Back", it returns to the first menu? The files names are: Menu - Selector.


Solution

  • Not really familiar with this library, but I am guessing.

    Check the docs here.

    From the reference here,

    # Import the required libraries
    import pygame
    import pygame_menu
    
    # Initialize pygame
    pygame.init()
    surface = pygame.display.set_mode((600, 400))
    
    # Make menu 2
    menu2 = pygame_menu.Menu('Menu 2', 400, 300, theme=pygame_menu.themes.THEME_BLUE)
    ...
    
    # Make main menu
    menu = pygame_menu.Menu('Welcome', 400, 300, theme=pygame_menu.themes.THEME_BLUE)
    ...
    menu.add.button('Menu2', menu2)
    menu.add.button('Quit', pygame_menu.events.EXIT)
    
    # Run your menu
    menu.mainloop(surface)
    

    You may assume the surface is your display surface.