You need to enable JavaScript to run this app. Pygame使用pygame_textinput时如何在用户按下ENTER键后获取输入
优惠活动
大模型
产品
解决方案
定价
更多

Pygame使用pygame_textinput时如何在用户按下ENTER键后获取输入

解决方法

你使用的pygame_textinput模块内置了回车事件的判断逻辑:update()方法会返回布尔值,当用户按下回车键时返回True,此时调用get_text()方法即可读取到用户输入的全部内容。
你只需要新增游戏状态变量控制场景切换,再修改主循环逻辑即可,完整修改后代码如下:

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()
# 新增:游戏状态变量,控制不同场景渲染
game_state = "first_area"


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():
    our_game_display.fill(background_color_black)
    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)


while True:

    our_game_display.fill((background_color_black))

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

    # 新增:根据游戏状态渲染对应场景
    if game_state == "first_area":
        first_area()
        # Feed it with events every frame
        enter_pressed = textinput.update(events)
        # 新增:判断用户按下回车
        if enter_pressed:
            # 获取用户输入,转小写适配大小写输入
            user_input = textinput.get_text().lower().strip()
            if user_input == "yes":
                game_state = "play"
            elif user_input == "no":
                exit()
            # 可选:清空输入框方便下次输入
            textinput.clear()
    elif game_state == "play":
        choose_to_play()

    # 只在输入场景显示输入框
    if game_state == "first_area":
        screen.blit(textinput.get_surface(), (10, 600))
    screen.blit(our_game_display, (0, 0))
    pygame.display.update()
    clock.tick(30)

功能说明

  • 输入yes回车后会自动跳转到游玩场景
  • 输入no回车后直接退出游戏
  • 自动适配大小写输入,比如输入Yes、YES都能正常识别
  • 跳转场景后自动隐藏输入框

内容的提问来源于stack exchange,提问作者wawa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 06:15:03