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.