1

project is to make a timer with resume/pause/reset/stop features. So in my previous question, someone recommended using pygame. So accordingly I used Pygame. When i tried to simulate the successful execution of keydown from a previous project, it failed to execute. The while loop is completely ignoring my keydown and continues looping. Tried moving the keydown code into the while loop, failed again. How should i execute this successfully?

Output image given below. My keydown commands are not being read, and the 'W' key I pressed during the execution of the while loop are being input at the end.

import time
import datetime
import pygame

pygame.init()

timeron = True

while timeron:
    def timeinput():
        h = input("Enter the time in hours: ")
        m = input("Enter the time in minutes: ")
        s = input("Enter the time in seconds: ")
        countdown(int(h), int(m), int(s))

    def countdown(h, m, s):
        print("Press w to reset \nPress s to stop \nPress a to pause \nPress d to resume")

        total_seconds = h * 3600 + m * 60 + s
        while total_seconds > 0:
            timer = datetime.timedelta(seconds=total_seconds)
            print(timer, end='\r')
            time.sleep(1)
             total_seconds -= 1

        print("Bzzzt! The countdown is at zero seconds!")

    timeinput()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            timeron = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
               total_seconds = 0
               print("Timer will reset")
               timeinput()


pygame.quit()

result

  • your `while`-loop runs all time and pygame can't run `for event` loop to check keys. You would have to run another `for event` inside `while`-loop – furas Jun 20 '22 at 20:59

0 Answers0