Advertisement
jesusthekiller

Gold Rush

Jun 6th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 KB | None | 0 0
  1. --[[
  2.     Project info:
  3.    
  4.     Name: Gold Rush
  5.     Creator: Jesusthekiller
  6.     Language: Lua (CC)
  7.     Website: None
  8.     License: GNU GPL
  9.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  10.  
  11.     Version: 1.3
  12. ]]--
  13.  
  14. --[[
  15.     Changelog:
  16.       1.0:
  17.         Beta Release
  18.       1.1:
  19.         Public Release
  20.       1.2:
  21.         Better graphics
  22.       1.3:
  23.         Added levels
  24.         Added suicide key
  25. ]]--
  26.  
  27. --[[
  28.     LICENSE:
  29.    
  30.     Gold Rush
  31.     Copyright (c) 2013 Jesusthekiller
  32.  
  33.     This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  34.  
  35.     This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  36.  
  37.     See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  38. ]]--
  39.  
  40. if not term.isColor() then
  41.     error("Not Adv. Computer!", 0)
  42. end
  43.  
  44. -- Map class
  45. local map = {}
  46.  
  47. map.init = function()
  48.     for i = 1, 51 do
  49.         map[i] = {}
  50.         for j = 1, 19 do
  51.             map[i][j] = 0
  52.         end
  53.     end
  54. end
  55.  
  56. map.print = function(x, y)
  57.     if x > 51 or x < 1 then
  58.         return
  59.     end
  60.    
  61.     if y > 19 or y < 1 then
  62.         return
  63.     end
  64.    
  65.     term.setCursorPos(x, y)
  66.    
  67.     if map[x][y] == 0 then
  68.         term.setBackgroundColor(colors.black)
  69.         write(" ")
  70.     end
  71.    
  72.     if map[x][y] == 1 then
  73.         term.setTextColor(colors.yellow)
  74.         term.setBackgroundColor(colors.black)
  75.         write("$")
  76.     end
  77.    
  78.     if map[x][y] == 2 then
  79.         term.setTextColor(colors.gray)
  80.         term.setBackgroundColor(colors.black)
  81.         write("#")
  82.     end
  83.    
  84.     if map[x][y] == 3 then
  85.         term.setBackgroundColor(colors.red)
  86.         write(" ")
  87.     end
  88. end
  89.  
  90. -- Timer IDs table
  91. local ids = {}
  92.  
  93. -- Player class
  94. local player = {}
  95.  
  96. player.getPos = function()
  97.     return player.x, player.y
  98. end
  99.  
  100. player.char = " "
  101.  
  102. player.setPos = function(x, y, domap)
  103.     x = math.floor(x)
  104.     y = math.floor(y)
  105.    
  106.     if domap then map.print(player.getPos()) end
  107.    
  108.     if x > 51 or x < 1 then
  109.         return
  110.     end
  111.    
  112.     if y > 19 or y < 2 then
  113.         return
  114.     end
  115.    
  116.     if map[x][y] == 3 then
  117.         return
  118.     end
  119.    
  120.     if map[x][y] == 1 then
  121.         -- Score
  122.  
  123.         local function score()
  124.             term.setBackgroundColor(colors.gray)
  125.             term.setTextColor(colors.yellow)
  126.             term.setCursorPos(1, 1)
  127.            
  128.             for i = 1, 51 do
  129.                 write(" ")
  130.             end
  131.            
  132.             local msg = "Score: "..player.getGold()
  133.            
  134.             term.setCursorPos((51 - #msg) / 2, 1)
  135.             write(msg)
  136.         end
  137.  
  138.         -- GoldGen
  139.  
  140.         local function genGold()
  141.             local x, y
  142.                
  143.             repeat
  144.                 x = math.random(1, 51)
  145.                 y = math.random(2, 19)
  146.             until map[x][y] ~= 3 and map[x][y] ~= 2 and map[x][y] ~= 1
  147.                
  148.             map[x][y] = 1
  149.             map.print(x, y)
  150.         end
  151.        
  152.         player.addGold()
  153.         genGold()
  154.         score()
  155.         map[x][y] = 0
  156.     end
  157.    
  158.     player.x = x
  159.     player.y = y
  160. end
  161.  
  162. player.print = function()
  163.     term.setCursorPos(player.getPos())
  164.     term.setBackgroundColor(colors.cyan)
  165.     term.setTextColor(colors.white)
  166.     write(player.char)
  167. end
  168.  
  169. player.gold = 0 -- Init gold
  170.  
  171. player.diff = 1
  172.  
  173. player.getGold = function()
  174.     return player.gold
  175. end
  176.  
  177. player.addGold = function()
  178.     player.gold = player.gold + 1
  179. end
  180.  
  181. -- Score
  182.  
  183. local function score()
  184.     term.setBackgroundColor(colors.gray)
  185.     term.setTextColor(colors.yellow)
  186.     term.setCursorPos(1, 1)
  187.    
  188.     for i = 1, 51 do
  189.         write(" ")
  190.     end
  191.    
  192.     local msg = "Score: "..player.getGold()
  193.    
  194.     term.setCursorPos((51 - #msg) / 2, 1)
  195.     write(msg)
  196. end
  197.  
  198. -- GoldGen
  199.  
  200. local function genGold()
  201.     local x, y
  202.        
  203.     repeat
  204.         x = math.random(1, 51)
  205.         y = math.random(2, 19)
  206.     until map[x][y] ~= 3 and map[x][y] ~= 2 and map[x][y] ~= 1
  207.        
  208.     map[x][y] = 1
  209.     map.print(x, y)
  210. end
  211.  
  212. -- Splash
  213.  
  214. local function splash()
  215.     term.setBackgroundColor(colors.black)
  216.     term.setTextColor(colors.white)
  217.     term.clear()
  218.     term.setCursorPos(1, 1)
  219.    
  220.     print([[
  221. Gold Rush
  222.  
  223. Controls:
  224. WASD: Walk
  225. K: Suicide
  226. O: Quit
  227.  
  228. Collect gold and don't get smashed by falling blocks!
  229.  
  230. Choose difficulity: (E)asy, (N)ormal, (H)ard, H(a)rdcore]])
  231.     while true do
  232.         local e, k = os.pullEvent("key")
  233.        
  234.         if k == keys.e then
  235.             player.diff = 1.5
  236.             break
  237.         end
  238.        
  239.         if k == keys.n then
  240.             player.diff = 3
  241.             break
  242.         end
  243.        
  244.         if k == keys.h then
  245.             player.diff = 5
  246.             break
  247.         end
  248.        
  249.         if k == keys.a then
  250.             player.diff = 10
  251.             break
  252.         end
  253.     end
  254.    
  255.     term.clear()
  256.     term.setCursorPos(1, 1)
  257. end
  258.  
  259. -- Controls
  260.  
  261. local function controls()
  262.     while true do
  263.         local e, k = os.pullEvent("key")
  264.        
  265.         local x, y = player.getPos()
  266.        
  267.         if k == keys.w then
  268.             player.setPos(x, y - 1, true)
  269.             player.char = "^"
  270.         end
  271.        
  272.         if k == keys.s then
  273.             player.setPos(x, y + 1, true)
  274.             player.char = "V"
  275.         end
  276.        
  277.         if k == keys.a then
  278.             player.setPos(x - 1, y, true)
  279.             player.char = "<"
  280.         end
  281.        
  282.         if k == keys.d then
  283.             player.setPos(x + 1, y, true)
  284.             player.char = ">"
  285.         end
  286.        
  287.         if k == keys.o then
  288.             break
  289.         end
  290.        
  291.         if k == keys.k then
  292.             term.setBackgroundColor(colors.black)
  293.             term.clear()
  294.             term.setCursorPos(1, 1)
  295.                
  296.             score()
  297.                
  298.             local msg = "Game Over!"
  299.                
  300.             term.setBackgroundColor(colors.black)
  301.             term.setTextColor(colors.white)
  302.                
  303.             term.setCursorPos((51 - #msg) / 2, (18 / 2) + 1)
  304.             write(msg)
  305.            
  306.             os.pullEvent("key")
  307.             coroutine.yield()
  308.             break
  309.         end
  310.        
  311.         player.print()
  312.     end
  313. end
  314.  
  315. -- Falling blocks
  316.  
  317. local function doBlock()
  318.     while true do
  319.         sleep(1 / player.diff)
  320.        
  321.         local x, y
  322.        
  323.         repeat
  324.             x = math.random(1, 51)
  325.             y = math.random(2, 19)
  326.         until map[x][y] ~= 3 and map[x][y] ~= 2
  327.        
  328.         map[x][y] = 2
  329.         map.print(x, y)
  330.        
  331.         local i = os.startTimer(1)
  332.        
  333.         ids[i] = {}
  334.         ids[i]['x'] = x
  335.         ids[i]['y'] = y
  336.     end
  337. end
  338.  
  339. -- Grow block
  340.  
  341. local function blockGrow()
  342.     while true do
  343.         local e, i = os.pullEvent("timer")
  344.         if ids[i] ~= nil then
  345.             local px, py = player.getPos()
  346.             local x = ids[i]["x"]
  347.             local y = ids[i]["y"]
  348.            
  349.             map[x][y] = 3
  350.             map.print(x, y)
  351.            
  352.             if x == px and y == py then
  353.                 term.setBackgroundColor(colors.black)
  354.                 term.clear()
  355.                 term.setCursorPos(1, 1)
  356.                
  357.                 score()
  358.                
  359.                 local msg = "Game Over!"
  360.                
  361.                 term.setBackgroundColor(colors.black)
  362.                 term.setTextColor(colors.white)
  363.                
  364.                 term.setCursorPos((51 - #msg) / 2, (18 / 2) + 1)
  365.                 write(msg)
  366.                
  367.                 os.pullEvent("key")
  368.                 coroutine.yield()
  369.                 break
  370.             end
  371.         end
  372.     end
  373. end
  374.  
  375. -- Gold Gen (timed)
  376.  
  377. local function timedGoldGen()
  378.     while true do
  379.         genGold()
  380.         sleep(5)
  381.     end
  382. end
  383.  
  384. -- doQuit
  385.  
  386. local function doQuit()
  387.     coroutine.yield()
  388.    
  389.     sleep(0)
  390.    
  391.     term.setBackgroundColor(colors.black)
  392.     term.setTextColor(colors.white)
  393.     term.clear()
  394.     term.setCursorPos(1, 1)
  395.    
  396.     write(" Gold Rush by Jesusthekiller. Thanks for playing!")
  397.    
  398.     term.setCursorPos(1, 2)
  399. end
  400.  
  401. --[[
  402.     Initialize game:
  403. ]]--
  404.  
  405. -- Splash
  406. splash()
  407.  
  408. -- Make map:
  409. map.init()
  410.  
  411. -- Init player pos
  412. local tx, ty = term.getSize()
  413. player.setPos(tx / 2, ty / 2)
  414.  
  415. -- Init Scoreboard
  416. score()
  417.  
  418. -- Init player print
  419. player.print()
  420.  
  421. -- Start game:
  422. parallel.waitForAny(controls, doBlock, blockGrow, timedGoldGen)
  423.  
  424. doQuit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement