0

Where it says user_input = menu.add.text_input('User: ') or age_input = menu.add.text_input('Age: '), you can write something. I need to assign the words that are written to a variable. How could I do it?

import pygame
import pygame_menu
import random

pygame.init()
#Size - name of the window
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Example")

def game():
    #Variables
    score = 0
    user_age = age_input.get_value()
    user_name = user_input.get_value()
    while True:
        x = random.randint(0, 10)
        y = random.randint(0, 10)
        z = x + y
        print(str(x) + "+" + str(y))
        result = int(input())
        if result == z:
            print("Correct")
            score = score + 5
            
        else:
            if result != z:
                stop = input("Wrong! Wanna stop? ")
                if stop == ("yes"):
                    print("You have " + str(score) + " points")
                    break
                else:
                    continue

menu = pygame_menu.Menu('Menu', 600, 400,
                       theme=pygame_menu.themes.THEME_BLUE)

user_input = menu.add.text_input('User: ')
age_input = menu.add.text_input('Age: ')
menu.add.button('Start', game)
menu.add.button('Exit', pygame_menu.events.EXIT)

print (user_input)
print (age_input)

menu.mainloop(surface)
8
  • Couldn't tell you for sure but I think those functions returns the name, so you can do age = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black'). Got that from here Commented Aug 14, 2021 at 21:10
  • Well, at least it doesn't crash. How can I know if it created the variable? Commented Aug 14, 2021 at 21:27
  • @Torchllama print(age) it will be at least None (or an empty string which is kinda the same you just won't really see that anything has been printed) but if it has some value it will print that value Commented Aug 14, 2021 at 21:42
  • The print (age) should be printed almost at the end right (right before the menu.mainloop(surface)? Commented Aug 14, 2021 at 21:46
  • I have tried what you told me @user16038533, but it doesn't print any value. However, now it says <pygame_menu.widgets.widget.textinput.TextInput object at 0x000001C5F7B8CD00>. I don't know what that means. Anyway, is there any other way? Commented Aug 14, 2021 at 22:10

1 Answer 1

1

This is how you can retrieve user data just before starting the game:

def start_the_game():
    user_age = age_input.get_value()
    print(user_age)  # to print the value of user_age, only for debugging tho, don't use this when you have finished the game, because user is not really supposed to read the console anyways
    # rest of the game code


age_input = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black')
menu.add.button('Comencem', start_the_game,font_name = font, font_color = 'green')
Sign up to request clarification or add additional context in comments.

10 Comments

So for User part It would be the same right?
@Torchllama pretty much yes
I Will give It a try. Thanks
I have put what you told, me, and there is no crash. However, it still doesn't print anything when I make for example print (age_input). Right before the menu.mainloop(surface).
@Torchllama can you please show what you have done? edit your question and add the current code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.