Advertisement
1lann

Gem-miner example, no comments

May 8th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. -- Example gem-miner by 1lann
  2.  
  3. if not term.isColor() then
  4.     print("Not available for use on normal computers")
  5. end
  6. print("Mine all of the yellow gems!")
  7. sleep(3)
  8. local score = 0
  9. local yellows = {}
  10. local numYellows = 0
  11. local possibilities = {colors.orange, colors.lightBlue, colors.lime, colors.yellow}
  12.  
  13. local function generateBoard()
  14.     term.clear()
  15.     term.setCursorBlink(false)
  16.     local width, height = term.getSize()
  17.     for y = 2, height do
  18.         for x = 1, width do
  19.             local curColor = possibilities[math.random(1,#possibilities)]
  20.             if curColor == colors.yellow then
  21.                 yellows[tostring(x) .. "," .. tostring(y)] = true
  22.                 numYellows = numYellows+1
  23.             end
  24.             term.setCursorPos(x, y)
  25.             term.setBackgroundColor(curColor)
  26.             term.write(" ")
  27.         end
  28.     end
  29. end
  30. generateBoard()
  31.  
  32. local function printScore(score)
  33.     term.setCursorPos(37,1)
  34.     term.setBackgroundColor(colors.gray)
  35.     term.setTextColor(colors.white)
  36.     term.write("Score: " .. tostring(score).."/"..tostring(numYellows))
  37.     if score >= numYellows then return true end
  38. end
  39.  
  40. local function mainGame()
  41.     term.setCursorPos(1,1)
  42.     term.setBackgroundColor(colors.gray)
  43.    
  44.     term.clearLine()
  45.     term.setTextColor(colors.red)
  46.     term.write("[X] ")
  47.     term.setTextColor(colors.white)
  48.     term.write("Gem-miner example program!")
  49.     printScore(0)
  50.     while true do
  51.         local e, _, x, y = os.pullEvent()
  52.         if e == "mouse_click" then
  53.             if yellows[tostring(x)..","..tostring(y)] then
  54.                 score=score+1
  55.                 if printScore(score) then
  56.                     break
  57.                 end
  58.                 term.setBackgroundColor(colors.black)
  59.                 term.setCursorPos(x,y)
  60.                 term.write(" ")
  61.                 yellows[tostring(x)..","..tostring(y)] = nil
  62.             elseif (y == 1) and (x >= 1) and (x <= 3) then
  63.                 break
  64.             end
  65.         end
  66.     end
  67.     term.setBackgroundColor(colors.black)
  68.     term.clear()
  69.     term.setTextColor(colors.yellow)
  70.     term.setCursorPos(1,1)
  71.     if score >= numYellows then
  72.         print("Congratulations, you win!")
  73.     end
  74.     print("Thanks for trying out gem-miner")
  75.     return
  76. end
  77. mainGame()
  78. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement