Advertisement
Mackan90096

Gem Miner

Apr 12th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 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. -- Background color
  31. local bgColor = colors.black
  32. -- Text color
  33. local txtColor = colors.white
  34.  
  35. ------------ Internals ------------
  36.  
  37. ------ User Data ------
  38. local money = 0
  39. local gemsClicked = 0
  40. local multi = 1
  41. local multiCost = 100 -- add new variable for multiCost
  42.  
  43. ------ Gem Data ------
  44. -- Table that stores the pixel data (coordinates and color)
  45. local gems = { }
  46.  
  47. ------------ Functions ------------
  48. function frame()
  49.      bg = paintutils.loadImage("/.game1")
  50.     paintutils.drawImage(bg, 1,1)
  51. end
  52.  
  53.  
  54. local function buymultiply() -- make a function out of it so you can use it where you need it  
  55. if money >= multiCost then  -- should be > else you only can buy it when you have exactly this amount
  56. money = money - multiCost
  57. multiCost = multiCost*2
  58.  multi = multi+1
  59.  stats()
  60. end
  61. end
  62.  
  63. function newGem()
  64.     local w, h = term.getSize()
  65.     local x, y
  66.  
  67.     -- Pick a random spot for the pixel to appear
  68.     while true do
  69.         x = math.random(minX, maxX) -- Random number for the x coordinate
  70.         y = math.random(minY, maxY) -- Random number for the y coordinate
  71.         if #gems > 0 then
  72.             local good = true
  73.             for i, v in ipairs(gems) do
  74.                 -- Check to see if a gem already exists in that position
  75.                 if v[1] == x and v[2] == y then good = false end
  76.             end
  77.             if good then break end
  78.         else
  79.             break
  80.         end
  81.     end
  82.  
  83.     -- Now, we choose a random color for the pixel (maybe you don't need this)
  84.     local color = colorChoices[math.random(1, #colorChoices)]
  85.  
  86.     -- This holds the coordinates and the color of the gem
  87.     local gem = {x, y, color}
  88.     table.insert(gems, gem) -- Adds the gem data to the main table
  89.  
  90.     -- Draw the gem
  91.     term.setCursorPos(x, y)
  92.     term.setBackgroundColor(color)
  93.     write(" ")
  94.     term.setBackgroundColor(bgColor)
  95. end
  96.  
  97. function spawnGems(n)
  98.     for i = 1, n do
  99.         newGem()
  100.     end
  101. end
  102.  
  103. local function mpupgrade()
  104. local buytext = "Add Multiplier:"
  105. local mclen = tostring(multiCost) --how many chars has multiply cost at the moment
  106. local stringlen = #buytext+#mclen
  107. local mpp = width - stringlen -- substract lenght of buytext + and cost and all charachters after cost from width to put the button to the right side
  108.     if money >= multiCost then
  109.     term.setCursorPos(mpp, 1)
  110.     term.setBackgroundColor(colors.black)
  111.     term.setTextColor(colors.white)
  112.     write (buytext ..multiCost)
  113.     end
  114.     term.setBackgroundColor(bgColor)
  115.     return mpp, mpp+stringlen-1
  116. end
  117.  
  118. local function checkmultiplier(pos)
  119. if money < multiCost then
  120. term.setBackgroundColor(bgColor)
  121. term.setCursorPos(pos, 1)
  122. term.clearLine()
  123. stats()
  124. end
  125. end
  126.  
  127. function stats()
  128.     frame()
  129.     term.setCursorPos(1,1)
  130.     -- You don't need term.setCursorPos(1, 2) since print automatically moves a line down each time
  131.     print("Money: "..money)
  132.     print("Gems: "..gemsClicked)
  133.     print("Multiplier:")
  134.     print(multi.."x")
  135. end
  136.  
  137. ------------ Running Code ------------
  138.  
  139. -- Don't use a number for the color unless absolutely nessecary...and I have no idea when it would be
  140. -- Use the colors API. 16384 is the same as saying colors.red
  141. term.setBackgroundColor(bgColor)
  142. term.setTextColor(txtColor)
  143. term.clear()
  144. spawnGems(gemsOnScreen)
  145. msp, mep = mpupgrade()
  146. stats()
  147.  
  148. while true do
  149.     local e, p1, p2, p3 = os.pullEvent("mouse_click")
  150.     term.setCursorPos(1, 1)
  151.     -- Check if the coordinates clicked are any of the pixel's coordinates
  152.         if  p1 == 1 and p2 >= msp and p2 <= mep and p3 == 1 then  -- im not sure about this but i this this should work cause needs to be checked after click and has to be outside the loop for getting gemcoords
  153.                 buymultiply()
  154.                 msp, mep = mpupgrade()
  155.                 checkmultiplier(msp)       
  156.         end
  157.     for i, v in ipairs(gems) do
  158.         if p1 == 1 and v[1] == p2 and v[2] == p3 then -- A gem was LEFT clicked
  159.             -- Do stuff here
  160.             -- Do more stuff
  161.             -- Increment variables
  162.             gemsClicked = gemsClicked + 1
  163.  
  164.             -- Give money
  165.             money = money + (gemValue[v[3]] * multi) -- Gem value multiplied by the multiplier
  166.  
  167.             -- Remove the gem
  168.             term.setBackgroundColor(bgColor)
  169.             term.setCursorPos(v[1], v[2])
  170.             write(" ")
  171.             table.remove(gems, i)
  172.  
  173.             -- Spawn a new gem
  174.             newGem()
  175.  
  176.             -- Update stats
  177.             msp, mep = mpupgrade()
  178.             stats()
  179.             end
  180.     end
  181.     sleep(0.05)
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement