Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Lets make a game.. No plagurizing or sharing this to any site other then Computercraft.info
- local Version = 0.1
- local Player = {"<", ">", "^", "V"}
- local Laser = {}
- local Lasers = {"-", "-", "i", "!"}
- local Pulsing = {"o", "O", "0", "O"}
- local PulseExplodes = {}
- local Pulse = {}
- local Cooldown = 0
- local CooldownPulse = 0
- local Map = {}
- local MapView = {}
- local Health = 10
- local Lives = 3
- local Score = 10
- local Name = ""
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function LoadFile()
- repeat
- clear()
- term.setCursorPos(1,2)
- print(fs.list("Maps"))
- term.setCursorPos(1,1)
- write("A map file: ")
- Name = read()
- if not fs.exists("Maps/"..Name) then
- print("That file doesnt exist")
- sleep(3)
- end
- until fs.exists("Maps/"..Name)
- clear()
- File = io.open("Maps/"..Name, "r")
- for line in File:lines() do
- Map[#Map+1] = line.."\n"
- end
- PlayerCords = Map[1]
- PlayerDir = tonumber(string.sub(PlayerCords, 5, 5))
- PlayerX = tonumber(string.sub(PlayerCords, 1, 2))
- PlayerY = tonumber(string.sub(PlayerCords, 3, 4))
- table.remove(Map, 1)
- File:close()
- p = io.open("Debug", "w")
- for n=1,#Map do
- p:write(Map[n])
- end
- for n=1,#Map do
- MapView[n] = Map[n]
- end
- p:close()
- StatusBox()
- reDraw()
- end
- function Load()
- Name = ""
- LoadFile()
- end
- function Tick()
- if Cooldown > 0 then
- Cooldown = Cooldown-1
- end
- if CooldownPulse > 0 then
- CooldownPulse = CooldownPulse-1
- end
- PulseMove()
- LaserMove()
- CheckUnder()
- UpdateExplode()
- reDraw()
- Ticker = os.startTimer(0.1)
- end
- function CheckUnder() -- Check under the player
- if CheckMapView(PlayerX, PlayerY) ~= " " then
- if CheckMapView(PlayerX, PlayerY) == "x" then --Pickup pack!
- StatusBox("Health", 5)
- deleteMapView(PlayerX, PlayerY)
- elseif CheckMapView(PlayerX, PlayerY) == "o" then -- Hit by explosion!
- StatusBox("Health", -3)
- deleteMapView(PlayerX, PlayerY)
- elseif CheckMapView(PlayerX, PlayerY) == "i" or CheckMapView(PlayerX, PlayerY) == "!" or CheckMapView(PlayerX, PlayerY) == "-" then --Laser!
- StatusBox("Health", -5)
- deleteMapView(PlayerX, PlayerY)
- for n=1,#Laser do
- if Laser[n] then
- local LaserX = tonumber(string.sub(Laser[n], 1, 2))
- local LaserY = tonumber(string.sub(Laser[n], 3, 4))
- if LaserX == PlayerX and LaserY == PlayerY then
- table.remove(Laser, n)
- n = n-1
- end
- end
- end
- elseif checkPulse() then --pulse!
- StatusBox("Health", -10)
- deleteMapView(PlayerX, PlayerY)
- PulseExplode(checkPulse())
- for n=1,#Pulse do
- if Pulse[n] then
- local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
- local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
- if PulseX == PlayerX and PulseY == PlayerY then
- table.remove(Pulse, n)
- n = n-1
- end
- end
- end
- end
- end
- end
- function checkPulse()
- for n=1,#Pulse do
- if Pulse[n] then
- local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
- local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
- if PulseX == PlayerX and PulseY == PlayerY then
- return PulseX, PulseY
- end
- end
- end
- return false
- end
- function StatusBox(Type, Modifier)
- FlashHP = false
- FlashLives = false
- if Type == "Health" then
- Health = tonumber(Health)+tonumber(Modifier)
- FlashHP = true
- if Health <= 0 then
- if Lives > 1 then
- Lives = Lives-1
- Health = 10
- FlashLives = true
- else
- clear()
- print("You died")
- error()
- end
- end
- end
- DrawStatus()
- end
- function DrawStatus()
- for n=1,3 do
- MapView[n] = string.sub(MapView[n], 14, string.len(MapView[4]))
- end
- local ScoreNum = tonumber("4")-string.len(Score)
- local Spaces = string.rep(" ", ScoreNum)
- MapView[1] = "Score: "..Score..Spaces.." O"..MapView[1]
- if FlashLives == true then
- MapView[2] = "Lives: O"..MapView[2]
- else
- MapView[2] = "Lives: "..Lives.." O"..MapView[2]
- end
- local HealthNum = tonumber("4")-string.len(Health)
- local Spaces = string.rep(" ", HealthNum)
- if FlashHP == true then
- MapView[3] = "Health: O"..MapView[3]
- else
- MapView[3] = "Health: "..Health..Spaces.."O"..MapView[3]
- end
- MapView[4] = string.sub(MapView[4], 14, string.len(MapView[4]))
- MapView[4] = "OOOOOOOOOOOOO"..MapView[4]
- Draw = os.startTimer(0.15)
- end
- function CreateLaser(x, y, dir)
- if CheckMapView(x, y) ~= "O" then
- Laser[#Laser+1] = Num(x)..Num(y)..dir
- EditMapView(x, y, Lasers[dir])
- end
- Cooldown = 5
- end
- function CreatePulse(x, y, dir)
- if CheckMapView(x, y) ~= "O" then
- Pulse[#Pulse+1] = Num(x)..Num(y)..dir.."1".."1"
- EditMapView(x, y, Pulsing[dir])
- end
- CooldownPulse = 30
- end
- function Num(num)
- if string.len(num) == 1 then
- return "0"..num
- else
- return num
- end
- end
- function LaserMove()
- for n=1,#Laser do
- if Laser[n] then
- local LaserX = tonumber(string.sub(Laser[n], 1, 2))
- local LaserY = tonumber(string.sub(Laser[n], 3, 4))
- local LaserDir = tonumber(string.sub(Laser[n], 5, 5))
- deleteMapView(LaserX, LaserY)
- if LaserDir == 1 then
- LaserX = LaserX-1
- elseif LaserDir == 2 then
- LaserX = LaserX+1
- elseif LaserDir == 3 then
- LaserY = LaserY-1
- elseif LaserDir == 4 then
- LaserY = LaserY+1
- else
- error("LaserDir errored")
- end
- if CheckMapView(LaserX, LaserY) == " " then
- EditMapView(LaserX, LaserY, Lasers[LaserDir])
- Laser[n] = Num(LaserX)..Num(LaserY)..LaserDir
- else
- table.remove(Laser, n)
- n = n-1
- end
- end
- end
- end
- function PulseMove()
- for n=1,#Pulse do
- if Pulse[n] then
- local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
- local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
- local PulseDir = tonumber(string.sub(Pulse[n], 5, 5))
- local PulseState = tonumber(string.sub(Pulse[n], 6, 6))
- local Speed = tonumber(string.sub(Pulse[n], 7, 7))
- deleteMapView(PulseX, PulseY)
- local Sparex = PulseX
- local Sparey = PulseY
- if Speed == 1 then
- if PulseDir == 1 then
- PulseX = PulseX-1
- elseif PulseDir == 2 then
- PulseX = PulseX+1
- elseif PulseDir == 3 then
- PulseY = PulseY-1
- elseif PulseDir == 4 then
- PulseY = PulseY+1
- else
- error("LaserDir errored")
- end
- Speed = 0
- else
- Speed = 1
- end
- if PulseState == 4 then
- PulseState = 1
- else
- PulseState = PulseState+1
- end
- if CheckMap(PulseX, PulseY) == " " then
- EditMapView(PulseX, PulseY, Pulsing[PulseState])
- Pulse[n] = Num(PulseX)..Num(PulseY)..PulseDir..PulseState..Speed
- else
- PulseExplode(Sparex, Sparey)
- table.remove(Pulse, n)
- n = n-1
- end
- end
- end
- end
- function PulseExplode(x, y, Spread)
- if not Spread then Spread = "3" end
- local Nope = 5
- for n=1,#PulseExplodes do
- if PulseExplodes[n] then
- local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
- local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
- if PulseX == x and PulseY == y then -- Matches another explosion. Dont make it! Abort abort!
- Nope = 4
- end
- end
- end
- if Nope == 5 then -- Nothing is here.. right?
- if CheckMapView(x, y) ~= "O" then
- PulseExplodes[#PulseExplodes+1] = Num(x)..Num(y).."3"..Spread
- EditMapView(x, y, "o")
- end
- end
- end
- fs.delete("Log")
- function Logger(loggy)
- p = io.open("Log", "a")
- p:write(loggy.."\n")
- p:close()
- end
- function UpdateExplode()
- for n=1,#PulseExplodes do
- if PulseExplodes[n] then
- local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
- local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
- local PulseStatus = tonumber(string.sub(PulseExplodes[n], 5, 5))
- local Spread = tonumber(string.sub(PulseExplodes[n], 6, 6))
- if PulseStatus < 1 then -- How long it has left in the world
- deleteMapView(PulseX, PulseY)
- table.remove(PulseExplodes, n)
- elseif Spread ~= 0 then
- PulseExplode(PulseX+1, PulseY, Spread-1)
- PulseExplode(PulseX-1, PulseY, Spread-1)
- PulseExplode(PulseX, PulseY+1, Spread-1)
- PulseExplode(PulseX, PulseY-1, Spread-1)
- end
- if PulseExplodes[n] then
- if PulseStatus ~= 0 then
- PulseExplodes[n] = Num(PulseX)..Num(PulseY)..(PulseStatus-1)..(Spread-1)
- end
- end
- end
- end
- end
- function deleteMapView(x,y)
- local First = string.sub(MapView[y], 1, x-1)
- local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
- MapView[y] = First.." "..Second
- end
- function deleteMap(x,y)
- local First = string.sub(MapView[y], 1, x-1)
- local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
- MapView[y] = First.." "..Second
- end
- function Direction(xory, dir)
- if xory == "x" then
- if dir == 1 then --Left
- return -1
- elseif dir == 2 then --Right
- return 1
- elseif dir == 3 or dir == 4 then
- return 0
- else
- error("funtion Direction was handled incorrectly")
- end
- elseif xory == "y" then
- if dir == 3 then --Up
- return -1
- elseif dir == 4 then --Down
- return 1
- elseif dir == 1 or dir == 2 then
- return 0
- else
- error("funtion Direction was handled incorrectly")
- end
- else
- error("You need to use x or y!!!! in function direction!")
- end
- end
- function reDraw()
- clear()
- for n=1,#MapView do
- term.setCursorPos(1, n)
- term.write(MapView[n])
- end
- term.setCursorPos(PlayerX, PlayerY)
- term.write(Player[PlayerDir])
- end
- function CheckMap(x, y)
- if Map[y] then
- local MapString = Map[y]
- MapString = string.sub(MapString, x, x)
- return MapString
- else
- return "O"
- end
- end
- function CheckMapView(x, y)
- if MapView[y] then
- local Lens = string.len(MapView[y])
- if x > 0 and x < Lens then
- local MapString = MapView[y]
- MapString = string.sub(MapString, x, x)
- return MapString
- else
- return "O"
- end
- end
- return "O"
- end
- function EditMapView(x, y, Get)
- local First = string.sub(MapView[y], 1, x-1)
- local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
- MapView[y] = First..Get..Second
- end
- function EditMap(x, y, Get)
- local First = string.sub(Map[y], 1, x-1)
- local Second = string.sub(Map[y], x+1, string.len(Map[y]))
- Map[y] = First..Get..Second
- end
- function MovePlayer()
- Tick()
- while true do
- Events = {os.pullEvent()}
- if Events[1] == "key" then
- if Events[2] == 200 or Events[2] == 17 then -- Up
- PlayerDir = 3
- if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
- PlayerY = PlayerY+Direction("y", PlayerDir)
- CheckUnder()
- end
- reDraw()
- elseif Events[2] == 208 or Events[2] == 31 then -- Down
- PlayerDir = 4
- if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
- PlayerY = PlayerY+Direction("y", PlayerDir)
- CheckUnder()
- end
- reDraw()
- elseif Events[2] == 203 or Events[2] == 30 then -- Left
- PlayerDir = 1
- if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
- PlayerX = PlayerX+Direction("x", PlayerDir)
- CheckUnder()
- end
- reDraw()
- elseif Events[2] == 205 or Events[2] == 32 then -- Right
- PlayerDir = 2
- if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
- PlayerX = PlayerX+Direction("x", PlayerDir)
- CheckUnder()
- end
- reDraw()
- elseif Events[2] == 57 then --Spacebar
- if Cooldown == 0 then
- CreateLaser(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
- end
- elseif Events[2] == 45 then -- x button
- if CooldownPulse == 0 then
- CreatePulse(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
- end
- end
- elseif Events[1] == "timer" and Events[2] == Ticker then --Tick
- Tick()
- elseif Events[1] == "timer" and Events[2] == Draw then
- StatusBox("Nothing", "Ignore")
- end
- end
- end
- Load()
- MovePlayer()
Advertisement
Add Comment
Please, Sign In to add comment