Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import threading
- import os
- import time
- import msvcrt
- import queue
- cls = lambda: os.system('cls') #The keyword "lambda" is very similar to "def", but it works in a single line, so this makes a really small function that manually clears the screen
- def timerFunc(tqueue,cutofftime): #tqueue is a queue object, threads have to use special objects to communicate with the rest of the program, they are isolated, seconds_passed can not be viewed outside this thread
- seconds_passed = 0
- inputstring = ''
- while True:
- time.sleep(.01)
- seconds_passed += .01
- if(seconds_passed >= cutofftime):
- tqueue.put('FAIL')
- print('\nOUT OF TIME!') #There are better ways to do this, since this will still print the LOLNO bit even though the user never guessed, but its easy
- break
- print('\rTIME REMAINING: ' + str(cutofftime - int(seconds_passed)) + ' ?:' + inputstring,end='',flush=False) #The \r is something called a 'carriage return', it means to overwrite the last line instead of making a new one
- if msvcrt.kbhit(): #This uses a special module called msvcrt to see if the user pressed a key
- pushed = msvcrt.getch()
- if pushed == b'\x08': #This checks if the user pushed backspace and moves the cursor back if they did
- inputstring = inputstring[:-1]
- elif pushed == b'\r': #This checks if the user pushed the enter key
- tqueue.put(inputstring)
- break #If they did, it puts the string into the queue so the rest of the program can see it and breaks its loop, thus ending the thread
- else:
- inputstring = inputstring + pushed.decode('utf-8') #This will take the keycode which is stored as a byte object and turns it into a letter if it is not enter or backspace
- morse_dict = {'A': '.-', 'B': '-...', 'C': '-.-.',
- 'D': '-..', 'E': '.', 'F': '..-.',
- 'G': '--.', 'H': '....', 'I': '..',
- 'J': '.---', 'K': '-.-', 'L': '.-..',
- 'M': '--', 'N': '-.', 'O': '---',
- 'P': '.--.', 'Q': '--.-', 'R': '.-.',
- 'S': '...', 'T': '-', 'U': '..-',
- 'V': '...-', 'W': '.--', 'X': '-..-',
- 'Y': '-.--', 'Z': '--..',
- '0': '-----', '1': '.----', '2': '..---',
- '3': '...--', '4': '....-', '5': '.....',
- '6': '-....', '7': '--...', '8': '---..',
- '9': '----.'
- }
- Score = 0
- while True:
- cls() #clears the screen
- dif = ''
- while dif != '1' and dif != '2' and dif != '3':
- dif = input('DIFFICULT LEVEL 1 2 OR 3?:')
- if dif == '1':
- wordsize = 8
- cutofftime = 60 #Replace these numbers with whatever you want
- scoreinc = 1
- elif dif == '2':
- wordsize = 16
- cutofftime = 40
- scoreinc = 2
- elif dif == '3':
- wordsize = 20
- cutofftime = 30
- scoreinc = 3
- word = ''.join(random.choice('0123456789ABCDEF') for i in range(wordsize))
- morse = [morse_dict[i] for i in word]
- morse = ''.join(morse)
- print('CURRENT SCORE: ' + str(Score) + '\n')
- print('GUESS THIS!\n')
- print(morse + '\n')
- tqueue = queue.Queue()
- timer_thread = threading.Thread(None,target = timerFunc, args = (tqueue,cutofftime)) #creates the thread, the None at the beggining is there for some weird reasons, but it does have to be there, and it will always be None, Target tells it what function to run outside of the rest of the program
- timer_thread.daemon = True #This means that if this thread is left running alone, end the program, so if the main program somehow ends, the other thread wont stay running alone forever
- timer_thread.start()
- timer_thread.join() #This blocks the code below here for doing its stuff until timer_thread has ended
- guess = tqueue.get() #Gets the string out of the queue
- if guess.upper() == word:
- print('\nYOU WIN WOW!')
- Score += scoreinc
- else:
- print('\nNOPE LOL WAT A NERD\n')
- print('IT WAS ACTUALLY ' + word)
- do_restart = ''
- while do_restart != 'Y' and do_restart != 'N':
- do_restart = input('WANT TO TRY AGAIN? (y/n):')
- do_restart = do_restart.upper()
- if do_restart == 'Y':
- continue
- elif do_restart == 'N':
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement