This question already has an answer here:
My idea is I want to take some input from user in the game like name of user, then I want to put it on the screen.Maybe before starting the game or middle of the game, it matter.
I tried InputBox etc. but it's not working actually, I tried some of modules like it but none of working, some of them just typing my text on the screen thats all, what I want is I want to take that input and put some places on the pygame screen.
So I have to save that input as a variable.Is there any possible 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 score variable on the screen, then when I want to update the score like in snake game, when sneak eat the apple I can update text so I can update score variable.But this score variable is already defined, I want to do that with a variable given by user.
EDIT: I already check all answers&questions in SO, those are not what Im asking and what I want. There is some ways to print that variables, this is not I want as I said. Catching keyboard events and printing them isn't any part of my question.I gave the example with 'name' but THIS question is not relation with my question
EDIT 2: 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
And we can ask this whenever we want,wherever we want.. 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.
Have you tried getting key event's and then putting them together to form something? You could put something like this inside of your main game loop.
Then you can check for when the user is done with input(button/enter key), and finalize the variable. With this you might run into a problem where the key is held longer so you get 3 w's for only pressing the button once. If this is the case you can either 1. not allow another input until a certain time has passed(.25 s maybe?) or 2. use pygame.KEYUP instead of pygame.KEYDOWN and exclusively check for strokes.
Here is a possible solution, it may be really useful for future visitors that are also suffering with this problem like me. Which is;
Then a function works like
input
;As you see, this function catching keyboard events, it has its own
while
loop also, it's important. I break the loop when pressingEnter
button which isif event.key == pygame.K_RETURN: done=False
. Then returning our word withtext1
function, displaying. Other events can be set of course opinion-based, like space button for a gap etc.See that we break this loop with the
Enter
button again. So we will ask the user our question in intro, then we will set it on the screen, top-right corner for example.To make it so a user inputs something type in
Now a user can type something to make it do something type
Now if I type in input it will say the input works
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:
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:
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:
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 :
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:
You do this with many other occasions. I hope this is clearer and more helpful!