1

I have made a code that renders 2 different random numbers on screen, but it justs keep updating the numbers. I want the program to stop updating those numbers once the first 2 appear on screen. Here is the code:

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
    is_correct = False
    points = 0
    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,(260,120))
    input_rect.w = max(100,text_surface.get_width()+10)

running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    start_the_game()
    pygame.display.update()
pygame.quit()

Maybe using an if statement would be the solution, but I can't figure out where and what should I introduce to the code. How would you do it?

Torchllama
  • 43
  • 1
  • 7
  • Following your logic, you are constantly starting your game in the `while loop` at the end (`running` is always True). Maybe put the call to `start_the_game()` outside the loop. – Jan Aug 17 '21 at 16:27
  • If I put it outside the loop, the window it opens will be just black. It won't show anything (and I don't know why). – Torchllama Aug 17 '21 at 16:28
  • Why don't you put the `x` and `y` `= random.randint(0,10)` just above the `start_the_game()` function? – quamrana Aug 17 '21 at 16:30
  • I tried what you said quamrana and the window Will turn fully black. However, if I exit from the program, for a second before closing It Will show 2 numbers as I want them to be. – Torchllama Aug 17 '21 at 16:35
  • 4
    You need to split that function in two: a part that generates the random numbers (called outside the loop), and a part that renders the numbers to the screen (called inside the loop). – jasonharper Aug 17 '21 at 17:05

1 Answers1

1

As @jasonharper suggested, you probably are better off with two functions, one to initialize things and another to display the game:

def start_the_game():
    x = random.randint(0, 10)
    y = random.randint(0, 10)
    return x, y


def display_the_game(x, y):
    # Variables
    is_correct = False
    points = 0

    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, (260, 120))
    input_rect.w = max(100, text_surface.get_width() + 10)


x, y = start_the_game()
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    display_the_game(x, y)
    pygame.display.update()
pygame.quit()

By putting random.randint in a function outside the loop, it will only be called once.

Zev
  • 3,423
  • 1
  • 20
  • 41