4

I want to count time in pygame, when an event occurs. Ive read something in the documentation but I dont really understand on how to do it.

In the documentation you can get time in miliseconds but it starts counting when the pygame.init() is called. I want to count from 0 when the boolean is true.

import pygame
pygame.init()

loop = True

boolean = False

while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.RETURN:
                boolean = True

     screen.fill((255, 255, 255))

     if boolean:
        # start counting seconds

     pygame.display.update()

Thanks for your time.

Jože Strožer
  • 663
  • 1
  • 6
  • 14
  • There's an error in your code (typo?). The key constants start with `K_`, so it shoud be `pygame.K_RETURN` – r0the Aug 05 '17 at 11:16
  • Oh that doesnt matter even at all I just wanted to demonstrate.. I just need the code on how to count seconds.. – Jože Strožer Aug 05 '17 at 13:11
  • Possible duplicate of [Countdown timer in Pygame](https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame) – skrx Aug 05 '17 at 14:19
  • As you can see in the linked question, you have three choices. Either `pygame.time.get_ticks()`, the `dt = clock.tick(fps)` solution or `pygame.time.set_timer`. I usually use the dt (delta time) approach, because I need to pass the `dt` to my sprite classes as well to make them frame rate independent (multiply their velocity by `dt`). – skrx Aug 05 '17 at 14:40

2 Answers2

6

To determine the time that has passed since a certain event, you just measure the time at that event and subtract it from the current time.

Here's a working example:

import pygame

pygame.init()

FONT = pygame.font.SysFont("Sans", 20)
TEXT_COLOR = (0, 0, 0)
BG_COLOR = (255, 255, 255)

loop = True
start_time = None
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                start_time = pygame.time.get_ticks()

    screen.fill(BG_COLOR)

    if start_time:
        time_since_enter = pygame.time.get_ticks() - start_time
        message = 'Milliseconds since enter: ' + str(time_since_enter)
        screen.blit(FONT.render(message, True, TEXT_COLOR), (20, 20))

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

pygame.quit()
r0the
  • 617
  • 4
  • 13
  • Thank you very much! If i put a little more time and effort in this question i would have come up with it its so simple logic :D Thanks!! – Jože Strožer Aug 05 '17 at 19:56
0

I suggest setting a variable to the time in milliseconds whenever boolean is true, and to 0 whenever it is not.

Also, this is just a pet peeve of mine, but please don't call a boolean boolean, or a list list, or an int int, or any variable the type of variable it is (you don't really have to do this).

LW001
  • 2,452
  • 6
  • 27
  • 36