Demo entry 6117599

Code

   

Submitted by Ben Roberts on Sep 30, 2016 at 12:34
Language: Python 3. Code size: 11.6 kB.

import random as r
import time as t
import os
#clear screen function (easier to write)
def c():
    os.system("cls")

#cool dance animation (named badly due to original purpose)
def loseGame(win):
    print("            ", win)
    print("""              .-.
             (o.o)
              |=|
             __|__
           //.=|=.\\
          // .=|=. \\
          \\ .=|=. //
           \\(_=_)//
            (:| |:)
             || ||
             () ()
             || ||
             || ||
            ==' '==""")
    t.sleep(1)
    c()
    for x in range(2):
        print("            ", win)
        print("""              .-.
             (o.o)     ___
              |=|     //
             __|_____// 
           //.=|=.----
          // .=|=. 
          \\ .=|=. 
           \\(_=_)
            (:| |:
             || ||
             () ()
             || ||
             || ||
            ==' '==""")
        t.sleep(0.5)

        c()
        print("            ", win)
        print("""              .-.
     ___     (o.o)
       \\      |=|
        \\_____ |__
         ----.=|=.\\
             .=|=. \\
             .=|=. //
             (_=_)//
            (:| |:)
             || ||
             () ()
             || ||
             || ||
            ==' '==""")
        t.sleep(0.5)

        c()
    for x in range(2):
        print("            ", win)
        print("""              .-.
             (o.o)
              |=|
             __|__
           //.=|=.\\
          // .=|=. \\
        __|| .=|=. ||__
        ---  (_=_)  ---
            (:| |:)
             || ||
             () ()
             || ||
             || ||
            ==' '==""")
        t.sleep(0.5)

        c()
        print("            ", win)
        print("""
              .-.
             (o.o)
              |=|
             __|__
           //.=|=.\\
          // .=|=. \\
        __|| .=|=. ||__
        ---  (_=_)  ---
            (:| |:)
             //  \\
            ()    ()
             \\   //
            =='  '==""")
        t.sleep(0.5)

        c()


#helpfull help menu
def helpGame():
    print("====================================")
    print("You dont need help. This is handMan")
    print("    You are already expert")
    print("====================================")
    print()
    input("press any key to return")
    t.sleep(1)
    print("""

  _____.___.              ________                 __   
  \__  |   | ____  __ __  \______ \   ____   _____/  |_  
   /   |   |/  _ \|  |  \  |    |  \ /  _ \ /    \   __\  
   \____   (  <_> )  |  /  |    `   (  <_> )   |  \  |   
   / ______|\____/|____/  /_______  /\____/|___|  /__|   
   \/                             \/            \/       
""")
    t.sleep(1)
    print("""                        .___   ___ ___         .__          
  ____   ____   ____   __| _/  /   |   \   ____ |  | ______  
 /    \_/ __ \_/ __ \ / __ |  /    ~    \_/ __ \|  | \____ \ 
|   |  \  ___/\  ___// /_/ |  \    Y    /\  ___/|  |_|  |_> >
|___|  /\___  >\___  >____ |   \___|_  /  \___  >____/   __/ 
     \/     \/     \/     \/         \/       \/     |__|    
""")
    t.sleep(1)
    c()
#creates the immage and sorts into variables below
part13="""               ____    """
part12="""             ,'   Y`.      """
part11="""            /        \  """
part10="""            \ ()  () /                  """
part9="""    /)       `. /\ ,'        (\ """
part8="""   / )        | "" |         ( \ """
part7=""" _( (_      _ `LLLU'   _     _) ) """
part6=""" ((\  \   /_/         |_\   / /))) """
part5=""" (\\\\\\\ \_/ /           \ \_/ ////) """
part4="""  \       /             \       /"""
part3="""   \    _/               \_    / """
part2="""   /   /                   \   \ """
part1="""  /   /                     \   \ """
#groups all the image variables together
parts = [part1,part2,part3,part4,part5,part6,part7,part8,part9,part10,part11,part12,part13]
#list used incase the main list isn't found
randlist = ["computer","monitor","keyboard","mouse","randomaccessmemory","centeralprocessingunit","desktop","laptop","Lol"]
try:
    #trys to open the list of words
    externList = open("listOfWords.txt", "r")
    for word in externList:
        #imports all the words into rand list from the word list
        randlist.append(word.strip("\n"))
except FileNotFoundError:
    #if the files aren't found, prints a mesage
    print("Text file not found")
    print("Using default list instead")
    t.sleep(1)
    c()
else:
    #tellls the user that the word list was found
    print("text file found")
    t.sleep(1)
    c()
