2

I have just gotten into python and am trying to create a quiz game. I would like to add a countdown timer of 2 minutes to my quiz so when the timer reaches zero the questions stop and says you are out of time. I also tried to do this with a stopwatch but couldn't figure out how to terminate play so if you know how to do that it would be just as good.

Note: I am NOT using pygame. Many other users have asked in previous threads.

This is the code I gathered from the other documents but it doesn't stop once it reaches 2 mins or 0 mins.

def countdown():
    t = 60
    while t:
        mins, secs = divmod(t, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='\r')
        time.sleep(1)
        t -= 1
    print("You're out of time!\n")
    sys.exit ("goodbye")

def main_game():
    count_thread = threading.Thread(None, countdown)
    question_4 = ("question 1?")
    option_4 = (" a. 1 \n b. 5 \n c. 3 \n d. 7")
    print(question_4)
    print(option_4)            
    answer_4 = input (">")
    if answer_4.lower()== "b":
        score = score + 100
    else:
        print("Incorrect")
import time
start1 = time.time()
if start1 >= 120:
      import sys
      sys.exit
question_a = ("question?")
option_a = (" a. 7 \n b. 8 \n c. 2 \n d. 1")
print(question_a)
print(option_a)            
answer_a = input (">")
if answer_a.lower()== "b":
     score = score + 100
else:
    print("Incorrect")
###rest of questions

end1 = time.time()

Both codes are two different versions I have tried. Both codes don't work. The bottom code times the play but it doesn't stop at the 2 minute mark.

Any help, advice, or feedback would be appreciated!

bella
  • 39
  • 5
  • 1
    you have to use `count_thread.run()` to run thread. – furas May 23 '19 at 09:58
  • look for function `getchar()` - it doesn't block console so you can create loop which check keyboard and check time - and it doesn't need threading. But it is not standard function: https://stackoverflow.com/questions/2408560/python-nonblocking-console-input – furas May 23 '19 at 10:01
  • where would I add this? – bella May 24 '19 at 02:09
  • you have to use `run()` after you create `count_thread` and before you use `input()` which blocks code. – furas May 24 '19 at 09:03

1 Answers1

1

Since you use Pygame.

The simplest code you may have should (by good codding standards) include pygame.quit(). Like this:

import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
               pygame.quit()
               sys.exit()
               pygame.display.update()

Followed by Python sys.exit function.

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

You can make the quiz game w/o explicit use of Python threads, but If you plan to use Python threads you can start by checking the quick and dirty example from here.

Hope this helps. You already can find quizzes in pygame online and possible modify that.

prosti
  • 42,291
  • 14
  • 186
  • 151
  • Thanks for the help! Where would you suggest adding the sys.exit function and to which code? – bella May 23 '19 at 10:44
  • I was not particularly attached to the question @bella, I will edit my answer since you use pygame, and you use threads and you already have sys.exit. :) – prosti May 23 '19 at 11:14
  • Ok sorry about that. Ps. I am not using pygame I haven't learnt that mode yet. – bella May 23 '19 at 11:32
  • No worries @bella, keep with the good work. You will get even better answers than this, I am sure. I am adding the `pygame` tag. – prosti May 23 '19 at 11:35