0

Stopwatch in Python

I need a stopwatch for a game I'm trying to make in python but I can't work out how to code this. And help and suggestions would be much appreciated, thanks.

28th Dec 2018, 10:09 AM
Will Beech
Will Beech - avatar
5 Answers
+ 8
Yes, that was just a demo. The code displays the current time at each loop iteration. The loop itself takes some time tobe processed (loops are *really* slow), this is why you get fractions of seconds piling up. In a game you probably need to take time.time(), constantly checking the current time and increasing a stopwatch indicator as soon as the time difference exceeds one second. https://code.sololearn.com/ckBSI6ceZv2z/?ref=app
28th Dec 2018, 11:15 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
28th Dec 2018, 10:20 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 2
import time seconds=0 while True: time.sleep(1) seconds+=1 print(seconds, end='\r')
2nd Aug 2021, 5:07 AM
Sancho Godinho
Sancho Godinho - avatar
+ 1
Kuba SiekierzyƄski thanks for your help, only, when i change it to 30 seconds it outputs this: 0.0 1.0 All the way to 10.0 as it should 11.1 12.1 13.1 All the way to 29.1
28th Dec 2018, 10:28 AM
Will Beech
Will Beech - avatar
+ 1
Kuba SiekierzyƄski thanks, i just changed the 1f at the end to 0f to make it return an integer, then realised i could just remove the print so no one would know. It is only a 3 minute timer so shouldn't make a difference. Thanks for your help
28th Dec 2018, 12:20 PM
Will Beech
Will Beech - avatar