Derek1017

Guess My Number

Jul 9th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.07 KB | None | 0 0
  1. -- Set up some variables
  2.  
  3. local myNumber = math.random(1, 100)
  4. local tries = 0
  5. local maxTries = 5
  6.  
  7. -- Display the intro
  8.  
  9. term.clear()
  10. print("I'm thinking of a number between 1 and 100.")
  11. print("You have 5 tries to guess it!")
  12.  
  13. -- The main game loop
  14.  
  15. while true do
  16.  
  17.   -- Read the next guess from the user
  18.  
  19.   tries = tries + 1
  20.   print("\nTry #" .. tries .. ". What's your guess?")
  21.   local guess = tonumber(read())
  22.  
  23.   -- Compare the guess to my number
  24.  
  25.   if guess < myNumber then
  26.     print("Too low!")
  27.   elseif guess > myNumber then
  28.     print("Too high!")
  29.   else
  30.     print("\nYou got it!")
  31.     shootFireworks(5)
  32.     break
  33.   end  
  34.  
  35.   -- Check if the user has used up their tries
  36.  
  37.   if tries == maxTries then
  38.     print("\nBad luck, you ran out of tries!")
  39.     break
  40.   else
  41.     print("Try again...")
  42.   end
  43.  
  44. end
  45.  
  46.  
  47. -- Shoot some fireworks!
  48.  
  49. function shootFireworks(numFireworks)
  50.  
  51.   sleep(1)
  52.  
  53.   for i=1,numFireworks do
  54.     redstone.setOutput("left", true)
  55.     sleep(.2)
  56.     redstone.setOutput("left", false)
  57.     sleep(.2)
  58.   end
  59.  
  60. end
Add Comment
Please, Sign In to add comment