1

So, I am trying to make a text-based game using pygame, and I am testing out two tutorials. One talks about how I can create a box to hold text, and the other is showing me how to create a text-input box. Now, I am trying to combine these two aspects to create a game that has both, but whenever I try to run it, a window where it runs opens, and then i get a notification saying that the window isn't responding. How can I do this so that then it will run the game properly with both a box forgiven text and a text input box?

Note: the point of the game is for the user to find errors in the prompts section and type in the right answer, so mistakes in the prompts elements are intentional.

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()

0 Answers0