How to get user input in Pygame and save it as a variable?

My idea is that I want to take some input from the user in the game, for example, the username, then I want to put it on the screen. Perhaps before the start of the game or in the middle of the game it matters.

I tried InputBox , etc., but it doesnโ€™t actually work, I tried some of these modules, but none of them work, some of them just print my text on the screen, everything I want, I want to accept this entry and put some places on the pygame screen.

So, I have to save this 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 will change the score variable on the screen, and then when I want to update the score, as in the game with the snake, when I approach the apple, I can update the text so that I can update the score variable. But this counter variable is already defined, I want to do this with a variable set by the user.

EDIT: I already check all the answers and questions in SO, this is not what I ask and what I want. There are several ways to print these variables; this is not . I want, as I said. Capturing events on the keyboard and printing them are not part of my question. I gave an example called "name", but THIS question is not related to 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 about it whenever we want, wherever we want. This is what I want to do in Pygame.

+5
source share
6 answers

Here is a possible solution, it can be really useful for future visitors who also suffer from this problem, like me. What:

First, create a text rendering function.

 def text1(word,x,y): font = pygame.font.SysFont(None, 25) text = font.render("{}".format(word), True, RED) return screen.blit(text,(x,y)) 

Then the function works as input ;

 def inpt(): word="" text1("Please enter your name: ",300,400) #example asking name pygame.display.flip() done = True while done: for event in pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: word+=str(chr(event.key)) if event.key == pygame.K_b: word+=chr(event.key) if event.key == pygame.K_c: word+=chr(event.key) if event.key == pygame.K_d: word+=chr(event.key) if event.key == pygame.K_RETURN: done=False #events... return text1(word,700,30) 

As you can see, this function catches keyboard events, it has its own while , this is important. I break the loop when I press the Enter button, which if event.key == pygame.K_RETURN: done=False . Then we will return our word using the function text1 that displays. Other events can be customized, of course, based on the opinion, for example, the space bar for space, etc.

Then, in fact, we have to make the intro function, and we will ask the username there, for example, when the intro function is executed, the name will be set on the screen until the game ends.

 def game_intro(): intro=True while intro: for event in pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() quit() if event.type==pygame.KEYDOWN: if event.key==pygame.K_RETURN: intro=False inpt() #Here we are calling our function screen.fill(white) pygame.display.update() clock.tick(15) 

Let's see that we again broke this loop using the Enter button. Therefore, we will ask the user our question in the introduction, then we will install it on the screen, for example, in the upper right corner.

-1
source

Pygame didn't sing anything for this. You will need to either use a third-party graphics library or create one yourself. Example: if the text field has focus, take all the blocking events and add to the line. Each frame, draw a field, then draw a line at the top.

Building a simple one doesn't have to be that complicated, but if you want a more fully functional one, it will most likely be easier to use the library.

+4
source

Currently, I assume that this function works with your program successfully and does not cause errors. Here is the function in the link that 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,"") 

Looks like this is how you get input from a user with a pygame screen correctly? Let's look at line 4 of this function:

 current_string = [] 

The content that the user enters is stored in this list. Assuming that you know how to take a string and place it on the screen, you can save the string as follows:

 string_input = ''.join(current_string) 

If you can do a similar function (if this one does not work), you can do the same! Just save the first item in the list containing the string in the variable as shown above. If you have any problems, please comment so that I can edit my answer.
To the next part now. You can simply activate this feature at any time. You can activate when something happens. For example, a snake eats an apple. You probably have a feature that I believe in. You can make a variable like this:

 Eat = 0 

And paste this function. When this variable is 0. Nothing really activates another function. When the snake eats an apple, reset the variable to 1, and then activates the function as follows:

 if Eat = 0: someclassname.ask() 

You do this with many other cases. Hope this is more understandable and useful!

+4
source

Did you try to get the key event and then connect them to form something? You can add something like this inside the main game loop.

 input = '' for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: playing = False if event.key == pygame.K_w: input = input + "w" if event.key == pygame.K_s: input = input + "s" if event.key == pygame.K_a: input = input + "a" 

Then you can check when the user has completed the input (button / enter key) and complete the variable. In this case, you may encounter a problem when the key is held for longer, so you get 3 w for a single button press. If this is the case, you can: 1. not allow another input until a certain time has passed (.25 s, maybe?) Or 2. use pygame.KEYUP instead of pygame.KEYDOWN and exclude checking for hits.

+2
source

I think the below line works for you!

mydata = raw_input ('Request:')

-3
source

To make the user enter something, enter

 variable = input() 

Now the user can enter something to do something like

 if variable = ('input'): print('the input works') 

Now, if I type in the input, it will say: the input works

-4
source

Source: https://habr.com/ru/post/1210141/


All Articles