0

I'm making an unoriginal game for a first project that just runs in my python terminal. The user is randomly given a set of 2-3 letters and the user has to come up with a real word (checked by the Webster dictionary) that contains the given set of letters within 5 seconds. For example, if the game generates "le" the user can input "elephant" within 5 seconds as a correct word and gives them a point.

The problem is that I can't seem to implement the 5 second timer function to run in the back for every time a prompt is given without messing up some other part or running into another problem. I've looked into threading and can't seem to make use of it.

Here is the code for the main game:

from letters import letter_sets_list

fhand = open("words_dictionary.json")
data = fhand.read()

global score 
score = int(0)
game_over = False
while game_over is False:
    import random
    random_letters = random.choice(letter_sets_list)
    print('Word that contains:', random_letters)

    answer = input("Type a word:")
    if answer in data and random_letters in answer:
        score += 1
        print("nice one")
    else:
        game_over = True
        
print("Game Over \n Score:", score)   
fhand.close()
exit()

Here is the timer function I found off YouTube and tried to implement:

def countdown():
    global my_timer

    my_timer = int(5)

    for x in range(5):
        my_timer -= 1
        sleep(1)

countdown_thread = threading.Thread(target=countdown)
countdown_thread.start()
Blustuff
  • 1
  • 1

1 Answers1

0

Take a look at that. Especially check if that will work for you:

import time
from threading import Thread

answer = None

def check():
    time.sleep(2)
    if answer != None:
        return
    print("Too Slow")

Thread(target = check).start()

answer = input("Input something: ")

Edit: I tried to implement code from my previous answer to your code but with a little different approach:

import time, threading
#from letters import letter_sets_list
#import random

#fhand = open("words_dictionary.json")
#data = fhand.read()
data = ["answer"]

answer = [None]
random_letters = [None]

global score 
score = int(0)
game_over = False
x = 0

def game(answer, random_letters):
    #random_letters = random.choice(letter_sets_list)
    print('Word that contains:', random_letters)
    while True:
        answer[0] = input("Type a word: ")

mythread = threading.Thread(target=game, args=(answer, random_letters))
mythread.daemon = True
mythread.start()

for increment in range(5):
    time.sleep(1)
    if answer[0] in data:       # and random_letters in answer
        score += 1
        print("Good answer")
        x = 1
        break
   
if x != 1:    
    print("\nTime is up")
else:
    x = 0
game_over = True

In this approach I didnt use time.sleep() inside threaded function but outside of it which helps with killing it. Also i assumed that if your answer is incorrect you would like to have another chance to answer untill time is up or answer is correct so I just used while loop in threaded function. The code is quite simple so I think if you spend a little time analysing your parts of the code you will figure it out.

Some parts of the code I didn't use as I dont have access to your json files ect., but if I understand correctly what you're trying to do, it shoud work. Also check how will behave your kernel. In my case sometimes it shows some problems but my PC is not the best and so it might be only problem with RAM or other part of my computer as it happens quite often with my projects.

Paproch
  • 1
  • 3
  • Are you able to guide me on fitting this into my code? I've been trying to apply this code and the other answer but seem to end up with problems such as the timer not resetting to 5 seconds after looping through the while game_over loop. Also, if you know how to exit/stop my program after the time runs out that would be appreciated. Thanks for the help so far! – Blustuff Aug 13 '22 at 23:05
  • With the new code everything works such as the countdown and multiple chances to answer within the time limit (Which your assumption was correct) but the answer checking system with the webster dictionary and whether the word contains the random letters doesn't work or function correctly. I've messed around with the code and I think it may be the lists, or how the data variable was set to "answer" which I'm completely confused about since the data variable would be used to check for if it was in the dictionary. Sorry for my incompetence as a beginner. I may have a hard time with interpretation – Blustuff Aug 15 '22 at 02:46
  • In my example I used `data = ["answer"]` for testing as I dont't have your `words_dictionary.json` file. In my code I left some of the lines that you can use and marked them as comments (#comment). Also at the start of the program you should swap my declaration of "answer" with your .json file which was used only for testing. As I don't know how does the data inside that file look, make sure that either variable `answer` is a list with elements of .json file inside or if it's dictionary customize `if answer[0] in data` statement so it will check correctly. – Paproch Aug 15 '22 at 06:48