0

I'm trying to apply pygame-menu library (https://pygame-menu.readthedocs.io). How can I extract the 'Name' value from the text-input field of the menu - the value that this field contains when I start the game i.e. strictly at the moment when I press the 'Play' and process this value further - pass to my class 'Game'. Can't figure out the documentation. Maybe someone came across this?

import pygame
import pygame_menu

# A class that defines the game environment and launches the gameplay
# Works successfully without a menu
from game import Game

# I need this function from `button('Play', start_the_game)` to start the game
# and NAME is the value that I need to extract from the field 'Name' and pass it
# to the instance of me class Game
def start_the_game(NAME):
    Game(NAME).run()    # Launches the actual game


pygame.init()
screen = pygame.display.set_mode(900, 600)
screen.fill((0,0,0))

menu = pygame_menu.Menu("Let's play!", 400, 300,
                        theme=pygame_menu.themes.THEME_DARK)

menu.add.text_input('Name :', default='A Player')
menu.add.button('Play', start_the_game)
menu.add.button('Quit', pygame_menu.events.EXIT)

menu.mainloop(screen)
  • If you don't mind, please select the answer that you based your final solution on. You had left a comment, stating that my answer helped you, but you didn't give any credit! – mole1000 Nov 30 '21 at 22:10

2 Answers2

0

Looks like you should try: player_name = menu.add.text_input('Name :', default='A Player') Then try: start_the_game(player_name.get_value())

According to the docs, the more standard method would probably be something like: player_name = menu.add.text_input('Name :', default='A Player', onchange=get_name) And then define a function like:

def get_name(value):
    player_name = value

I tested this, and both methods do work for getting the value, although the two methods require a slightly different method of passing the value at the right time. Just to clarify, this will get the value and pass it to your start_the_game function at the time it is called:

name_box = menu.add.text_input('Name :', default='A Player')
menu.add.button('Play', start_the_game, name_box)

If your start_the_game function were like this:

def start_the_game(NAMEBOX):
    GAME(NAMEBOX.get_value()).run()
mole1000
  • 161
  • 6
0

This answer came from a comment by mole1000.

The final version:

import pygame
import pygame_menu
from catch_ball import CatchBall
import settings


def start_the_game(namebox):
    CatchBall(namebox.get_value()).run()


def main():

    pygame.init()
    screen = pygame.display.set_mode((settings.screen_width,
                                      settings.screen_height))

    menu = pygame_menu.Menu('Cath a ball!',
                            settings.menu_width, settings.menu_height,
                            theme=pygame_menu.themes.THEME_DARK)

    name_box = menu.add.text_input('Name: ',)
    menu.add.button('Play', start_the_game, name_box)
    menu.add.button('Quit', pygame_menu.events.EXIT)
    menu.mainloop(screen)


if __name__ == '__main__':
    main()