Advertisement
1lann

Gem-miner example with Comments

May 8th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. -- Example gem-miner by 1lann
  2. -- Free to modify, use and whatever without my permission or credit!
  3.  
  4. if not term.isColor() then
  5.     print("Not available for use on normal computers")
  6. end
  7. -- Just a quick heads-up
  8. print("Mine all of the yellow gems!")
  9. sleep(3)
  10.  
  11. -- Declare variables
  12. local score = 0
  13. local yellows = {}
  14. local numYellows = 0
  15. -- Configurable
  16. local possibilities = {colors.orange, colors.lightBlue, colors.lime, colors.yellow}
  17.  
  18. local function generateBoard()
  19.     term.clear()
  20.     term.setCursorBlink(false)
  21.     local width, height = term.getSize()
  22.     -- Loop through every pixel on the screen except for top row
  23.     -- Note the variable names I used for the current pixel
  24.     for y = 2, height do
  25.         for x = 1, width do
  26.             -- Generate random color
  27.             local curColor = possibilities[math.random(1,#possibilities)]
  28.             -- If the color is yellow then...
  29.             if curColor == colors.yellow then
  30.                 -- Set an index of the yellows table to contain the coordinates of the pixel
  31.                 -- I used the index to contain the data to remove the need of a loop to check
  32.                 -- and for effeciency purposes.
  33.                 yellows[tostring(x) .. "," .. tostring(y)] = true
  34.                 -- Register a yellow gem
  35.                 numYellows = numYellows+1
  36.             end
  37.  
  38.             -- Set the cursor position
  39.             term.setCursorPos(x, y)
  40.             -- Set the color
  41.             term.setBackgroundColor(curColor)
  42.             -- Using term.write as it is more effecient and is faster
  43.             term.write(" ")
  44.         end
  45.     end
  46. end
  47.  
  48. -- We'll just get on and generate the board
  49. generateBoard()
  50.  
  51. local function printScore(score)
  52.     -- Set color and positioning
  53.     term.setCursorPos(37,1)
  54.     term.setBackgroundColor(colors.gray)
  55.     term.setTextColor(colors.white)
  56.     -- Print out score over the number of yellow gems
  57.     term.write("Score: " .. tostring(score).."/"..tostring(numYellows))
  58.     -- If the user has gotten all of the gems then signify to quit!
  59.     if score >= numYellows then return true end
  60. end
  61.  
  62. local function mainGame()
  63.     -- Clear out top bar for stats
  64.     term.setCursorPos(1,1)
  65.     term.setBackgroundColor(colors.gray)
  66.    
  67.     term.clearLine()
  68.     -- Print close button and make it noticeable.
  69.     term.setTextColor(colors.red)
  70.     term.write("[X] ")
  71.     -- Now print the program name
  72.     term.setTextColor(colors.white)
  73.     term.write("Gem-miner example program!")
  74.     -- Print out 0 score
  75.     printScore(0)
  76.     -- Main game loop
  77.     while true do
  78.         -- Only get mouse_click events and the x and y events
  79.         -- I'm going to ignore which button was pressed in this example
  80.         local e, _, x, y = os.pullEvent()
  81.         if e == "mouse_click" then
  82.             -- Checking if the pixel clicked was yellow...
  83.             if yellows[tostring(x)..","..tostring(y)] then
  84.                 -- If so then increment the score!
  85.                 score=score+1
  86.                 -- Print it out!
  87.                 if printScore(score) then
  88.                     -- If printScore returns true then,
  89.                     -- the user has collected all of the gems!
  90.                     -- Break out of the loop to finish the game
  91.                     break
  92.                 end
  93.                 -- Clear the pixel/gem
  94.                 term.setBackgroundColor(colors.black)
  95.                 term.setCursorPos(x,y)
  96.                 term.write(" ")
  97.                 -- Delete the gem from the database
  98.                 yellows[tostring(x)..","..tostring(y)] = nil
  99.             -- Check if the close button was pressed
  100.             -- Round brackets are used to prevent confusion
  101.             -- Further Explaination: I check if the row clicked was the first
  102.             -- (Meaning the top bar) and the limits were within the first 3 characters
  103.             -- (Since [X] is 3 characters long)
  104.             elseif (y == 1) and (x >= 1) and (x <= 3) then
  105.                 -- Break the loop to signify ending of game
  106.                 break
  107.             end
  108.         end
  109.     end
  110.     -- Loop has quit, meaning user ended the game
  111.     -- Print out ending stuffs.
  112.     term.setBackgroundColor(colors.black)
  113.     term.clear()
  114.     term.setTextColor(colors.yellow)
  115.     term.setCursorPos(1,1)
  116.     -- If the user has won then, congrats!
  117.     if score >= numYellows then
  118.         print("Congratulations, you win!")
  119.     end
  120.     print("Thanks for trying out gem-miner")
  121.     -- Just incase, shouldn't be necessary
  122.     return
  123. end
  124.  
  125. -- We'll just run the mainGame
  126. mainGame()
  127. -- Just incase, shouldn't be necessary
  128. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement