Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Example gem-miner by 1lann
- -- Free to modify, use and whatever without my permission or credit!
- if not term.isColor() then
- print("Not available for use on normal computers")
- end
- -- Just a quick heads-up
- print("Mine all of the yellow gems!")
- sleep(3)
- -- Declare variables
- local score = 0
- local yellows = {}
- local numYellows = 0
- -- Configurable
- local possibilities = {colors.orange, colors.lightBlue, colors.lime, colors.yellow}
- local function generateBoard()
- term.clear()
- term.setCursorBlink(false)
- local width, height = term.getSize()
- -- Loop through every pixel on the screen except for top row
- -- Note the variable names I used for the current pixel
- for y = 2, height do
- for x = 1, width do
- -- Generate random color
- local curColor = possibilities[math.random(1,#possibilities)]
- -- If the color is yellow then...
- if curColor == colors.yellow then
- -- Set an index of the yellows table to contain the coordinates of the pixel
- -- I used the index to contain the data to remove the need of a loop to check
- -- and for effeciency purposes.
- yellows[tostring(x) .. "," .. tostring(y)] = true
- -- Register a yellow gem
- numYellows = numYellows+1
- end
- -- Set the cursor position
- term.setCursorPos(x, y)
- -- Set the color
- term.setBackgroundColor(curColor)
- -- Using term.write as it is more effecient and is faster
- term.write(" ")
- end
- end
- end
- -- We'll just get on and generate the board
- generateBoard()
- local function printScore(score)
- -- Set color and positioning
- term.setCursorPos(37,1)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- -- Print out score over the number of yellow gems
- term.write("Score: " .. tostring(score).."/"..tostring(numYellows))
- -- If the user has gotten all of the gems then signify to quit!
- if score >= numYellows then return true end
- end
- local function mainGame()
- -- Clear out top bar for stats
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- -- Print close button and make it noticeable.
- term.setTextColor(colors.red)
- term.write("[X] ")
- -- Now print the program name
- term.setTextColor(colors.white)
- term.write("Gem-miner example program!")
- -- Print out 0 score
- printScore(0)
- -- Main game loop
- while true do
- -- Only get mouse_click events and the x and y events
- -- I'm going to ignore which button was pressed in this example
- local e, _, x, y = os.pullEvent()
- if e == "mouse_click" then
- -- Checking if the pixel clicked was yellow...
- if yellows[tostring(x)..","..tostring(y)] then
- -- If so then increment the score!
- score=score+1
- -- Print it out!
- if printScore(score) then
- -- If printScore returns true then,
- -- the user has collected all of the gems!
- -- Break out of the loop to finish the game
- break
- end
- -- Clear the pixel/gem
- term.setBackgroundColor(colors.black)
- term.setCursorPos(x,y)
- term.write(" ")
- -- Delete the gem from the database
- yellows[tostring(x)..","..tostring(y)] = nil
- -- Check if the close button was pressed
- -- Round brackets are used to prevent confusion
- -- Further Explaination: I check if the row clicked was the first
- -- (Meaning the top bar) and the limits were within the first 3 characters
- -- (Since [X] is 3 characters long)
- elseif (y == 1) and (x >= 1) and (x <= 3) then
- -- Break the loop to signify ending of game
- break
- end
- end
- end
- -- Loop has quit, meaning user ended the game
- -- Print out ending stuffs.
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.yellow)
- term.setCursorPos(1,1)
- -- If the user has won then, congrats!
- if score >= numYellows then
- print("Congratulations, you win!")
- end
- print("Thanks for trying out gem-miner")
- -- Just incase, shouldn't be necessary
- return
- end
- -- We'll just run the mainGame
- mainGame()
- -- Just incase, shouldn't be necessary
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement