0

im trying to do a random number generator in pygame everything is ok until that i got 1 problem the text that give the awnser does not actualise when clicking on the button (there might be error its my first time using stack overflow)

resumate of my code: there 2 text zone that we use to put the minimal number and the max number then we click on the generate button that will actualise the text that gives the number but my problem is that it doesent

here is my whole code if anyone wondering

import pygame
import pygame.freetype
import random
pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode([1000, 800])
base_font = pygame.font.Font(None, 32)
generate_font = pygame.freetype.Font(None, 32)
GAME_FONT = pygame.freetype.Font(None, 60)
user_text = '0'
user_text2 = '100'
final_number = '0'
text = 'GENERATE'
font = pygame.font.SysFont(None, 16)


pygame.display.set_caption('random number generator')
input_rect = pygame.Rect(200, 500, 400, 32)
input_rect2 = pygame.Rect(200, 550, 400, 32)
button = pygame.Rect(400, 650, 200, 50)

color_active = pygame.Color('grey')

color_passive = pygame.Color('grey9')
color = color_passive

color_active2 = pygame.Color('grey')

color_passive2 = pygame.Color('grey9')
color2 = color_passive2

color_G = pygame.Color('grey9')

active = False
active2 = False

running = True
while running:

    number_1 = (user_text)
    number_2 = (user_text2)

    def generate():
        if not number_1 == '':
            if not int(number_1) > int(number_2):
                final_number = random.randint(int(number_1), int(number_2))
                final_number = str(final_number)
                print(final_number)
    def update():
        number_text = font.render((final_number), 1, (0, 0, 0))
        screen.blit(number_text, (200, 300))
        

    
        

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False
            if input_rect2.collidepoint(event.pos):
                active2 = True
            else:
                active2 = False
            if button.collidepoint(event.pos):
                generate()
                update()
                                       
         if event.type == pygame.KEYDOWN:
            if active:
                if event.key == pygame.K_BACKSPACE:
                    user_text = user_text[:-1]
                elif event.key == pygame.K_0:
                    if active:
                         user_text += event.unicode
                elif event.key == pygame.K_1:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_2:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_3:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_4:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_5:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_6:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_7:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_8:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_9:
                    if active:
                        user_text += event.unicode
                elif event.key == pygame.K_MINUS:
                    if active:
                        if user_text == '':
                            user_text += event.unicode
            if active2:
                if event.key == pygame.K_BACKSPACE:
                    user_text2 = user_text2[:-1]
                elif event.key == pygame.K_0:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_1:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_2:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_3:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_4:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_5:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_6:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_7:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_8:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_9:
                    if active2:
                        user_text2 += event.unicode
                elif event.key == pygame.K_MINUS:
                    if active2:
                       if user_text2 == '':
                            user_text2 += event.unicode
        

    screen.fill((255, 255, 255))

    if active:
        color = color_active

    else:
        color = color_passive
    if active2:
        color2 = color_active2

    else:
        color2 = color_passive2

    pygame.draw.rect(screen, color, input_rect)
    pygame.draw.rect(screen, color2, input_rect2)
    pygame.draw.rect(screen, color_G, button)
    GAME_FONT.render_to(screen, (20, 10), "RANDOM NUMBER GENERATOR", (0, 0, 0))
    generate_font.render_to(screen, (410, 660), "GENERATE", (255, 255, 255))




    text_surface = base_font.render(user_text, True, (255,255, 255))
    text_surface2 = base_font.render(user_text2, True, (255,255, 255))
    generate = generate_font.render(text, True, (255,255, 255))
    final = base_font.render(text, True, (255,255, 255))

    screen.blit(text_surface, (input_rect.x+5, input_rect.y+5))
    screen.blit(text_surface2, (input_rect2.x+5, input_rect2.y+5))






    input_rect.w = max(200, text_surface.get_width()+10)
    input_rect2.w = max(200, text_surface2.get_width()+10)

    update()

    pygame.display.flip()

    clock.tick(120)

    pygame.quit()
BOB
  • 23
  • 5

1 Answers1

0

You must use the global statement to change a variable in global namespace:

def generate():
    global final_number                    # <---

    if not number_1 == '':
        if not int(number_1) > int(number_2):
            final_number = random.randint(int(number_1), int(number_2))
            final_number = str(final_number)
            print(final_number)

I recommend reading Using global variables in a function.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @BOB See [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Rabbid76 Mar 13 '22 at 15:04