0

I'm building my first timer in Python and looking into time module. This is what I want:

  1. Inside while loop, when the if condition "A" is true for the first time, start timer

  2. When 10 seconds has passed, trigger an event

  3. When 30 seconds has passed, clear timer so that it's ready to start again when if condition "A" is true

I feel like I have all the building blocks but still cannot get my head around for starting the 10 second timer. In pseudocode, I want this:

if current_time - the_time_from_first_if_statement_match > 10:
        do something

This is what I have now. Any help appreciated!

def main():

start_time = time.time() 
time_limit = 10

while True:
   current_time = time.time() #keeps updating
   matchresult = video_process.match

   if matchresult == True:
      elapsed_time = current_time - start_time #can be anything like 14 or 100 at this point, I'd just want a 10 sec timer

      if elapsed_time > time_limit:
         print("do something")

3
  • 1
    matchresult is not defined. Commented Mar 29, 2022 at 14:58
  • This will print "do something" after 10 seconds. (assuming matchresult is true) and keeps doing so forever. How are you trying to solve Step #3? Commented Mar 29, 2022 at 15:01
  • @ScottHunter The problem with the current code is that elapsed_time can be 100 or 1000 when matchresult is true for the first time (it's an interactive piece that waits for people to come and interact). That means that elapsed_time is immediately more than time_limit in that case and no 10 sec timer is created. Step 3 I haven't tried yet before solving my first problem! Commented Mar 29, 2022 at 16:21

2 Answers 2

1

Something along these lines:

while True:
    while not condition:
        ...
    start_time = ...
    while elapsed_time < 10:
        ...
    do_something
    while elapsed_time < 30:
        ...
    
Sign up to request clarification or add additional context in comments.

Comments

-1
while True:
    if not paused:
        # Update timer
        elapsed_time = int(time.time() - start_time)
        remaining_time = max(0, time_limit - elapsed_time)
        timer_display.clear()
        timer_display.write(f"Time: {remaining_time}", font=("Arial", 16, "normal"))

        # End game if time runs out
        if remaining_time <= 0:
            end_game("Time's up! Game Over!")
            break

        # Move whale and fishes
        move_whale()
        move_fishes()

        # Check for speed boost expiration
        if speed_boost_active and time.time() > speed_boost_end_time:
            speed_boost_active = False

        # Check for invincibility expiration
        if invincibility_active and time.time() > invincibility_end_time:
            invincibility_active = False

        # Spawn fishes
        if time.time() - last_fish_spawn_time > 3:
            fish = turtle.Turtle()
            fish.shape(random.choice(fish_shapes))
            fish.penup()
            fish.goto(random.randint(-300, 300), random.randint(-200, 200))
            fishes.append(fish)
            last_fish_spawn_time = time.time()

        screen.update()
    else:
        timer_display.goto(0, 0)
        timer_display.write("Paused", font=("Arial", 24, "normal"))
        screen.update()

1 Comment

Don't add code-only answers. Explain what your code does, why it works, and how it helps to solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.