-1

I am creating a 2 player number guessing game in python 3.8. I want a countdown timer to run in the background so that the program is terminated when time runs out, but its not working. I have tried using a solution I found online but the program does not get terminated.

declaring all the variable and modules

import random
guess=""
Gamewinner=0
n = random.randint(1, 99)
lives = 8
answer = "yes"
print("You have a total of 8 lives. A wrong guess would result in one less life.")
SingleOrDouble=input("Do you want single player or double player?")
from time import *
import threading

The solution that I found online

def countdown():
    global my_timer
    
    my_timer=5

    for x in range(5):
        my_timer=my_timer-1
        sleep(1)
    print("Out of time")

countdown_thread=threading.Thread(target=countdown)
countdown_thread.start()

main program

while my_timer>0:
    if SingleOrDouble=="Single":
        while answer=="yes" or answer=="Yes":
            while lives!=0 and n!=guess:
                guess = int(input("Enter an integer from 1 to 99: "))
                if guess < n:
                    print("guess is low")
                    lives-=1
                    print("You have "+ str(lives) +" lives left.")
        
                elif guess > n:
                    print ("guess is high")
                    lives-=1
                    print("You have "+ str(lives) +" lives left.")
        
                else:
                    print("You guessed it!")
                    print("You won with "+ str(lives) +" lives left.")
                    break
            print("Game over, the number was "+ str(n))
            answer = input("Do you want to play again?")
            if answer=="yes" or answer=="Yes":
                lives=8
    else:
        lives=16 
        while answer=="yes"or answer=="Yes":
            while lives!=0 and n!=guess:
                if lives%2==0:
                    guess = int(input("Player 1 please Enter an integer from 1 to 99: "))
        
                else:
                    guess = int(input("Player 2 please Enter an integer from 1 to 99: "))
        
                if guess < n:
                    print("guess is low")
                    lives-=1
                    print("You have "+str(lives//2)+" lives left.")
        
                elif guess > n:
                    print ("guess is high")
                    lives-=1
                    print("You have "+ str(lives//2) +" lives left.")
        
                else:
                    if lives%2==0:
                        print("Player 1 guessed it!")
                        print("You won with "+ str(lives//2) +" lives left.")
                        Gamewinner=2
                    else:
                        print("Player 2 guessed it!")
                        print("You won with "+ str(lives//2) +" lives left.")
                        Gamewinner=1
                    break
            if Gamewinner>0:
                print("Game Over! Player "+str(Gamewinner)+"the number was "+ str(n))
            else:
                print("Game Over! The number was "+ str(n))
            answer = input("Do you want to play again?")
            if answer=="yes" or answer=="Yes":
                lives=8
    sleep(1)
    if my_timer==0:
        break

2 Answers2

1

The code for the countdown timer is working just fine. What you could do is - Use a system exit when the timer hits zero so that you could stop the execution of your program.

You would need to use os._exit(1) to terminate your Python program. The modified code would look something like this -

from time import *

import os
import random
import threading

guess=""
Gamewinner=0
n = random.randint(1, 99)
lives = 8
answer = "yes"

print("You have a total of 8 lives. A wrong guess would result in one less life.")
SingleOrDouble=input("Do you want single player or double player?")

def countdown():
    global my_timer
    
    my_timer=5

    for x in range(5):
        my_timer=my_timer-1
        sleep(1)
    print("Out of time")
    os._exit(1)

countdown_thread=threading.Thread(target=countdown)
countdown_thread.start()
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I would put the game in a function called game and run it in a daemon thread so that it terminates when the main thread terminates. If N_SECONDS is the number of seconds allotted to the game then the main thread becomes simply:

from threading import Thread
import sys

N_SECONDS = some_value

t = Thread(target=game)
t.daemon = True
t.start()
t.join(N_SECONDS) # block for N_SECONDS or until the game ends by returning
if t.is_alive(): # the game has not returned and is still running
    print('Out of time!')
sys.exit(0) # exit and the thread terminates with us

No additional timing is required!

Booboo
  • 38,656
  • 3
  • 37
  • 60