Я сделал игру в крестики-нолики и добавил ограничение по времени в ход игры. Моя проблема заключается в том, как сделать оставшееся время видимым для игрока. Я не знаю, как одновременно запустить другую строку кода, например текст, отображающий оставшееся время с функцией ввода.
Это мой текущий код: (функция является частью класса)
from inputimeout import inputimeout, TimeoutOccurred
def play_turn(self):
while True:
try:
choice = int(inputimeout(prompt=f"{self.players[self.current_player].name}, choose a move: ", timeout=5))
if 1 <= int(choice) <= 9 and self.board.update_board(choice, self.players[self.current_player].symbol):
break
else:
print("Invalid move,try again")
except TimeoutOccurred:
print("Timed out!!")
time.sleep(2)
return
except ValueError:
print("Please enter a number between 1 and 9")
@quamrana, похоже, вопрос касается конкретного использования сторонней библиотеки, предназначенной для этой цели.
Для этого вы можете использовать потоки Python. Вы можете использовать эти две функции (countdownTimer и getUserInputTimeout) для реализации угрозы с помощью ввода.
Например:
import threading
import time
from inputimeout import inputimeout, TimeoutOccurred
def countdownTimer(timeout, timer_event):
for i in range(timeout, 0, -1):
if timer_event.is_set():
break # Exit the loop if the timer event is set
time.sleep(1)
def getUserInputTimeout(prompt, timeout):
# Create an event to signal the timer thread to stop
timer_event = threading.Event()
# Start the countdown timer in a separate thread
timer_thread = threading.Thread(target=countdownTimer, args=(timeout, timer_event))
timer_thread.daemon = True # Set the timer thread as a daemon thread
timer_thread.start()
try:
# Loop to wait for user input until timeout or input received
start_time = time.time()
while True:
remaining_time = timeout - int(time.time() - start_time)
if remaining_time <= 0:
raise TimeoutOccurred
print(f"\rTime left: {remaining_time} seconds: ", end='', flush=True)
try:
choice = inputimeout(prompt='', timeout=1)
return choice
except TimeoutOccurred:
pass
except TimeoutOccurred:
print("\nTimeout. No input received.")
finally:
timer_event.set()
timer_thread.join()
timeout_duration = 5
user_input = getUserInputTimeout(prompt = "Answer: ", timeout=timeout_duration)
if user_input is not None:
print(f"\nResponse: {user_input}")
Исполнение:
Для нашего кода вы можете реализовать это следующим образом:
import threading
import time
from inputimeout import inputimeout, TimeoutOccurred
def countdownTimer(timeout, timer_event):
for i in range(timeout, 0, -1):
if timer_event.is_set():
break # Exit the loop if the timer event is set
time.sleep(1)
def getUserInputTimeout(prompt, timeout):
# Create an event to signal the timer thread to stop
timer_event = threading.Event()
# Start the countdown timer in a separate thread
timer_thread = threading.Thread(target=countdownTimer, args=(timeout, timer_event))
timer_thread.daemon = True # Set the timer thread as a daemon thread
timer_thread.start()
try:
# Loop to wait for user input until timeout or input received
start_time = time.time()
while True:
remaining_time = timeout - int(time.time() - start_time)
if remaining_time <= 0:
raise TimeoutOccurred
print(f"\rTime left: {remaining_time} seconds: ", end='', flush=True)
try:
choice = inputimeout(prompt='', timeout=1)
return choice
except TimeoutOccurred:
pass
except TimeoutOccurred:
print("\nTimeout. No input received.")
finally:
timer_event.set()
timer_thread.join()
def play_turn(self):
while True:
try:
choice = int(getUserInputTimeout(prompt=f"{self.players[self.current_player].name}, choose a move: ", timeout=5))
if 1 <= int(choice) <= 9 and self.board.update_board(choice, self.players[self.current_player].symbol):
break
else:
print("Invalid move,try again")
except TimeoutOccurred:
print("Timed out!!")
time.sleep(2)
return
except ValueError:
print("Please enter a number between 1 and 9")
Помогают ли вообще ответы на этот вопрос ?