【问题标题】:Pygame: How to catch user input after they press the ENTER keyPygame:如何在用户按下 ENTER 键后捕捉用户输入
【发布时间】:2021-06-17 02:08:31
【问题描述】:

我正在制作一个基于文本的 RPG 作为我的第一个 python/pygame 项目。在我的游戏中,我想向玩家展示选择,然后让他们输入。我能够下载并导入一个接受用户输入并将其显示在屏幕上的模块。但是,在将它用于任何类似的东西之前......让我们说,如果用户输入是“是”,那么他们会去一个新区域,我希望程序只在他们按下回车键后才接受用户输入。我相信我下载的 textinput 模块的教程有这样做的指导,但老实说,我只是不明白它在说什么。我尝试了多种类型的循环,但没有任何反应。任何帮助将不胜感激。这是我的主要游戏代码:

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)

现在这里是 pygame textinput 模块的源代码,我想我会链接到代码,所以这篇文章不会太拥挤: https://github.com/Nearoo/pygame-text-input

【问题讨论】:

    标签: python pygame user-input


    【解决方案1】:

    您需要一个变量来表示游戏的状态。一旦按下 enter 就会改变状态。根据游戏的状态在应用循环中实现不同的case::

    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)
    
        # [...]
    

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多