I want to take input from the user in the game (e.g. their name), then put it on the screen.
I tried modules (including InputBox), but none of them are working. They just displayed my text on screen.
I want to save that input to a variable. Is there any way to do this?
Example:
font1 = pygame.font.SysFont("None", 30)
score = 0
text = font1.render("{}".format(score), True,(255,255,255))
...
...
if sneakeattheapple:
score += 1
text = font1.render("{}".format(score), True,(255,255,255))
...
...
screen.blit(text,(275,6))
This is going to put the score
variable on the screen. But score
is already defined, I want to do this with a variable given by user.
EDIT: Let me be more clear. In Python we can do this:
x = input("Do you want to do this? (y/n): ")
if x == "y":
#do something
if x == "n":
#do something
This is what I want to do in Pygame.
There's nothing baked into Pygame for this. You will either need to use a 3rd-party GUI library, or build it yourself. Example: if the textbox has focus, take all keydown events, and append to a string. Each frame, draw a box, then draw the string on top.
Building a simple one shouldn't be that hard, but if you want a more full-featured one, it will likely be easier to use a library.
I am currently assuming this function worked with your program is successful and raises no errors. Here is a function in the link you gave us:
def ask(screen, question):
"ask(screen, question) -> answer"
pygame.font.init()
current_string = []
display_box(screen, question + ": " + string.join(current_string,""))
while 1:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
break
elif inkey == K_MINUS:
current_string.append("_")
elif inkey <= 127:
current_string.append(chr(inkey))
display_box(screen, question + ": " + string.join(current_string,""))
return string.join(current_string,"")
It looks like this is how you get input from a user with a pygame screen right? Let's look at line 4 of this function:
current_string = []
The stuff the user types in is stored in this list. Assuming you know how to take a string and put it on the screen, you could save the string like this:
string_input = ''.join(current_string)
If you can make a similar function (if this one doesn't work), you can do the same thing! Just save the first item in the list holding the string in a variable as shown above. If you have any problems, please comment so I can edit my answer.
To the next part now. You can simply activate this function at any time. You might want to activate when something happens. An example is when the snake eats the apple. You probaly have a function for that I believe. You can make a variable like this :
Eat = 0
And put in that function. When that variable is equal to 0. Nothing really activate the other function. When the snake eats the apple, reset the variable to 1 and then activate the function like this:
if Eat = 0:
someclassname.ask()
You do this with many other occasions. I hope this is clearer and more helpful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With