'When the time expires, the function starts again from the beginning
import pygame as pg
pg.init()
def fixTime():
totalTime = 4
return totalTime
while 1:
flowTime = int(pg.time.get_ticks()/1000)
elapsed_time = (fixTime() - flowTime)
if elapsed_time <=0:
fixTime()
else:
print(elapsed_time)
When the current ends, 1 is repeated, but I want to make a function that returns the time to the beginning.
Solution 1:[1]
Just use pygame.time.get_ticks():
Return the number of milliseconds since
pygame.init()was called.
You can just subtract 2 time values:
def get_elapsed_time(from_time = 0):
to_time = pygame.time.get_ticks()
return to_time - from_time
start_time = pygame.time.get_ticks():
while run:
elapsed = get_elapsed_time(start_time)
# [...]
pygame.time.get_ticks() returns the time in milliseconds. If you want the time in seconds you must divide by 1000. e.g.: return (to_time - from_time) / 1000
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rabbid76 |
