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)
Torchllama
  • 43
  • 1
  • 7
  • 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](https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/simple.py) –  Aug 14 '21 at 21:10
  • Well, at least it doesn't crash. How can I know if it created the variable? – Torchllama Aug 14 '21 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 – Matiiss Aug 14 '21 at 21:42
  • The print (age) should be printed almost at the end right (right before the ```menu.mainloop(surface)```? – Torchllama Aug 14 '21 at 21:46
  • I have tried what you told me @user16038533, but it doesn't print any value. However, now it says ``````. I don't know what that means. Anyway, is there any other way? – Torchllama Aug 14 '21 at 22:10
  • Seems like its an object. It must have a function or a variable that you can access like `age.GetText()` or something. You will have to dig through the documentation, but I didn't find it very clear about things. –  Aug 14 '21 at 22:19
  • you need to use `age.get_value()` when you are ready to get the value, for example define a function that will `print(age.get_value())` and add it to some random menu button then when you enter text in that widget, use the button and it should print out what you entered (reference: https://github.com/ppizarror/pygame-menu/blob/b3974f8977c0e02e3fcf0b4aa9964caa54bcf25b/pygame_menu/widgets/widget/textinput.py#L413) – Matiiss Aug 15 '21 at 08:46
  • Maybe when you press 'Comencem' (that would be translated as start') I could introduce the function you told me @Matiiss, so when you start, the age and the name from the user will be saved. However, how could I create that function? Looking at the example you linked? – Torchllama Aug 15 '21 at 09:53

1 Answers1

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')
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • So for User part It would be the same right? – Torchllama Aug 15 '21 at 10:31
  • @Torchllama pretty much yes – Matiiss Aug 15 '21 at 10:40
  • I Will give It a try. Thanks – Torchllama Aug 15 '21 at 11:07
  • 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 Aug 15 '21 at 15:27
  • @Torchllama can you please show what you have done? edit your question and add the current code – Matiiss Aug 15 '21 at 15:43
  • I used the old start_the_game function because I still haven't made the entry box. – Torchllama Aug 15 '21 at 15:52
  • @Torchllama well obviously you won't get what you want because you print out the objects right after they are made, you need to place print functions inside the `start_the_game()` function after you define `user_age` and `user_name`, after that put `print(user_age)` and `print(user_name)`, edited my answer to show what I mean – Matiiss Aug 15 '21 at 15:54
  • This is what happens when I put what you say (both user and age):``` ``` – Torchllama Aug 15 '21 at 15:59
  • @Torchllama ok, did you `print(user_input)` or `print(user_name)`? you need to use the one that has the returned value from `.get_value()`, also you could update the current code then – Matiiss Aug 15 '21 at 16:02
  • Holy moly now it works. It saves the variable you created. Thanks a ton dude. Now I will be trying to create the box input, if anything I will post a new question. Thank you @Matiiss – Torchllama Aug 15 '21 at 16:06