Search code examples
pythonpygameuser-input

Pygame: How to catch user input after they press the ENTER key


I am making a text based RPG as my first python/pygame project. In my game I would like to present the player with choices then ask them to type it in. I was able to download and import a module which accepts user input and displays it on the screen. However, before using it for anything like..lets say, if the user input is 'yes', then they go to a new area, I'd like the program to only accept the user input once they hit the enter key. I believe the tutorial for the textinput module I downloaded has directions to do this, but to be honest I just don't understand what it's saying. I've tried multiple types of loops but nothing is happening. Any help will be appreciated. Here is my main game code:

import pygame_textinput
import pygame
pygame.init()
#fps
clock=pygame.time.Clock()
# create font here
font_name = pygame.font.get_default_font()
WHITE_TEXT_COLOR = (255, 255, 255)
# create screen and window and display and font here
screen_width, screen_height = 800, 700
background_color_black = (0, 0, 0)
screen = pygame.display.set_mode((screen_width, screen_height))
our_game_display = pygame.Surface((screen_width, screen_height))
pygame.display.set_caption('MyRPGGame')
#create text input object
textinput = pygame_textinput.TextInput()







def draw_text(text, size, x, y):
        pygame.font.init()
        font = pygame.font.Font(font_name, size)
        text_surface = font.render(text, True, WHITE_TEXT_COLOR)
        text_rect = text_surface.get_rect()
        text_rect.center = (x, y)
        our_game_display.blit(text_surface, text_rect)


def choose_to_play():
    draw_text("You've decided to play",20,screen_width/2,screen_height/2+50)

def first_area():
        our_game_display.fill(background_color_black)
        draw_text('The story of this game depends on your choices. Do you wish to play?', 20, screen_width / 2,screen_height / 2 - 100)
        draw_text('Type your answer and hit enter.', 20, screen_width / 2,screen_height / 2 - 50)
        draw_text('Yes', 20, screen_width/2,screen_height/2+50)
        draw_text('No', 20, screen_width/2,screen_height/2+100)


        screen.blit(our_game_display, (0, 0))
        pygame.display.update()


while True:

    our_game_display.fill((background_color_black))

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exit()

    first_area()
    # Feed it with events every frame
    textinput.update(events)



    # Blit its surface onto the screen
    screen.blit(textinput.get_surface(), (10, 600))

    pygame.display.update()
    clock.tick(30)

Now here is the source for the pygame textinput module, figured I would link to the code so this post isn't too crowded: https://github.com/Nearoo/pygame-text-input


Solution

  • You need to a variable for the state of the game. Once enter is pressed change the state. Implement different cases in the application loop depending on the state of the game::

    game_state = 'start'
    
    while True:
    
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    game_state = 'input'
    
        our_game_display.fill((background_color_black))
    
        if game_state == 'input':
            textinput.update(events)
    
        # [...]