Ask coding questions

← Back to all posts
How can I create a game timer?
Potatert0t (0)

Hello all! I've been working on a monopoly clone in Python, and I was hoping to be able to add a stopwatch kind of thing, and whenever the game hits the 30-40 minute mark the game will end, and whoever has the most money wins. I'm not that great at Python yet, so I don't know how to make one myself. Would the time module have some sort of timer or stopwatch command? Thanks in advance!

RhinoRunner (692)

time.perf_counter() might work.

import time

length_of_game = 0

while True:
    turn_start = time.perf_counter() #time at start
    #put the game loop stuff for playing monopoly here 
    turn_end = time.perf_counter() #time at end
    how_long = int(turn_end) - int(turn_start) #amount of time in between, in seconds as an integer
    length_of_game += how_long
    if length_of_game >= 2100: break #ends loop if the time is >= 35 minutes