Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Project info:
- Name: Gold Rush
- Creator: Jesusthekiller
- Language: Lua (CC)
- Website: None
- License: GNU GPL
- License file can be fount at www.jesusthekiller.com/license-gpl.html
- Version: 1.3
- ]]--
- --[[
- Changelog:
- 1.0:
- Beta Release
- 1.1:
- Public Release
- 1.2:
- Better graphics
- 1.3:
- Added levels
- Added suicide key
- ]]--
- --[[
- LICENSE:
- Gold Rush
- Copyright (c) 2013 Jesusthekiller
- 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.
- 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.
- 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/>.
- ]]--
- if not term.isColor() then
- error("Not Adv. Computer!", 0)
- end
- -- Map class
- local map = {}
- map.init = function()
- for i = 1, 51 do
- map[i] = {}
- for j = 1, 19 do
- map[i][j] = 0
- end
- end
- end
- map.print = function(x, y)
- if x > 51 or x < 1 then
- return
- end
- if y > 19 or y < 1 then
- return
- end
- term.setCursorPos(x, y)
- if map[x][y] == 0 then
- term.setBackgroundColor(colors.black)
- write(" ")
- end
- if map[x][y] == 1 then
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.black)
- write("$")
- end
- if map[x][y] == 2 then
- term.setTextColor(colors.gray)
- term.setBackgroundColor(colors.black)
- write("#")
- end
- if map[x][y] == 3 then
- term.setBackgroundColor(colors.red)
- write(" ")
- end
- end
- -- Timer IDs table
- local ids = {}
- -- Player class
- local player = {}
- player.getPos = function()
- return player.x, player.y
- end
- player.char = " "
- player.setPos = function(x, y, domap)
- x = math.floor(x)
- y = math.floor(y)
- if domap then map.print(player.getPos()) end
- if x > 51 or x < 1 then
- return
- end
- if y > 19 or y < 2 then
- return
- end
- if map[x][y] == 3 then
- return
- end
- if map[x][y] == 1 then
- -- Score
- local function score()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.yellow)
- term.setCursorPos(1, 1)
- for i = 1, 51 do
- write(" ")
- end
- local msg = "Score: "..player.getGold()
- term.setCursorPos((51 - #msg) / 2, 1)
- write(msg)
- end
- -- GoldGen
- local function genGold()
- local x, y
- repeat
- x = math.random(1, 51)
- y = math.random(2, 19)
- until map[x][y] ~= 3 and map[x][y] ~= 2 and map[x][y] ~= 1
- map[x][y] = 1
- map.print(x, y)
- end
- player.addGold()
- genGold()
- score()
- map[x][y] = 0
- end
- player.x = x
- player.y = y
- end
- player.print = function()
- term.setCursorPos(player.getPos())
- term.setBackgroundColor(colors.cyan)
- term.setTextColor(colors.white)
- write(player.char)
- end
- player.gold = 0 -- Init gold
- player.diff = 1
- player.getGold = function()
- return player.gold
- end
- player.addGold = function()
- player.gold = player.gold + 1
- end
- -- Score
- local function score()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.yellow)
- term.setCursorPos(1, 1)
- for i = 1, 51 do
- write(" ")
- end
- local msg = "Score: "..player.getGold()
- term.setCursorPos((51 - #msg) / 2, 1)
- write(msg)
- end
- -- GoldGen
- local function genGold()
- local x, y
- repeat
- x = math.random(1, 51)
- y = math.random(2, 19)
- until map[x][y] ~= 3 and map[x][y] ~= 2 and map[x][y] ~= 1
- map[x][y] = 1
- map.print(x, y)
- end
- -- Splash
- local function splash()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- print([[
- Gold Rush
- Controls:
- WASD: Walk
- K: Suicide
- O: Quit
- Collect gold and don't get smashed by falling blocks!
- Choose difficulity: (E)asy, (N)ormal, (H)ard, H(a)rdcore]])
- while true do
- local e, k = os.pullEvent("key")
- if k == keys.e then
- player.diff = 1.5
- break
- end
- if k == keys.n then
- player.diff = 3
- break
- end
- if k == keys.h then
- player.diff = 5
- break
- end
- if k == keys.a then
- player.diff = 10
- break
- end
- end
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Controls
- local function controls()
- while true do
- local e, k = os.pullEvent("key")
- local x, y = player.getPos()
- if k == keys.w then
- player.setPos(x, y - 1, true)
- player.char = "^"
- end
- if k == keys.s then
- player.setPos(x, y + 1, true)
- player.char = "V"
- end
- if k == keys.a then
- player.setPos(x - 1, y, true)
- player.char = "<"
- end
- if k == keys.d then
- player.setPos(x + 1, y, true)
- player.char = ">"
- end
- if k == keys.o then
- break
- end
- if k == keys.k then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- score()
- local msg = "Game Over!"
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos((51 - #msg) / 2, (18 / 2) + 1)
- write(msg)
- os.pullEvent("key")
- coroutine.yield()
- break
- end
- player.print()
- end
- end
- -- Falling blocks
- local function doBlock()
- while true do
- sleep(1 / player.diff)
- local x, y
- repeat
- x = math.random(1, 51)
- y = math.random(2, 19)
- until map[x][y] ~= 3 and map[x][y] ~= 2
- map[x][y] = 2
- map.print(x, y)
- local i = os.startTimer(1)
- ids[i] = {}
- ids[i]['x'] = x
- ids[i]['y'] = y
- end
- end
- -- Grow block
- local function blockGrow()
- while true do
- local e, i = os.pullEvent("timer")
- if ids[i] ~= nil then
- local px, py = player.getPos()
- local x = ids[i]["x"]
- local y = ids[i]["y"]
- map[x][y] = 3
- map.print(x, y)
- if x == px and y == py then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- score()
- local msg = "Game Over!"
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos((51 - #msg) / 2, (18 / 2) + 1)
- write(msg)
- os.pullEvent("key")
- coroutine.yield()
- break
- end
- end
- end
- end
- -- Gold Gen (timed)
- local function timedGoldGen()
- while true do
- genGold()
- sleep(5)
- end
- end
- -- doQuit
- local function doQuit()
- coroutine.yield()
- sleep(0)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- write(" Gold Rush by Jesusthekiller. Thanks for playing!")
- term.setCursorPos(1, 2)
- end
- --[[
- Initialize game:
- ]]--
- -- Splash
- splash()
- -- Make map:
- map.init()
- -- Init player pos
- local tx, ty = term.getSize()
- player.setPos(tx / 2, ty / 2)
- -- Init Scoreboard
- score()
- -- Init player print
- player.print()
- -- Start game:
- parallel.waitForAny(controls, doBlock, blockGrow, timedGoldGen)
- doQuit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement