🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to use get_time() method in Pygame

Started by
5 comments, last by jomodev 4 years, 6 months ago

I read about get_time() method on Pygame documentation and other

then try to coding but its output it's not same i think

this below is my code

and output

i think it will print 10 but it's not show.

Anybody can tell me how get_time() method work?




Advertisement

For the next time, please use a code block to insert code in a post (the "</>" symbol in the editor), it makes copy/paste possible which saves time and eliminates typos.

As for question, actually, it outputs "0" after 1000 seconds as per https://docs.python.org/3/library/time.html?highlight=time%20sleep#time.sleep

It looks like it works as intended to me. I am not sure why you think outputs 10, as you haven't even called "a.tick()" twice in the code so there isn't a "time between the previous two calls to tick" as apparently needed by get_time().

As I never used pygame.time.Clock, I can;t help you much further, but hopefully I left a few pointers to get you on your way again.

You have to call pygame.init() I think then

call a.tick() after sleep to update the clock

then you can call get_time() successfully

</

import pygame,time

pygame.init()

print("hello")

a=pygame.time.Clock()
a.tick()
#time.sleep(1000)
time.sleep(10)
a.tick()

print(str(a.get_time()))

>

Doug

The other option would be to use 'get_rawtime' which should not factor in any extra time between ticks.

This will wait 10 seconds and then display the time in seconds and quit.

import pygame,time
import sys

pygame.init()

print("hello")

a=pygame.time.Clock()
a.tick()
pygame.time.wait(10000)
a.tick()
stop_time = (int)(a.get_rawtime() / 1000)
print(str(stop_time))
pygame.quit()
sys.exit()

jomodev said:
stop_time = (int)(a.get_rawtime() / 1000)

Python 3 has // for integer division, so you can do "stoptime = a.getrawtime() // 1000" without having to cast to an integer.

Yeah, floor division would be valid in this instance, however if either value is a decimal then the result will be a decimal as well.

This topic is closed to new replies.

Advertisement