#main function
def playGame():
    #defines variables
    usedLetters = []
    #gets the random word to be used
    randWord = r.choice(randlist)
    finalWord = []
    #goes through the length of randWord
    for x in range(len(randWord)):
        #adds the characters of randWord to a list
        finalWord += randWord[x]
    #gets the length of randWord
    wordLength = len(randWord)
    underWord = []
    for x in range(wordLength):
        #this is the variable to be printed to the user
        #getts filled with underscores
        underWord.append("_")
    #defines variables
    lives = 7
    lose = False
    win = False
    #defines image stages and current stage
    stages = [[""],[""],[""],[""],[""],[""],[""],[""],[""],[""],[""],[""],[""]]
    stage = 0
    #loops until you either win or lose
    while lose == False and win == False:
        #prints a gui
        print("================================")
        print(stages[12][0])
        print(stages[11][0])
        print(stages[10][0])
        print(stages[9][0])
        print(stages[8][0])
        print(stages[7][0])
        print(stages[6][0])
        print(stages[5][0])
        print(stages[4][0])
        print(stages[3][0])
        print(stages[2][0])
        print(stages[1][0])
        print(stages[0][0])
        print("================================")
        print("|                               |")
        print("        " , "".join(underWord) , "                       ")
        print("|                               |")
        print("|       lives: ",lives,"              |")
        print("|                               |")
        print("|   please enter a letter       |")
        print("================================")
        print(", ".join(usedLetters))
        #gets the users input in lower case
        gameinput = input(" > ").lower()
        
        
        try:
            #try to make the user input an integer
            int(gameinput)
        except ValueError:
            #if the user input is a string, it should error and execute the following
            #checks if the user enters a single character
            if len(gameinput) != 1:
                print("Please enter a single letter")
                t.sleep(2)
                c()
            else:
                #user entered a correct input
                if gameinput in finalWord:
                    while gameinput in finalWord:
                        #defines variables
                        indexes = []
                        location = 0
                        #gets the location of the letter
                        location = finalWord.index(gameinput)
                        #saves the index of the letter
                        indexes.append(finalWord.index(gameinput))
                        #removes the letters from the refrence and replaces with ~
                        finalWord.pop(location)
                        finalWord.insert(location, "~")
                        for y in indexes:
                            #uses the found indexes previously to change underword
                            underWord.pop(y)
                            underWord.insert(y,gameinput)
                #user entered an incorrect input            
                else:
                    #takes away one life
                    lives += -1
                    print("Incorrect")
                    t.sleep(1)
                    c()
                    #changes current image stage
                    for x in range(2):   
                        stages[stage].pop()
                        stages[stage].append(parts[stage])
                        stage += 1
                        if stage == 13:
                            break
                #adds the incorrect letter to the used letter list
                usedLetters += gameinput
        #if the user enterd a number:
        else:
            print("Please enter a letter")
            t.sleep(1)
            c()

        #runs loop until a character doesnt equal ~
        for g in finalWord:
            if g != "~":
                win = False
                break
            else:
                win = True
            
                
    
        #if win is true, prints a win message and animation
        if win == True:
            print("You win")
            t.sleep(1)
            loseGame("You Win")
        #if lives is 0, prints a lose message and animation
        if lives == 0:
            loseGame("You Lose")
            t.sleep(1)
            lose = True
        c()                            


#defines main menu
def mainMenu():
    #infinate loop until broken
    while True:
        #friendly gui
        print("""====================================================
|           HandMan             |       _          |
|                               |      /(|         |
|        1. Play                |     (  :         |
|        2. Help me pls         |    __\  \  _____ |
|        3. Don't play          |  (____)  `|      |
|                               | (____)|   |      |
|                               |  (____).__|      |
|                               |   (___)__.|_____ |
|                               |                  |
====================================================""")
        #gets lower case user input
        menuInput = input(" > ").lower()
        #checks the user input
        if menuInput == "play" or menuInput == "1":
            #clears the screen
            c()
            #main function
            playGame()
        elif ("help me" or "help") in menuInput or menuInput == "2":
            c()
            helpGame()
        elif menuInput =="don't play" or menuInput == "3":
            #breaks from loop which ends the program
            break
        else:
            #tells the user their input was invalid
            print("invalid input")
            t.sleep(1)
            c()

#checks if the program was run from a module or not
if __name__ == "__main__":
    #runs the menu function
    mainMenu()


#message for those running from a module
else:
    print("If your seeing this message")
    t.sleep(1)
    print("""
     )                                          (          
  ( /(               (                )         )\ )    )  
  )\())       (    ( )\  (         ( /(    (   (()/( ( /(  
 ((_)\  (    ))\   )((_) )(    (   )\())  ))\   /(_)))\()) 
__ ((_) )\  /((_) ((_)_ (()\   )\ ((_)\  /((_) (_)) (_))/  
\ \ / /((_)(_))(   | _ ) ((_) ((_)| |(_)(_))   |_ _|| |_   
 \ V // _ \| || |  | _ \| '_|/ _ \| / / / -_)   | | |  _|  
  |_| \___/ \_,_|  |___/|_|  \___/|_\_\ \___|  |___| \__| 
""")
    t.sleep(60)

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).