0

I need the following function to work:

import pygame
import random


pygame.init()
clock = pygame.time.Clock()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)
base_font = pygame.font.Font(None, 32)
user_text = ''
color_active = pygame.Color('lightskyblue3')

def start_the_game():
    # Variables
    points = 0
    while True:
        x = random.randint(0, 10)
        y = random.randint(0, 10)
        z = x + y
        surface.fill((255, 70, 90))
        text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))
        input_rect = pygame.Rect(200, 200, 180, 50)
        pygame.draw.rect(surface, color_active,input_rect)
        text_surface = base_font.render(user_text,True,(255,255,255))
        surface.blit(text_surface, input_rect)
        surface.blit(text, (0, 0))
        pygame.display.update()
        result = int(input())
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        if result == z:
            print("Correct")
            points += 5
            print("You have this many points: ", points)
        else:
            if result != z:
                print("Wrong!")
                parar = input("Would you like to stop? ")
                if parar == "yes":
                    print("You have made " + str(points) + "points")
                    break
                else:
                    continue


start_the_game()

At the moment the window just freezes. I have been told that the inputs are the problems, so I have thought of adding an entry box to make it work. However, I don't know how could I continue from here. I need to use the input box at every input that the program requires right now, but I don't know how to do it. Any tips?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Torchllama
  • 43
  • 1
  • 7
  • 1
    Basically you want to write a program with a GUI (graphical user interface), and generally speaking they can't pause and wait for user input and instead are what is called event-drivem — a part of their main processing loops check for things like mouse movement and keyboard keypresses — so your call to `input()` freezes the program because it blocks all processing. I believe that are some user-interface modules for `pygame` that provide a framework for doing such things if you look around. – martineau Aug 15 '21 at 23:25
  • Here's one called — shock! — [Pygame GUI](https://pygame-gui.readthedocs.io/en/latest/). – martineau Aug 16 '21 at 10:52
  • Alright, I will have a look and say something. Thanks! – Torchllama Aug 16 '21 at 14:53

0 Answers0