Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone.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 (I posted something related some time ago, but I improved it).

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


What I have tried:

Putting the x and y variables befor the start_the_game (), but it doesn't work.
Posted
Updated 17-Aug-21 22:50pm
v2

That is all your start_the_game function does, pretty much - print two random numbers on the screen.
And since it is called in a loop that does nothing except repeatedly say "tick at 60 ticks per second" and the call start_the_game that's what happens: it repeatedly calls the function and displays numbers.

I think that what you need to do is take a step back, think about what you are actually trying to do, and work it out as a whole design rather that jumping straight into code and hoping it will all work. It won't - and since we have no real idea what this "game" is supposed to do (and it isn't even slightly obvious from that code) we can't tell you what to do!
 
Share this answer
 
The issue is much the same as your previous question at Making this code unfreeze, and then introducing an entry box.[^]. You do not appear to understand how to control your loops properly. I would suggest spending some time working through The Python Tutorial — Python 3.9.6 documentation[^].
 
Share this answer
 
Quote:
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.

You want to generate the random numbers just once on a program that run many times.
The solution is rather simple: you want the random values to persist between runs
.
- Create a set of persistent variables as needed. Initialize them with out of range values, or add a flag to detect if random values are already generated.
- Generate the values
Python
if ! generated
    # set variables to random
    generated= true

and you are done, the variables will be generated only once.
 
Share this answer
 
Hey everyone. Thank you for all the advices you gave me. I have been thinking about this issue, and I have rethought what I really want to do.
It's my first "big" project, as you can imagine. Anyway, I need to display two random numbers on screen. Instead of making it a huge function like it used to be, now I have splitted it on different functions. To generate a random number:
Python
def start_the_game():
    x = random.randint(0, 10)
    y = random.randint(0, 10)
    return x, y


To show them on screen, I have thought of this function:
Python
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)


This way I stopped an endless-huge-hard function to something it is easier to work with.
So what I need would actually look like this:

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')
running = True
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()


I have not explained this before, but this is just a part of a program I am creating about simple maths. I have already prepared the menu, and I will need some help in the future. I think I have faced all the problems you suggested me. Please don't hesistate on what I could upgrade. See ya.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900