1

I have been working on a Pygame project, and recently I have added a timer to the program that appears on screen. The program basically generates 2 random numbers, and displays them on screen. Then, the user can put the result on the entry box. If it is correct, it will add 5 points to "Puntuació" (is the same as score). Everything worked fine and fluently until I added the timer. Right now this code still works and does not crash, but it runs really slowly. I am sure it must be related to the timer part, but don't know how to fix it. [EDITED]

import pygame
import random
from InputBox import InputBox
from pygame import mixer
import time

pygame.init()
pygame.time.set_timer(pygame.USEREVENT, 60000)
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
points = 0
t = 60
def TimeFunction():
    t = 60
    while True:
        time.sleep(1)
        t = t - 1
        return t

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

def display_the_game(x, y):
    # Variables
    z = x + y
    surface.fill((255, 70, 90))
    text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))

    text_surface = base_font.render(user_text, True, (255, 255, 255))
    surface.blit(text, (260, 120))
    input_box.draw(surface)
    punts = font.render("Puntuació: " +  str(points),True, (255,255,255))
    surface.blit(punts, (350,30))
    titolsuma = font.render("SUMA (1)", True, (0,0,0))
    surface.blit(titolsuma,(10,20))
    temps = font.render("Temps:" + str(t),True, (255,255,255))
    surface.blit(temps,(0,220))

x, y = start_the_game()
input_box = InputBox(190, 250, 200, 32)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.USEREVENT:
            t -= 1
            temps = font.render("Temps:" + str(t), True, (255, 255, 255))
            surface.blit(temps, (0, 220))
            pygame.display.flip()
            if int(t) == 0:
                pygame.QUIT
        else:
            result = input_box.handle_event(event)
            if result != None:
                if int(result) == int(x) + int(y):
                    points = points + 5
                    mixer.music.load('StarPost.wav')
                    mixer.music.play(1)

                # create new random numbers
                x, y = start_the_game()

                # reset input box (just create a new box)
                input_box = InputBox(190, 250, 200, 32)


    display_the_game(x, y)
    pygame.display.update()

pygame.quit()

Here is the code of the imported InputBox (just in case).

import pygame


pygame.init()
surface = pygame.display.set_mode((600, 400))
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('black')
FONT = pygame.font.SysFont('comicsans', 32)
base_font = pygame.font.Font(None, 32)
color_active = pygame.Color('lightskyblue3')
user_text = ''


class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                # Toggle the active variable.
                self.active = not self.active
            else:
                self.active = False
            # Change the current color of the input box.
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    user_input = self.text
                    self.text = ''
                    self.txt_surface = FONT.render(self.text, True, self.color)
                    return user_input
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # Resize the box if the text is too long.
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        # Blit the rect.
        pygame.draw.rect(screen, self.color, self.rect, 2)



def main():
    clock = pygame.time.Clock()
    input_box2 = InputBox(190, 250, 200, 32)
    input_boxes = [input_box2]
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)

        for box in input_boxes:
            box.update()

        surface.fill((255, 70, 90))
        for box in input_boxes:
            box.draw(surface)

        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pygame.quit()
Torchllama
  • 43
  • 1
  • 7
  • The timer takes the number 60 as the beginning, and after 1 second it subtracts 1. – Torchllama Aug 25 '21 at 15:48
  • 1
    Why the call to `time.sleep(1)`? – Iain Shelvington Aug 25 '21 at 15:52
  • 1
    I don't get what is `time.sleep(1)` supposed to do? Why do you delay the application loop in this way? `time.sleep(1)` makes the loop not responding for 1 second. Never do something like this. What are you trying to achieve? No matter what you want to do, `time.slee` is never the solution. – Rabbid76 Aug 25 '21 at 15:52
  • So where should I put it so it is displayed correctly on screen without lagging? I need to put a timer of 60 seconds, and after every second it subtracts 1, but I don't figure out how to make it appear and work as I want it to work. – Torchllama Aug 25 '21 at 15:58
  • 1
    Do not use `time.sleep` at all! Read [Countdown timer in Pygame](https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame/63551239#63551239) – Rabbid76 Aug 25 '21 at 16:00
  • I have updated to code so I the program doesn't use the time.sleep. I have been looking at the answers from that post, and I have updated a little bit. However, it does not display the timer, even though there are no crashes or problems. Now it just displays the 60 seconds. – Torchllama Aug 25 '21 at 16:42
  • 1
    You have to call `surface.blit(temps, (0, 220))` in the application loop. – Rabbid76 Aug 25 '21 at 16:46
  • Where is that? At the while running part? – Torchllama Aug 25 '21 at 17:10
  • I have tried to put it between the last part of the loop and it still does not work. I guess it is not the right place to put it right? – Torchllama Aug 25 '21 at 17:23

1 Answers1

1

You want to decrease the timer every second. Therefore, the timer time must be 1 second (1000 milliseconds) instead of 60 seconds (see pygame.time.set_timer):

pygame.time.set_timer(pygame.USEREVENT, 60000)

pygame.time.set_timer(pygame.USEREVENT, 1000)

See also Countdown timer in Pygame.

After an input, you have to reset t=60

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.USEREVENT:
            t -= 1
            temps = font.render("Temps:" + str(t), True, (255, 255, 255))
            surface.blit(temps, (0, 220))
            pygame.display.flip()
        else:
            result = input_box.handle_event(event)
            if result != None:
                if int(result) == int(x) + int(y):
                    points = points + 5
                    mixer.music.load('StarPost.wav')
                    mixer.music.play(1)

                # create new random numbers
                x, y = start_the_game()

                # reset input box (just create a new box)
                input_box = InputBox(190, 250, 200, 32)
                
                t = 60 # <--


    display_the_game(x, y)
    pygame.display.update()

pygame.quit()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Now it works! Thank you so much. As you probably have already seen, I'm a newbie, so I am still trying to learn a few things. You really helped me! – Torchllama Aug 25 '21 at 17:33
  • So if the timer reaches 0, how could I put it so the program stops? I have been trying this (please look at the `if event.type == pygame.USEREVENT:` part). – Torchllama Aug 25 '21 at 17:49