0

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).

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Torchllama
  • 43
  • 1
  • 7
  • because you are not rendering any text to screen; [read the docs](https://www.pygame.org/docs/ref/font.html#pygame.font.Font) – Matiiss Aug 13 '21 at 20:47
  • I took a look at it, but I still can't understand how could I introduce it. I looked for some tutorials, but nothing really explains what I am looking for. Could someone please give me an example of what they would do? Sorry, I'm a newbie, I would really appreciate that. – Torchllama Aug 13 '21 at 21:36

1 Answers1

0

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:

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • Nope, I have tried to apply what you told me, but I keep getting the sums in the python interpreter, not the window. Apart from that, if I click 'Comencem', that would be translated as 'let's start', the window freezes, but the sums appear at the interpreter. I will show the code. I hope anyone can fix this mess. – Torchllama Aug 14 '21 at 14:57
  • @Torchllama it freezes because you ask for `input()` input stops the script until you have inputed sth – Matiiss Aug 14 '21 at 15:10
  • Then how could I solve it? I really need to ask for an input() right there. – Torchllama Aug 14 '21 at 15:12
  • ok, but while nothing has been inputed it will freeze, or you need to get user input other ways for example use `threading` – Matiiss Aug 14 '21 at 15:16
  • Is it possible to ask for the input right in the beginning (at the menu, before pressing 'Comencem')? Because I could ask for the name and the age of the person right before the game starts, but what changes should I do in the code? – Torchllama Aug 14 '21 at 15:22
  • you ask for input in the main loop too, that therefore won't change much if you ask for input at the beginning, does it really matter tho if the window freezes, it will still update after input, or use `tkinter` for this, do you really need a game engine? – Matiiss Aug 14 '21 at 15:28
  • In this part of the code: menu.add.text_input('Usuari: ', font_name = font1, font_color = 'blue'), it asks who is the user who is using the program, and it allows you to write the name. How could I put a variable here to storage the words that you have written? Because if I can do this, I would be able to eliminate the inputs from the main loop. – Torchllama Aug 14 '21 at 15:35
  • @Torchllama use `global` and set the variables to be global that way other function may access them – Matiiss Aug 14 '21 at 15:51
  • How could I make them global? – Torchllama Aug 14 '21 at 16:08
  • @Torchllama use `global` keyword: https://www.programiz.com/python-programming/global-keyword – Matiiss Aug 14 '21 at 16:11
  • I can't figure it out. I will update the code later and post the changes for you to see. Thanks anyways @Matiiss – Torchllama Aug 14 '21 at 16:21
  • Alright, now the only variable right now is the puntuacio (score). But as I have said before, I still can't make it appear on screen. How would you change the code? The global variables is an issue that I have decided to take care after displaying the sums on the window. – Torchllama Aug 14 '21 at 20:33
  • @Torchllama I edited my answer, take a look – Matiiss Aug 14 '21 at 20:40
  • So I will have to make up something to replace the input. Right now, the input is only used to ask the user if he wants to stop doing sums. Maybe I could try to replace the input with an image, and if you click on it, it will automatically stop the program. Could that work? Because currently I don't have any other idea. The image would be always at a specific position when the sums start. What do you think? – Torchllama Aug 14 '21 at 20:56
  • @Torchllama yes you can use an image tho you can also use just some surface or draw a shape and render text there but you still will have one input which is asking for user to input the sum or sth which will be harder to deal with. you can try creating an entry box following this question (and the accepted answer I guess) https://stackoverflow.com/questions/46390231/how-can-i-create-a-text-input-box-with-pygame to get user input from the pygame window (or as I have suggested if the game is this simple maybe consider using `tkinter`) – Matiiss Aug 14 '21 at 21:01
  • This is just supposed just to be one part of the program. I am supposed to do also a substraction section. Maybe I could consider making the entry box, but I don't even know how to start. I wish I could use tkinter, but my teacher already told me to do it with Pygame. I am really appreciating your help. – Torchllama Aug 14 '21 at 21:19
  • I really don't like when teachers make you use a tool with which you can technically achieve the same results but there are other tools with whom you can achieve them easier and faster, it is like using a [this hammer](https://www.therange.co.uk/_m3/8/3/1476375636_835.jpg) instead of [this](http://mobileimages.lowes.com/productimages/06b48d3b-f329-46cd-8dd4-3fef971bee2b/04938592.jpg) to nail a nail (not exactly that bad in this case but you get the idea) also are you even allowed to receive help from other people? and if you have another question it would be better if you posted a new one – Matiiss Aug 14 '21 at 21:25
  • @Torchllama so know that you know how to render text, for user input I would suggest asking a separate question – Matiiss Aug 14 '21 at 21:27
  • Alright, I will post some new questions. Yeah, there are plenty of things that could make my work easier to do, but teacher really want to make us learn their way, even though it can make it harder. Apart from that, I just started learning Python like a month ago, and the teacher told me I am on my own, because she will just supervise the code, but won't do anything (to make sure I am using Pygame). Anyway, I really appreciate your help dude, I hope I can still rely on you in the future. – Torchllama Aug 14 '21 at 21:37
  • I created a new question related to this: https://stackoverflow.com/questions/68795124/input-freezes-the-pygame-window-entry-box-school-project – Torchllama Aug 15 '21 at 20:29