2

I've been working in my spare time on a simple game using pygame to allow me to get familiar with its code. The game I am making is a abstraction of a Four in a Row game which implements maths questions. To get the user's answer I am using a module called pygame_textinput. However, I am struggling to extract the user's answer which I want to then be able to compare to the correct answer, which will allow the user to place their disk in the grid.

The code for this I have is:

mathsanswer = pygame_textinput.TextInput()

This is outside of the main loop which I then call upon in the code below.

mathsanswer.update(events) #Check if user has inputted text
display.blit(mathsanswer.get_surface(), (600,725))

This part of the code works perfectly as the text the user types is displayed on screen.

However when I try to extract what the user has typed I get:

<pygame_textinput.TextInput object at 0x00000219C1F101D0>

Is there a way to get what the user has typed as the variable.

Thanks for any help.

2
  • What is the code that you use to try to get the input as a variable? Commented Mar 13, 2018 at 1:04
  • @johnashu I haven't got any code to try to get it as a variable. All i've tried to do is print (mathsanswer) which is when I get that value. I'm not sure how I would turn that into the what the user has typed in. Commented Mar 13, 2018 at 1:09

1 Answer 1

1

Try this.. you need to use get_text() to get the input.

To catch the user input after the user hits Return, simply evaluate the return value of the update()-method - it is always False except for when the user hits Return, then it's True. To get the inputted text, use get_text(). Example:

if mathsanswer.update(events):
    print(mathsanswer.get_text())
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Works perfectly.
can you select it as the correct answer please? the green tick :) Good luck with the game !

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.