如何让pygame文本输入检测Pygame中的键?

时间:2018-02-21 14:06:20

标签: python pygame display blit

我正在做一个使用Python和Pygame的项目。我想问一些关于Pygame中键的问题。

谢谢Shubhitgarg。我使用他/她的建议(使用pygame_textinput),它成功了。现在我想按下一个输入更改文本输入位置,但它无法检测到输入。我该如何解决?我的真实代码:

# installing package
import pygame
from pygame import *
import pygame_textinput
pygame.init()

# colour settings
red = (255, 0, 0)
green = (0, 255, 0)
grass_green = (112, 173, 71)
blue = (0, 0, 255)
yellow = (255, 255, 0)
white = (255, 255, 255)
black = (0, 0, 0)

# screen settings
window = pygame.display.set_mode((680, 600))
window.fill(grass_green)
pygame.display.flip()

# font settings
default_font = pygame.font.get_default_font()
big_font = pygame.font.Font(default_font, 96)
font_a = pygame.font.Font(default_font, 50)
font_b = pygame.font.Font(default_font, 30)
font_c = pygame.font.Font(default_font, 18)

# text input settings
textinput = pygame_textinput.TextInput("freeansbold.ttf", 96, True, black, white, 400, 35)

# timer
start = pygame.time.get_ticks()

# text
please_guess_a_number = font_a.render("Please guess a number. ", 1, white)
text_range = font_c.render("from                                                        to", 1, white)
wrong_bigger = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be bigger.)", 1, white)
wrong_smaller = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be smaller.)", 1, white)
correct = font_b.render("Congratulations! You guess it correctly!!", 1, white)

# game
ask_you_to_set_range_1 = True
ask_you_to_set_range_2 = False
while True:
    if ask_you_to_set_range_1:
        window.blit(please_guess_a_number, (60, 80))
        pygame.draw.rect(window, yellow, [60, 200, 230, 300])
        pygame.draw.rect(window, yellow, [390, 200, 230, 300])
        window.blit(text_range, (10, 550))
        pygame.display.flip()
        while True:
            events = pygame.event.get()
            textinput.update(events)
            window.blit(textinput.get_surface(), (110, 300))
            pygame.display.flip()
    if ask_you_to_set_range_2:
        while True:
            events = pygame.event.get()
            textinput.update(events)
            window.blit(textinput.get_surface(), (440, 300))
            pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if ask_you_to_set_range_1 and event.key == pygame.K_KP_ENTER:
                ask_you_to_set_range_1 = False
                ask_you_to_set_range_2 = True
                if textinput.update(event):
                    num1 = textinput.get_text()
                    text1 = big_font.render(num1, 1, black)
                    window.blit(text1, (110, 300))

有人能教我解决吗?

1 个答案:

答案 0 :(得分:0)

你看到这个https://github.com/ShubhitGarg/pygame-text-input。 您可以导入它并使用text_input类并在同一页或下一页上将其blit 使用此

textinput = pygame_textinput.TextInput()

while True:
    screen.fill((225, 225, 225))

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

    # Feed it with events every frame
    textinput.update(events)
    # Blit its surface onto the screen
    screen.blit(textinput.get_surface(), (10, 10))

    pygame.display.update()