The Python Oracle

Counting time in pygame

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 Counting Time In Pygame
00:37 Accepted Answer Score 6
01:10 Answer 2 Score 0
01:31 Thank you

--

Full question
https://stackoverflow.com/questions/4552...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pygame #pygameclock

#avk47



ACCEPTED ANSWER

Score 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()



ANSWER 2

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