I am doing a math program for my school project, and I have already prepared the menu for the game and the process of the game, that basically consists in doing endless loop of sums till you fail, giving you 5 points for every correct answer (in the future I have planned to connect it to a SQLite base, because it asks you a name and your age). Anyway, here are the code of the sums and the menu:
https://www.sololearn.com/Discuss/2848691/help-me-to-turn-this-into-a-function-and-make-this-infinite-school-project https://www.sololearn.com/Discuss/2856421/making-the-school-project-menu-with-pygame-menu-school-project
[Updated] The problem consists in when I try to make the code of the sums to appear in the window that the menu produces after pressing 'Comencem', the sums appear in the Python Console, but not in the window. I would like it to appear in the window, but I don't know how to do it. Any help please?
import pygame
import pygame_menu
from pygame import mixer
import random
pygame.init()
#Mida i nom de la finestra
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
#Logotip de la finestra
logotip = pygame.image.load("calculator.png")
pygame.display.set_icon(logotip)
font = pygame_menu.font.FONT_8BIT
font1 = pygame_menu.font.FONT_NEVIS
def start_the_game():
# Variables
puntuacio = 0
while True:
x = random.randint(0, 10)
y = random.randint(0, 10)
z = x + y
print(str(x) + "+" + str(y))
resultat = int(input())
if resultat == z:
print("Correcte")
puntuacio = puntuacio + 5
print("Tens aquests punts:", puntuacio)
else:
if resultat != z:
print("Malament!")
parar = input("Vols parar? ")
if parar == ("si"):
print("Has aconseguit " + str(puntuacio) + " punts")
break
else:
continue
menu = pygame_menu.Menu('Projecte MatZanfe', 600, 400,
theme=pygame_menu.themes.THEME_SOLARIZED)
menu.add.text_input('Usuari: ', font_name = font1, font_color = 'blue')
mixer.music.load('MusicaMenu.wav')
mixer.music.play(-1)
menu.add.text_input('Edat: ', font_name = font1,font_color = 'Black')
menu.add.button('Comencem', start_the_game,font_name = font, font_color = 'green')
menu.add.button('Sortir', pygame_menu.events.EXIT, font_name = font,font_color = 'red')
menu.mainloop(surface)
(Some words are in catalan but I think you can understand them. I will change it if you can't).
Here is a very simple example of how to render text in pygame
:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 400))
font = pygame.font.SysFont('comicsans', 50)
x = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill((0, 0, 0))
text = font.render(str(x), True, (255, 255, 255))
screen.blit(text, (0, 0))
pygame.display.update()
x += 1
Here is how your function would look like:
import pygame
import random
pygame.init()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)
def start_the_game():
# Variables
points = 0
while True:
x = random.randint(0, 10)
y = random.randint(0, 10)
z = x + y
surface.fill((0, 0, 0))
text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))
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("Correcte")
points += 5
print("Tens aquests punts:", points)
else:
if result != z:
print("Malament!")
parar = input("Vols parar? ")
if parar == "yes":
print("Has aconseguit " + str(points) + " punts")
break
else:
continue
start_the_game()
The issue is that input
doesn't allow script to run which freezes the window
Sources: