Advertisement
Guest User

press.lua

a guest
Aug 24th, 2014
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. --[[
  2.     Some stupid game thing by Mitchfizz05.
  3.     Code under the GNU General Public License v3.
  4. ]]
  5.  
  6. -- Constants.
  7. local version = 1.0
  8. local width,height = term.getSize()
  9.  
  10. -- Current game variables.
  11. local score = 0
  12. local rounds = 9
  13. local key
  14. local countdown = 10
  15.  
  16. -- Index of keys.
  17. local keys = {"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"}
  18.  
  19. -- Render main menu.
  20. local function mainMenu()
  21.     term.setBackgroundColor(colors.white)
  22.     term.setTextColor(colors.black)
  23.     term.clear()
  24.     term.setCursorPos(1,1)
  25.    
  26.     term.setBackgroundColor(colors.blue)
  27.     local bar = string.rep(" ", width)
  28.     term.write(bar)
  29.     term.setCursorPos(1,1)
  30.     term.setTextColor(colors.white)
  31.     term.write("Button Press Game Thing")
  32.    
  33.     term.setBackgroundColor(colors.white)
  34.     term.setTextColor(colors.black)
  35.     term.setCursorPos(1,3)
  36.     print("Welcome to Button Press Game Thing. By Mitchfizz05.")
  37.     print("")
  38.     print(" Press enter key to start!")
  39. end
  40.  
  41. -- Render countdown.
  42. local function countdownScreen()
  43.     term.setBackgroundColor(colors.white)
  44.     term.setTextColor(colors.black)
  45.     term.clear()
  46.     term.setCursorPos(1,1)
  47.    
  48.     term.setBackgroundColor(colors.blue)
  49.     local bar = string.rep(" ", width)
  50.     term.write(bar)
  51.     term.setCursorPos(1,1)
  52.     term.setTextColor(colors.white)
  53.     term.write("Button Press Game Thing")
  54.    
  55.     term.setBackgroundColor(colors.white)
  56.     term.setTextColor(colors.black)
  57.     term.setCursorPos(1,3)
  58.     term.write("Starting in " .. countdown .. " seconds...")
  59. end
  60.  
  61. -- Render game screen.
  62. local function gameScreen()
  63.     term.setBackgroundColor(colors.white)
  64.     term.setTextColor(colors.black)
  65.     term.clear()
  66.     term.setCursorPos(1,1)
  67.    
  68.     term.setBackgroundColor(colors.blue)
  69.     local bar = string.rep(" ", width)
  70.     term.write(bar)
  71.     term.setCursorPos(1,1)
  72.     term.setTextColor(colors.white)
  73.     term.write("Button Press Game Thing")
  74.    
  75.     term.setBackgroundColor(colors.white)
  76.     term.setTextColor(colors.black)
  77.     term.setCursorPos(1,3)
  78.     term.write("Quick! Press... ")
  79.     term.setTextColor(colors.red)
  80.     term.write(key)
  81.    
  82.     term.setTextColor(colors.purple)
  83.     term.setCursorPos(width - #"score: " - #tostring(score) - 1, 3)
  84.     term.write("Score: " .. score)
  85. end
  86.  
  87. -- Render time-up screen.
  88. local function timeupScreen()
  89.     term.setBackgroundColor(colors.white)
  90.     term.setTextColor(colors.black)
  91.     term.clear()
  92.     term.setCursorPos(1,1)
  93.    
  94.     term.setBackgroundColor(colors.blue)
  95.     local bar = string.rep(" ", width)
  96.     term.write(bar)
  97.     term.setCursorPos(1,1)
  98.     term.setTextColor(colors.white)
  99.     term.write("Button Press Game Thing")
  100.    
  101.     term.setBackgroundColor(colors.white)
  102.     term.setTextColor(colors.black)
  103.     term.setCursorPos(1,3)
  104.     term.write("Time up!")
  105.     term.setCursorPos(3,5)
  106.     term.setTextColor(colors.green)
  107.     term.write("Score: ")
  108.     term.setTextColor(colors.gray)
  109.     term.write(math.floor(score))
  110.    
  111.     sleep(2)
  112.     term.setCursorPos(1,7)
  113.     term.setTextColor(colors.lightGray)
  114.     term.write("Press any key to exit...")
  115. end
  116.  
  117. -- Show menu.
  118. mainMenu()
  119. while true do
  120.     local e,p1,p2,p3 = os.pullEvent()
  121.     if e == "key" and p1 == 28 then
  122.         break
  123.     end
  124. end
  125.  
  126. -- Run countdown.
  127. while (countdown > 0) do
  128.     -- Trololol.
  129.     if countdown > 6 and not countdown == 10 then
  130.         if math.random(1,20) == 1 then
  131.             -- Hehe.
  132.             countdown = 1
  133.         end
  134.     end
  135.    
  136.     countdownScreen()
  137.     sleep(1)
  138.     countdown = countdown - 1
  139. end
  140.  
  141. -- Main loop.
  142. while (rounds > 0) do
  143.     -- Pick a random key.
  144.     key = keys[math.random(1, #keys)]
  145.    
  146.     -- Print message.
  147.     gameScreen()
  148.    
  149.     os.startTimer( math.random(1,20)/10 )
  150.     while true do
  151.         local e,p1,p2,p3 = os.pullEvent()
  152.         if e == "char" and p1 == key then
  153.             score = score + 2
  154.             gameScreen()
  155.         elseif e == "char" then
  156.             score = score - 1
  157.             gameScreen()
  158.         elseif e == "timer" then
  159.             break
  160.         end
  161.     end
  162.    
  163.     rounds = rounds - 1
  164. end
  165.  
  166. -- Game finish.
  167. timeupScreen()
  168.  
  169. -- Wait for exit.
  170. os.pullEvent("key")
  171.  
  172. -- Cleanup for exit.
  173. term.setBackgroundColor(colors.black)
  174. term.setTextColor(colors.white)
  175. term.clear()
  176. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement