Advertisement
HyperQ

Hangman - ComputerCraft

Sep 22nd, 2012
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.43 KB | None | 0 0
  1. local letters = {"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"}
  2. local words5 = {"About","Above","After","Again","Apple","Began","Begin","Black","Block","Bring","Brook","Brown","Build","Carry","Chair","Color","Cloud","Dance","Drink","Empty","Found","Funny","Green","Guess","Happy","Hello","Horse","House","Hurry","Large","Laugh","Learn","Maybe","Never","Paint","Penny","Store","Thank","Their","Train","Truck","Under","Where","Which","White","Would"}
  3. local words6 = {"Across","Afraid","Always","Animal","Answer","Anyone","Around","Basket","Before","Behind","Belong","Beside","Better","Bitter","Bottle","Bottom","Bought","Bounce","Branch","Bridge","Button","Candle","Cannot","Caught","Cellar","Chance","Cowboy","Doctor","Dragon","Father","Friend","Kitten","Little","Mother","Peanut","Please","Pocket","Rabbit","Rocket","School","Should","Sister","Street","Turtle","Window","Yellow"}
  4. local words7 = {"Address","Against","Already","Another","Anxious","Balcony","Balloon","Battery","Because","Bedroom","Believe","Beneath","Between","Blossom","Bicycle","Blanket","Brother","Careful","Chicken","Country","Cupcake","Evening","Excited","Feather","Herself","Himself","Instead","Kitchen","Morning","Nothing","Outside","Picture","Raccoon","Snowman","Someone","Stopped","Thought","Through","Tonight","Tractor","Traffic","Trouble","Unhappy","Whisper","Whistle","Without"}
  5. local words8 = {"Accident","Airplane","Although","Anything","Anywhere","Backward","Backyard","Barnyard","BaseBall","Bithday","Business","Cheerful","Complain","Complete","Continue","Customer","Darkness","Daughter","Daydream","Discover","Distance","Doorbell","Electric","Election","Elephant","Elevator","Enormous","Everyone","Favorite","Football","Fountain","Friendly","Frighten","Goodness","Grateful","Handsome","Lemonade","Mountain","Neighbor","Remember","Sidewalk","Sometime","Squirrel","Suprise","Together","Tommorow","Yourself"}
  6. local health,currentletter = 7,1
  7. local level = 1
  8. function printc(str, xpos, ypos)
  9.     term.setCursorPos(xpos,ypos)
  10.     write(str)
  11. end
  12.  
  13. function winner()
  14.     local w,h = term.getSize()
  15.     level = level + 1
  16.     mc= false
  17.     printc("Congratulations Level up!",w/2-(string.len("Congratulations Level up!")/2)+1,17)
  18.     sleep(3)
  19.     play()
  20. end
  21.  
  22. local function findAll(s, pattern, plain)
  23.   local i = 1
  24.   local result = {}
  25.   while i < #s do
  26.     local pos = string.find(s, pattern, i, plain)
  27.     if not pos then
  28.           break
  29.     end
  30.     table.insert(result, pos)
  31.     i = pos + 1
  32.   end
  33.   return result
  34. end
  35.  
  36. function loser()
  37.     mc= false
  38.     printc("|                                              |",2,16)
  39.     local w,h = term.getSize()
  40.     local printit = "Your Word was: "..word
  41.     printc("You Lose!",w/2-(string.len("You Lose!")/2)+1,15)
  42.     printc(printit,w/2-(#printit/2)+1,16)
  43.     sleep(2)
  44.     menu()
  45. end
  46.  
  47. function drawframe()
  48.     term.clear()
  49.     printc("+----------------------------------------------+",2,1)
  50.     printc("| Used Letters:                                |",2,2)
  51.     printc("+----------------------------------+-----------+",2,3)
  52.     printc("|       ______________             | Level:    |",2,4)
  53.     printc("|       |/           |             +-----------+",2,5)
  54.     printc("|       |                                      |",2,6)
  55.     printc("|       |                                      |",2,7)
  56.     printc("|       |                                      |",2,8)
  57.     printc("|       |                                      |",2,9)
  58.     printc("|       |                                      |",2,10)
  59.     printc("|       |                                      |",2,11)
  60.     printc("|       |                                      |",2,12)
  61.     printc("|       |                                      |",2,13)
  62.     printc("|     __|__                                    |",2,14)
  63.     printc("|                                              |",2,15)
  64.     printc("|     -   -                                    |",2,16)
  65.     printc("|                                              |",2,17)
  66.     printc("+----------------------------------------------+",2,18)
  67. end
  68.  
  69. function chooseword()
  70.     if level <= 5 then words = words5 end
  71.     if level >= 6 and level <= 10  then words = words6 end
  72.     if level >= 11 and level <= 15  then words = words7 end
  73.     if level >= 16 and level <= 20  then words = words8 end
  74.     blank = {}
  75.     word = words[math.random(1,#words)]
  76.     for i=1,#word do table.insert(blank,"_")
  77.         term.setCursorPos(22,16)
  78.         for i=1,#blank do write(blank[i].." ") end
  79.     end
  80. end
  81.  
  82. function losehealth()
  83.     health = health - 1
  84.     if health == 6 then printc("/ \\",22,6);printc("\\ /",22,7) end --Head
  85.     if health == 5 then printc("|",23,8);printc("|",23,9);printc("|",23,10) end --Body
  86.     if health == 4 then printc("\\",21,8);printc("\\",22,9) end --Left Arm
  87.     if health == 3 then printc("/",25,8);printc("/",24,9) end --Right Arm
  88.     if health == 2 then printc("/",22,11);printc("_/",20,12) end --Left Leg
  89.     if health == 1 then printc("\\",24,11);printc("\\_",25,12);loser() end --Right Leg
  90. end
  91.  
  92. function play()
  93.     health = 7
  94.     usedletters = {}
  95.     drawframe()
  96.     chooseword()
  97.     printc(letters[currentletter],10,16)
  98.     mc=true
  99.     printc(level,47,4)
  100. end
  101.  
  102. function checkletter(number)
  103.     local varr,varr2 = true,true
  104.     for i=1,#usedletters do
  105.         if tonumber(usedletters[i]) == currentletter then varr = false;break end
  106.     end
  107.     if varr then
  108.         table.insert(usedletters, currentletter)
  109.         term.setCursorPos(18,2)
  110.         for i=1,#usedletters do write(letters[tonumber(usedletters[i])].." ") end
  111.         for _,i in ipairs(findAll(string.lower(word), string.lower(letters[number]))) do
  112.             if i ~= 1 then blank[i] = string.lower(letters[number]) else blank[i] = letters[number] end
  113.             term.setCursorPos(22,16)
  114.             for i=1,#blank do write(blank[i].." ") end
  115.         end
  116.         for i=1,#blank do -- checks for a win or not, if no under scores exist in #blank then you win
  117.             if blank[i] == "_" then break end
  118.             if i == #blank and #blank ~= "_"  then winner() end
  119.         end
  120.         if string.find(string.lower(word),string.lower(letters[number])) == nil then losehealth() end
  121.     end
  122. end
  123.  
  124. function menu()
  125.     term.clear()   
  126.     local w,h = term.getSize()
  127.     printc("_  _ ____ _  _ ____ _  _ ____ _  _",w/2-(string.len("_  _ ____ _  _ ____ _  _ ____ _  _")/2)+1,4)
  128.     printc("|__| |__| |\\ | | __ |\\/| |__| |\\ |",w/2-(string.len("|__| |__| |\\ | | __ |\\/| |__| |\\ |")/2)+1,5)
  129.     printc("|  | |  | | \\| |__] |  | |  | | \\|",w/2-(string.len("|  | |  | | \\| |__] |  | |  | | \\|")/2)+1,6)
  130.     printc(" Press Enter To Play ",w/2-(string.len(" Press Enter To Play ")/2)+1,12)
  131.     local fvar = true
  132.     os.startTimer(1)
  133.     local mc2 = true
  134.     while mc2 do
  135.         local event, param = os.pullEventRaw()
  136.         if event == "timer" then
  137.             if fvar == true then
  138.                 printc("[Press Enter To Play]",w/2-(string.len("[Press Enter To Play]")/2)+1,12)
  139.                 os.startTimer(1)
  140.                 fvar = false
  141.             else
  142.                 printc(" Press Enter To Play ",w/2-(string.len(" Press Enter To Play ")/2)+1,12)
  143.                 os.startTimer(1)
  144.                 fvar = true
  145.             end
  146.         elseif event == "key" and param == 28 then mc2 = false;level = 1;play() end
  147.     end    
  148. end                      
  149.  
  150. menu()
  151.  
  152. while mc == true do
  153.     local event,param = os.pullEventRaw()
  154.     if event == "key" then
  155.         if param == 208 then -- down
  156.             currentletter = currentletter +1
  157.             if currentletter == 27 then currentletter = 1 end
  158.             printc(letters[currentletter],10,16)
  159.         elseif param == 200 then -- up
  160.             currentletter = currentletter -1
  161.             if currentletter == 0 then currentletter = 26 end
  162.             printc(letters[currentletter],10,16)
  163.         elseif param == 28 then -- enter
  164.             checkletter(currentletter)
  165.         end
  166.     end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement