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:
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:
def display_the_game(x, y):
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.