首页 > 解决方案 > 有没有办法让带有文本的框和文本输入框一起出现?

问题描述

因此,我正在尝试使用 pygame 制作基于文本的游戏,并且正在测试两个教程。一个是关于如何创建一个框来保存文本,另一个是向我展示如何创建一个文本输入框。现在,我正在尝试将这两个方面结合起来创建一个兼具两者的游戏,但是每当我尝试运行它时,它运行的窗口就会打开,然后我会收到一个通知,说该窗口没有响应。我怎样才能做到这一点,然后它会在一个框被原谅的文本和一个文本输入框上正常运行游戏?

注意:游戏的重点是让用户在提示部分找到错误并输入正确答案,因此提示元素中的错误是故意的。

import pygame
pygame.init()
green = (0,255,0)
blue = (0,0,128)
white = (255,255,255)

prompts = ["hOW are you doing", "I writed it using a pencal.", "My dog ate the bone!.", "8+1 = 7", "Please stop!!", "Here's a challenge; That movie was terribly bad!", "I go home now meester person.", "I' d like to stay here.", "umm...hello?", "[darn it!!! I think I forget that I am Robin", "After I sated down I wanted to eating.", "I can see the Firetruck!?", "Is the usa a countery."]
answers = ["How are you doing?", "I wrote it using a pencil.", "My dog ate the bone!", "8+1 = 9", "Please stop!!", "Here's a challenge: That movie was bad!", "I am going to go home now, mister person.", "I'd like to stay here.", "umm...hello?", "Darn it!!! I think I forgot that I am Robin.", "After I sat down, I wanted to eat", "I can see the firetruck!", "Is the U.S.A a country?"]
score = 0
screen = pygame.display.set_mode((640, 480))
def main():

    font = pygame.font.Font(None, 32)
    clock = pygame.time.Clock()
    input_box = pygame.Rect(100, 100, 140, 32)
    color_inactive = pygame.Color('lightskyblue3')
    color_active = pygame.Color('dodgerblue2')
    color = color_inactive
    active = False
    text = ''
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                # If the user clicked on the input_box rect.
                if input_box.collidepoint(event.pos):
                    # Toggle the active variable.
                    active = not active
                else:
                    active = False
                # Change the current color of the input box.
                color = color_active if active else color_inactive
            if event.type == pygame.KEYDOWN:
                if active:
                    if event.key == pygame.K_RETURN:
                        print(text)
                        if text == answers[0]:
                            print("correct!")
                            del answers[0]
                            del prompts[0]

                            text = ''
                        else:
                            print("wrong")
                            text = ''
                            del answers[0]
                            del prompts[0]
                    elif event.key == pygame.K_BACKSPACE:
                        text = text[:-1]
                    else:
                        text += event.unicode

        screen.fill((30, 30, 30))
        # Render the current text.
        txt_surface = font.render(text, True, color)
        # Resize the box if the text is too long.
        width = max(200, txt_surface.get_width()+10)
        input_box.w = width
        # Blit the text.
        screen.blit(txt_surface, (input_box.x+5, input_box.y+5))
        # Blit the input_box rect.
        pygame.draw.rect(screen, color, input_box, 2)

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

font2 = pygame.font.Font('freesansbold.ttf', 32)

# create a text suface object,
# on which text is drawn on it.
text2 = font2.render(prompts[0], True, green, blue)

# create a rectangular object for the
# text surface object
textRect = text2.get_rect()

# set the center of the rectangular object.
textRect.center = (200, 200)

while True:
    screen.fill(white)

    screen.blit(text2, textRect)

if __name__ == '__main__':
    pygame.init()
    main()
    pygame.quit()

标签: pythonpygame

解决方案


推荐阅读