Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. import random
  2. import threading
  3. import os
  4. import time
  5. import msvcrt
  6. import queue
  7.  
  8. 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
  9.  
  10. 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
  11.     seconds_passed = 0
  12.     inputstring = ''
  13.     while True:
  14.         time.sleep(.01)
  15.         seconds_passed += .01
  16.        
  17.         if(seconds_passed >= cutofftime):
  18.             tqueue.put('FAIL')
  19.             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
  20.             break
  21.        
  22.         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
  23.         if msvcrt.kbhit(): #This uses a special module called msvcrt to see if the user pressed a key
  24.             pushed = msvcrt.getch()
  25.             if pushed == b'\x08': #This checks if the user pushed backspace and moves the cursor back if they did
  26.                 inputstring = inputstring[:-1]
  27.             elif pushed == b'\r': #This checks if the user pushed the enter key
  28.                 tqueue.put(inputstring)
  29.                 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
  30.             else:
  31.                 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
  32.        
  33.  
  34. morse_dict = {'A': '.-',     'B': '-...',   'C': '-.-.',
  35.         'D': '-..',    'E': '.',      'F': '..-.',
  36.         'G': '--.',    'H': '....',   'I': '..',
  37.         'J': '.---',   'K': '-.-',    'L': '.-..',
  38.         'M': '--',     'N': '-.',     'O': '---',
  39.         'P': '.--.',   'Q': '--.-',   'R': '.-.',
  40.          'S': '...',    'T': '-',      'U': '..-',
  41.         'V': '...-',   'W': '.--',    'X': '-..-',
  42.         'Y': '-.--',   'Z': '--..',
  43.  
  44.         '0': '-----',  '1': '.----',  '2': '..---',
  45.         '3': '...--',  '4': '....-',  '5': '.....',
  46.         '6': '-....',  '7': '--...',  '8': '---..',
  47.         '9': '----.'
  48.     }
  49.    
  50. Score = 0
  51.    
  52. while True:
  53.     cls() #clears the screen
  54.    
  55.     dif = ''
  56.     while dif != '1' and dif != '2' and dif != '3':
  57.         dif = input('DIFFICULT LEVEL 1 2 OR 3?:')
  58.     if dif == '1':
  59.         wordsize = 8
  60.         cutofftime = 60 #Replace these numbers with whatever you want
  61.         scoreinc = 1
  62.     elif dif == '2':
  63.         wordsize = 16
  64.         cutofftime = 40
  65.         scoreinc = 2
  66.     elif dif == '3':
  67.         wordsize = 20
  68.         cutofftime = 30
  69.         scoreinc = 3
  70.        
  71.     word = ''.join(random.choice('0123456789ABCDEF') for i in range(wordsize))
  72.     morse  = [morse_dict[i] for i in word]
  73.     morse = ''.join(morse)
  74.        
  75.     print('CURRENT SCORE: ' + str(Score) + '\n')
  76.        
  77.     print('GUESS THIS!\n')
  78.     print(morse + '\n')
  79.        
  80.     tqueue = queue.Queue()
  81.        
  82.     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
  83.     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
  84.     timer_thread.start()
  85.    
  86.     timer_thread.join() #This blocks the code below here for doing its stuff until timer_thread has ended
  87.     guess = tqueue.get() #Gets the string out of the queue
  88.    
  89.     if guess.upper() == word:
  90.         print('\nYOU WIN WOW!')
  91.         Score += scoreinc
  92.     else:
  93.         print('\nNOPE LOL WAT A NERD\n')
  94.         print('IT WAS ACTUALLY ' + word)
  95.        
  96.     do_restart = ''
  97.     while do_restart != 'Y' and do_restart != 'N':
  98.         do_restart = input('WANT TO TRY AGAIN? (y/n):')
  99.         do_restart = do_restart.upper()
  100.    
  101.     if do_restart == 'Y':
  102.         continue
  103.     elif do_restart == 'N':
  104.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement