Reputation: 39
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!
Upvotes: 2
Views: 963
Reputation: 46479
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.
Upvotes: 1