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



我正在python 3.8中创建一个双人猜号游戏。我想要一个倒计时计时器在后台运行,这样当时间用完时程序就会终止,但它不起作用。我尝试使用我在网上找到的解决方案,但该程序不会被终止。

声明所有变量和模块

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

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

您需要使用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()

我会把游戏放在一个名为game的函数中,并在守护进程线程中运行它,这样当主线程终止时它就会终止。如果N_SECONDS是分配给游戏的秒数,则主线程变为:

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

无需额外计时

最新更新