Advertisement
Smiley43210

[Mackan90096] Gem Program [Debug]

Apr 11th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.95 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.red
  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(bicolor)
  84.  
  85.     -- Draw the debug stuff
  86.     term.setCursorPos(41, 2)
  87.     write("X: "..x.." Y: "..y)
  88.     term.setTextColor(colors.black)
  89.     term.setCursorPos(x, 1)
  90.     write("|")
  91.     term.setCursorPos(1, y)
  92.     write("-")
  93.     term.setTextColor(txtColor)
  94. end
  95.  
  96. function spawnGems(n)
  97.     for i = 1, n do
  98.         newGem()
  99.     end
  100. end
  101.  
  102. function _drawEm()
  103.     for i,v in ipairs(gems) do
  104.         -- Draw the gem
  105.         term.setCursorPos(v[1], v[2])
  106.         term.setBackgroundColor(v[3])
  107.         write(" ")
  108.         term.setBackgroundColor(bgColor)
  109.     end
  110. end
  111.  
  112. function buyMulti()
  113.     money = money - multiCost
  114.     multiCost = multiCost * 2
  115.     multi = multi + 1
  116.     stats()
  117. end
  118.  
  119. function stats()
  120.     term.setCursorPos(1, 1)
  121.     local left = 13
  122.     left = 13 - #"Money: "..money
  123.     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).
  124.     print("Gems: "..gemsClicked)
  125.     print("Multiplier: ")
  126.     print(multi.."x")
  127.     print("Upgrades:")
  128.     print("Multiplier")
  129.     print("Cost: "..multiCost)
  130.     --[[if money >= multiCost then
  131.         -- Color the button as 'disabled'
  132.         term.setTextColor(colors.white)
  133.         term.setBackgroundColor(colors.black)
  134.     else
  135.         -- Color the button as 'enabled'
  136.         term.setTextColor(colors.lightGray)
  137.         term.setBackgroundColor(colors.gray)
  138.     end
  139.     write("Upgrd Multi")
  140.     term.setTextColor(colors.white)
  141.     term.setBackgroundColor(colors.red)]]--
  142. end
  143.  
  144. ------------ Running Code ------------
  145.  
  146. -- Don't use a number for the color unless absolutely nessecary...and I have no idea when it would be
  147. -- Use the colors API. 16384 is the same as saying colors.red
  148. term.setBackgroundColor(bgColor)
  149. term.setTextColor(txtColor)
  150. term.clear()
  151. spawnGems(gemsOnScreen)
  152. term.clear()
  153. stats()
  154. _drawEm()
  155.  
  156. while true do
  157.     local e, p1, p2, p3 = os.pullEvent("mouse_click")
  158.     if  x >= 1 and x <= 11 and y == 7 then
  159.         if money >= multiCost then -- We use >= because the user may have more money than the cost
  160.             buyMulti()
  161.         end
  162.     end
  163.     -- Check if the coordinates clicked are any of the pixel's coordinates
  164.     for i, v in ipairs(gems) do
  165.         if p1 == 1 and v[1] == p2 and v[2] == p3 then -- A gem was LEFT clicked
  166.             -- Do stuff here
  167.             -- Increment variables
  168.             gemsClicked = gemsClicked + 1
  169.  
  170.             -- Give money
  171.             money = money + (gemValue[v[3]] * multi) -- Gem value multiplied by the multiplier
  172.  
  173.             -- Remove the gem
  174.             term.setBackgroundColor(bgColor)
  175.             term.setCursorPos(v[1], v[2])
  176.             write(" ")
  177.             table.remove(gems, i)
  178.  
  179.             term.clear()
  180.             _drawEm()
  181.  
  182.             -- Spawn a new gem
  183.             newGem()
  184.  
  185.             -- Update stats
  186.             stats()
  187.         end
  188.     end
  189.     sleep(0.05)
  190. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement