Smiley43210

[Mackan90096] Gem Program

Apr 10th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. -- Change absolutely whatever you need to, even if it says you shouldn't
  2.  
  3. ------------ General Settings ------------
  4.  
  5. ------ Required Internal ------
  6. -- Changing anything in this section isn't recommended
  7. local width, height = term.getSize() -- Must be defined before Gem Settings
  8.  
  9. ------ Gem Settings ------
  10. -- The number of gems to have on the screen at all times
  11. local gemsOnScreen = 10
  12. -- The minimum x coordinate that a gem can be generated at
  13. local minX = 13
  14. -- The minimum y coordinate that a gem can be generated at
  15. local minY = 4
  16. -- The maximum x coordinate that a gem can be generated at
  17. local maxX = 47
  18. -- The maximum y coordinate that a gem can be generated at
  19. local maxY = height - 1
  20. -- List of possible gem colors
  21. local colorChoices = { colors.purple, colors.yellow, colors.lime, colors.lightBlue }
  22. -- Define the gem worth value (money received when clicked)
  23. local gemValue = { }
  24. gemValue[colors.purple] = 10 -- Get 10 money when you click a purple gem
  25. gemValue[colors.yellow] = 5 -- Get 5 money when you click a pink gem
  26. gemValue[colors.lime] = 15 -- Get 15 money when you click a lime gem
  27. gemValue[colors.lightBlue] = 20 -- Get 20 money when you click a blue gem
  28.  
  29. ------ Screen Settings ------
  30. -- This allows you to edit the colors without having to scrounge the code for each occurrence of a color
  31. -- Background color
  32. local bgColor = colors.blue
  33. -- Text color
  34. local txtColor = colors.white
  35.  
  36. ------------ Internals ------------
  37.  
  38. ------ User Data ------
  39. local money = 0 -- The player's money
  40. local gemsClicked = 0 -- The number of gems the player has collected
  41. local multi = 1 -- The current multiplier
  42. local multiCost = 100 -- The cost of upgrading the multiplier
  43.  
  44. ------ Game Data ------
  45. -- Table that stores the gem data (coordinates and color)
  46. local gems = { }
  47. -- Stores all buttons in the game
  48. local btns = {} -- Unused for now
  49.  
  50. ------------ Functions ------------
  51.  
  52. function newGem()
  53.     local w, h = term.getSize()
  54.     local x, y
  55.  
  56.     -- Pick a random spot for the pixel to appear
  57.     while true do
  58.         x = math.random(minX, maxX) -- Random number for the x coordinate
  59.         y = math.random(minY, maxY) -- Random number for the y coordinate
  60.         if #gems > 0 then
  61.             local good = true
  62.             for i, v in ipairs(gems) do
  63.                 -- Check to see if a gem already exists in that position
  64.                 if v[1] == x and v[2] == y then good = false end
  65.             end
  66.             if good then break end
  67.         else
  68.             break
  69.         end
  70.     end
  71.  
  72.     -- Now, we choose a random color for the pixel (maybe you don't need this)
  73.     local color = colorChoices[math.random(1, #colorChoices)]
  74.  
  75.     -- This holds the coordinates and the color of the gem
  76.     local gem = {x, y, color}
  77.     table.insert(gems, gem) -- Adds the gem data to the main table
  78.  
  79.     -- Draw the gem
  80.     term.setCursorPos(x, y)
  81.     term.setBackgroundColor(color)
  82.     write(" ")
  83.     term.setBackgroundColor(bgColor)
  84. end
  85.  
  86. function spawnGems(n)
  87.     for i = 1, n do
  88.         newGem()
  89.     end
  90. end
  91.  
  92. function buyMulti()
  93.     money = money - multiCost
  94.     multiCost = multiCost * 2
  95.     multi = multi + 1
  96.     stats()
  97. end
  98.  
  99. function stats()
  100.     term.setCursorPos(1, 1)
  101.     local left = 13
  102.     left = 13 - #"Money: "..money
  103.     print("Money: "..money..string.rep(" ", left)) -- So that when you buy stuff and your money goes down, digits don't remain. Example: If it displayed 2000 and you bought something that costed 1200, it would display 8000 (the last 0 wouldn't be cleared).
  104.     print("Gems: "..gemsClicked)
  105.     print("Multiplier: ")
  106.     print(multi.."x")
  107.     print("Upgrades:")
  108.     print("Multiplier")
  109.     print("Cost: "..multiCost)
  110.     --[[if money >= multiCost then
  111.         -- Color the button as 'disabled'
  112.         term.setTextColor(colors.white)
  113.         term.setBackgroundColor(colors.black)
  114.     else
  115.         -- Color the button as 'enabled'
  116.         term.setTextColor(colors.lightGray)
  117.         term.setBackgroundColor(colors.gray)
  118.     end
  119.     write("Upgrd Multi")
  120.     term.setTextColor(colors.white)
  121.     term.setBackgroundColor(colors.red)]]--
  122. end
  123.  
  124. ------------ Running Code ------------
  125.  
  126. -- Don't use a number for the color unless absolutely nessecary...and I have no idea when it would be
  127. -- Use the colors API. 16384 is the same as saying colors.red
  128. term.setBackgroundColor(bgColor)
  129. term.setTextColor(txtColor)
  130. term.clear()
  131. spawnGems(gemsOnScreen)
  132. stats()
  133.  
  134. while true do
  135.     local e, p1, p2, p3 = os.pullEvent("mouse_click")
  136.     if  p2 >= 1 and p2 <= 11 and p3 == 7 then
  137.         if money >= multiCost then -- We use >= because the user may have more money than the cost
  138.             buyMulti()
  139.         end
  140.     end
  141.     -- Check if the coordinates clicked are any of the pixel's coordinates
  142.     for i, v in ipairs(gems) do
  143.         if p1 == 1 and v[1] == p2 and v[2] == p3 then -- A gem was LEFT clicked
  144.             -- Do other stuff here
  145.             -- Increment variables
  146.             gemsClicked = gemsClicked + 1
  147.  
  148.             -- Give money
  149.             money = money + (gemValue[v[3]] * multi) -- Gem value multiplied by the multiplier
  150.  
  151.             -- Remove the gem
  152.             term.setBackgroundColor(bgColor)
  153.             term.setCursorPos(v[1], v[2])
  154.             write(" ")
  155.             table.remove(gems, i)
  156.  
  157.             -- Spawn a new gem
  158.             newGem()
  159.  
  160.             -- Update stats
  161.             stats()
  162.         end
  163.     end
  164.     sleep(0.05)
  165. end
Advertisement
Add Comment
Please, Sign In to add comment