提问者:小点点

我试图在后台添加一个倒计时计时器,这样当时间用完时,程序就会终止


我正在用Python3.8创建一个2人的数字猜谜游戏。 我想要一个倒计时计时器在后台运行,使程序在时间耗尽时终止,但它不工作。 我已经尝试使用我在网上找到的解决方案,但程序没有得到终止。

声明所有变量和模块

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

我在网上找到的解决方案

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()

主程序

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

共1个答案

匿名用户

倒计时计时器的代码运行良好。 你可以做的是--当计时器达到零点时使用系统退出,这样你就可以停止程序的执行。

您需要使用os._exit(1)终止您的Python程序。 修改后的代码应该是这样的-

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()