Posted 25 April 2012 - 06:13 PM
Now that I got your attention.
Let me first say this.
This game is a WIP and you use this at your own risk.
Anyone caught plagiarizing my dumb code will be hated and if possible I will do bla bla.
When I say "WIP" I mean you can already play it.
But you have no way to gain score or win..
Nothing to play against but to play with the game itself
Controls : Arrow keys, a,s,d,w
Controls : Spacebar, X
As for the map editor
The only letters you can safely use atm are: x-health, O-wall
Anything else will either look weird, Or bug out any interaction with it.
It uses a format of 18,49 by default.
You could probably expand that with no problems.
Any bugs, Please report them to me
You must make a map before you can play it.
Common sense :)/>/>
And now for the code..
–Map Maker
http://pastebin.com/8sZUfjhi - V0.3! Game!
http://pastebin.com/SWTaDCqn - V0.3! Map Editor!
This could be done a bit neater, But this is a WIP after all :)/>/>
Planned Features
Let me first say this.
This game is a WIP and you use this at your own risk.
Anyone caught plagiarizing my dumb code will be hated and if possible I will do bla bla.
When I say "WIP" I mean you can already play it.
But you have no way to gain score or win..
Nothing to play against but to play with the game itself
Controls : Arrow keys, a,s,d,w
Controls : Spacebar, X
As for the map editor
The only letters you can safely use atm are: x-health, O-wall
Anything else will either look weird, Or bug out any interaction with it.
It uses a format of 18,49 by default.
You could probably expand that with no problems.
Any bugs, Please report them to me
You must make a map before you can play it.
Common sense :)/>/>
And now for the code..
Spoiler
--Lets make a game.. No plagurizing or sharing this to any site other then Computercraft.info
local Version = 0.3
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 = 9
local Score = 10
local Name = ""
local DrawLineNo = {}
local Data = {}
local debug = false
function clear()
term.clear()
term.setCursorPos(1,1)
end
function checkData(y, x, well)
if Data[tonumber(y)] then
if Data[tonumber(y)][tonumber(x)] then
local Msg = string.sub(Data[y][x], 1, 3)
local msg = string.sub(Data[y][x], 4)
return Msg, msg
end
return false
end
end
function setData(y, x, Stuff)
if not Stuff then stuff = "nil" else stuff = Stuff end
if not Data[tonumber(y)] then
Data[tonumber(y)] = {}
end
Data[tonumber(y)][tonumber(x)] = Stuff
end
function LoadFile()
repeat
clear()
term.setCursorPos(1,2)
for n,files in ipairs(fs.list("Maps")) do
print(files)
end
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()
for n=1,#Map do
MapView[n] = Map[n]
end
StatusBox("Nothing", "Ignore")
for n=1, #MapView do
for q=1, string.len(MapView[1]) do
if string.sub(MapView[n], q, q) == "O" then
setData(q, n, "WAL")
elseif string.sub(MapView[n], q, q) == "x" then
setData(q, n, "HPP")
end
end
end
local GetSizeX,GetSizeY = term.getSize()
for n=1, GetSizeX do -- Set sides of screen to walls
setData(n, 0, "WAL")
setData(n, GetSizeY+1, "WAL")
end
for n=1, GetSizeY do -- Set row above top row to walls
setData(0, n, "WAL")
end
for n=1, GetSizeY do -- Set row under bottom row to walls
setData(GetSizeX+1, n, "WAL")
end
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()
Ticker = os.startTimer(0.1)
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
local a,z = term.getSize()
for n=1,z do
if DrawLineNo[n] then
drawLine(n)
end
end
DrawLineNo = {}
end
function CheckUnder() -- Check under the player
local sData, mData = checkData(PlayerX, PlayerY, "Dont check")
if sData then
if sData == "HHP" then --Pickup pack!
StatusBox("Health", 5)
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
setData(PlayerX, PlayerY, "PLR")
elseif sData == "EPL" then -- Hit by explosion!
StatusBox("Health", -3)
setData(PlayerX, PlayerY, "PLR")
--table.remove(PulseExplodes, tonumber(mData))
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
elseif sData == "LSR" then --Laser!
StatusBox("Health", -5)
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
table.remove(Laser, tonumber(mData))
setData(PlayerX, PlayerY, "PLR")
elseif sData == "PUL" then --pulse!
StatusBox("Health", -10)
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
PulseExplode(PlayerX, PlayerY)
table.remove(Pulse, tonumber(mData))
setData(PlayerX, PlayerY, "PLR")
end
end
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]
DrawLineNo[1] = true
DrawLineNo[2] = true
DrawLineNo[3] = true
DrawLineNo[4] = true
Draw = os.startTimer(0.15)
end
function CreateLaser(x, y, dir)
if not checkData(x, y) then
Laser[#Laser+1] = Num(x)..Num(y)..dir
EditMapView(x, y, Lasers[dir])
setData(x, y, "LSR"..#Laser)
DrawLineNo[y] = true
end
Cooldown = 5
end
function CreatePulse(x, y, dir)
if checkData(x, y) ~= "WAL" then
Pulse[#Pulse+1] = Num(x)..Num(y)..dir.."1".."1"
EditMapView(x, y, Pulsing[dir])
setData(x, y, "PUL"..#Pulse)
end
CooldownPulse = 0
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)
setData(LaserX, LaserY)
DrawLineNo[LaserY] = true
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 not checkData(LaserX, LaserY) then
EditMapView(LaserX, LaserY, Lasers[LaserDir])
Laser[n] = Num(LaserX)..Num(LaserY)..LaserDir
setData(LaserX, LaserY, "LSR"..n)
DrawLineNo[LaserY] = true
elseif checkData(LaserX, LaserY) == "PUL" then
local Type, ID = checkData(LaserX, LaserY)
table.remove(Laser, n)
n = n-1
PulseExplode(LaserX, LaserY)
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)
DrawLineNo[PulseY] = true
setData(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 checkData(PulseX, PulseY) ~= "WAL" and checkData(PulseX, PulseY) ~= "EPL" then
EditMapView(PulseX, PulseY, Pulsing[PulseState])
Pulse[n] = Num(PulseX)..Num(PulseY)..PulseDir..PulseState..Speed
setData(PulseX, PulseY, "PUL"..n)
DrawLineNo[PulseY] = true
elseif checkData(PulseX, PulseY) == "PLR" then
statusBox("Health", -5)
PulseExplode(Sparex, Sparey)
table.remove(Pulse, n)
n = n-1
elseif checkData(PulseX, PulseY) == "EPL" then
local Type, ID = checkData(PulseX, PulseY)
table.remove(Pulse, n)
n = n-1
PulseExplode(Sparex, Sparey)
elseif checkData(PulseX, PulseY) == "LSR" then
local Type, ID = checkData(LaserX, LaserY)
table.remove(Laser, n)
n = n-1
PulseExplode(LaserX, LaserY)
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 = "9"
end
EditMapView(x, y, "o")
if checkData(x, y) == "PUL" then
local sData, mData = checkData(x, y)
setData(x, y)
table.remove(Pulse, tonumber(mData))
PulseExplode(x, y)
elseif checkData(x, y) == "EPL" then
local TY, ID = checkData(x, y)
setData(x, y, "EPL"..ID)
PulseExplodes[ID] = Num(x)..Num(y).."3"..Spread
else
PulseExplodes[#PulseExplodes+1] = Num(x)..Num(y).."3"..Spread
end
setData(x, y, "EPL"..#PulseExplodes)
DrawLineNo[y] = true
end
fs.delete("Maps/Log")
function Logger(loggy) -- Used for debugging
if debug == true then
p = io.open("Maps/Log", "a")
p:write(loggy.."\n")
p:close()
end
end
function UpdateExplode()
for n=1,#PulseExplodes do
Logger(n.." "..#PulseExplodes)
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))
Spread = Spread-1
PulseStatus = PulseStatus-1
function checky(x, y)
if checkData(x, y) ~= "WAL" then
return true
end
end
if PulseStatus < 1 then -- How long it has left in the world
deleteMapView(PulseX, PulseY)
setData(PulseX, PulseY)
Logger(n.." = ".."Removed: "..PulseExplodes[n])
table.remove(PulseExplodes, n)
DrawLineNo[PulseY] = true
n = n-1
Logger((n+1).." is now "..n)
end
if PulseStatus > 0 then
PulseExplodes[n] = Num(PulseX)..Num(PulseY)..(PulseStatus).."0"
end
if Spread > 0 then
if checky(PulseX+1, PulseY) then
PulseExplode(PulseX+1, PulseY, Spread)
end
if checky(PulseX-1, PulseY) then
PulseExplode(PulseX-1, PulseY, Spread)
end
if checky(PulseX, PulseY+1) then
PulseExplode(PulseX, PulseY+1, Spread)
end
if checky(PulseX, PulseY-1) then
PulseExplode(PulseX, PulseY-1, Spread)
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 DirX(dir)
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
end
function DirY(dir)
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
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 drawLine(LineY)
term.setCursorPos(1, LineY)
term.write(MapView[LineY])
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)
if MapView[y] then
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
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 checkData(PlayerX, PlayerY+DirY(PlayerDir), "Dont check") ~= "WAL" then
PlayerY = PlayerY-1
CheckUnder()
EditMapView(PlayerX, PlayerY+1, " ")
drawLine(PlayerY+1)
end
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
drawLine(PlayerY)
elseif Events[2] == 208 or Events[2] == 31 then -- Down
PlayerDir = 4
if checkData(PlayerX, PlayerY+DirY(PlayerDir), "Dont check") ~= "WAL" then
PlayerY = PlayerY+1
CheckUnder()
EditMapView(PlayerX, PlayerY-1, " ")
drawLine(PlayerY-1)
end
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
drawLine(PlayerY)
elseif Events[2] == 203 or Events[2] == 30 then -- Left
PlayerDir = 1
if checkData(PlayerX+DirX(PlayerDir), PlayerY, "Dont check") ~= "WAL" then
PlayerX = PlayerX-1
CheckUnder()
EditMapView(PlayerX+1, PlayerY, " ")
drawLine(PlayerY)
end
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
drawLine(PlayerY)
elseif Events[2] == 205 or Events[2] == 32 then -- Right
PlayerDir = 2
if checkData(PlayerX+DirX(PlayerDir), PlayerY, "Dont check") ~= "WAL" then
PlayerX = PlayerX+1
CheckUnder()
EditMapView(PlayerX-1, PlayerY, " ")
drawLine(PlayerY)
end
EditMapView(PlayerX, PlayerY, Player[PlayerDir])
drawLine(PlayerY)
elseif Events[2] == 57 then --Spacebar
if Cooldown == 0 then
CreateLaser(PlayerX+DirX(PlayerDir), PlayerY+DirY(PlayerDir), PlayerDir)
end
elseif Events[2] == 45 then -- x button
if CooldownPulse == 0 then
CreatePulse(PlayerX+DirX(PlayerDir), PlayerY+DirY(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")
elseif Events[2] == "g" then
term.setCursorPos(PlayerX, PlayerY)
local G, P = checkData(PlayerX+DirX(PlayerDir), PlayerY, "Dont check")
if G then term.write(G)
if P then term.write(P)
end
else
term.write("G")
end
end
end
end
Load()
MovePlayer()
–Map Maker
Spoiler
--Map Maker, No plagurizing or sharing this to any site other then Computercraft.info
local Version = 0.3
Map = {}
term.setCursorPos(1,1)
termx = 1
termy = 1
function Clear()
term.clear()
term.setCursorPos(1,1)
end
function CreateEmpty()
termx = 1
termy = 1
for n=1,18 do
Map[n] = string.rep(" ", 50)
end
for n=1,4 do
Map[n] = string.sub(Map[n], 14, string.len(Map[n]))
end
Map[1] = "Press Ctrl ."..Map[1]
Map[2] = "To go to the."..Map[2]
Map[3] = "Menu.DontEdit"..Map[3]
Map[4] = "This..NoUse.."..Map[4]
end
function EditMap(Get)
local sMap = Map[y]
Map[y] = string.sub(sMap, 1, x-1)..Get..string.sub(sMap, x+1)
DrawLine()
if x == 49 then
term.setCursorPos(x,y)
end
DrawLine()
end
function Draw()
Clear()
for n=1,#Map do
term.write(Map[n])
term.setCursorPos(1,n+1)
end
term.setCursorPos(termx, termy)
end
function DrawLine()
term.setCursorPos(1, termy)
term.clearLine()
term.write(Map[termy])
term.setCursorPos(termx, termy)
end
function Menu()
Clear()
print("1. Save")
print("2. Discard and redo")
print("3. Resume")
print("4. Quit")
print("5. Map Options")
print("6. Load Map")
while true do
term.setCursorPos(1,8)
event,param1 = os.pullEvent()
if event == "char" then
if param1 == "1" then
if Dir then
Save()
Draw()
Move()
else
term.setCursorPos(1,7)
write("Please run options first")
term.setCursorPos(1,8)
end
elseif param1 == "2" then
CreateEmpty()
Draw()
Move()
elseif param1 == "3" then
Draw()
Move()
elseif param1 == "4" then
Clear()
print("You quit the Map Program!")
error()
elseif param1 == "5" then
Options()
Clear()
Menu()
elseif param1 == "6" then
write("Load Map: ")
if not Load(read()) then term.setCursorPos(1,7) write("Please choose a proper file name") sleep(4) Menu() else
Clear()
Draw()
Move()
end
end
end
end
end
function Options()
Clear()
repeat
Clear()
write("X Cord to start the player at: ")
xCord = read()
if not tonumber(xCord) then xCord = 22222222 end
if string.len(xCord) == 1 then
xCord = "0"..xCord
end
until tonumber(xCord) > 0 and tonumber(xCord) < 49
repeat
Clear()
write("Y Cord to start the player at: ")
yCord = read()
if not tonumber(yCord) then yCord = 22222222 end
if string.len(yCord) == 1 then
yCord = "0"..yCord
end
until tonumber(yCord) > 0 and tonumber(yCord) < 19
repeat
Clear()
write("Direction to start player 1-left, 2-right, 3-up, 4-down: ")
if not tonumber(Dir) then Dir = 22222222 end
Dir = read()
until tonumber(Dir) > 0 and tonumber(Dir) < 5
Poopbear = xCord..yCord..Dir
end
function Load(Name)
if fs.exists("Maps/"..Name) then
FP = io.open("Maps/"..Name, "r")
Map = {}
for line in FP:lines() do
Map[#Map+1] = line
end
Poopbear = Map[1]
table.remove(Map, 1)
FP:close()
return true
else
return false
end
end
function Save()
if not Name then
Clear()
write("Name of file: ")
Name = read()
end
if not fs.isDir("Maps") then fs.makeDir("Maps") end
table.insert(Map, 1, Poopbear)
local pi = io.open("Maps/"..Name, "w")
for n=1,#Map do
pi:write(Map[n].."\n")
end
pi:close()
table.remove(Map, 1)
end
function Move()
term.setCursorBlink(true)
while true do
event,param1,param2 = os.pullEvent()
if event == "char" then
local sMap = Map[termy]
Map[termy] = string.sub(sMap, 1, termx-1)..param1..string.sub(sMap, termx+1)
DrawLine()
if termx < 50 then
term.setCursorPos(termx+1, termy)
termx = termx+1
end
elseif event == "key" then
if param1 == 208 then-- Down
if termy < 18 then
term.setCursorPos(termx, termy+1)
termy = termy+1
end
elseif param1 == 203 then -- Left
if termx > 1 then
term.setCursorPos(termx-1, termy)
termx = termx-1
end
elseif param1 == 205 then -- right
if termx < 50 then
term.setCursorPos(termx+1, termy)
termx = termx+1
end
elseif param1 == 200 then -- up
if termy > 1 then
term.setCursorPos(termx, termy-1)
termy = termy-1
end
elseif param1 == 14 then -- Backspace
local sMap = Map[termy]
Map[termy] = string.sub(sMap, 1, termx-1).." "..string.sub(sMap, termx+1)
DrawLine()
if termx > 1 then
term.setCursorPos(termx-1, termy)
termx = termx-1
end
elseif param1 == 29 then --ctrl
Menu()
break
end
end
end
end
Clear()
CreateEmpty()
Draw()
Move()
http://pastebin.com/8sZUfjhi - V0.3! Game!
http://pastebin.com/SWTaDCqn - V0.3! Map Editor!
This could be done a bit neater, But this is a WIP after all :)/>/>
Planned Features
- Multiplayer Combat
- A way to score
- Enemies
- Idiot proof map making
- A version in RPG - Featuring maps connecting to each other.
- Perhaps NPCs.. No idea yet why you would want that.. So you can talk to them before they die?
- Still no idea how I plan to display other objects.
- Check Version button