libraryaddict

Game

Apr 25th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.42 KB | None | 0 0
  1. --Lets make a game.. No plagurizing or sharing this to any site other then Computercraft.info
  2. local Version = 0.1
  3. local Player = {"<", ">", "^", "V"}
  4. local Laser = {}
  5. local Lasers = {"-", "-", "i", "!"}
  6. local Pulsing = {"o", "O", "0", "O"}
  7. local PulseExplodes = {}
  8. local Pulse = {}
  9. local Cooldown = 0
  10. local CooldownPulse = 0
  11. local Map = {}
  12. local MapView = {}
  13. local Health = 10
  14. local Lives = 3
  15. local Score = 10
  16. local Name = ""
  17. function clear()
  18.   term.clear()
  19.   term.setCursorPos(1,1)
  20. end
  21.  
  22. function LoadFile()
  23.   repeat
  24.     clear()
  25.     write("A map file: ")
  26.     Name = read()
  27.     if not fs.exists("Maps/"..Name) then
  28.       print("That file doesnt exist")
  29.       sleep(3)
  30.     end
  31.   until fs.exists("Maps/"..Name)
  32.   clear()
  33.   File = io.open("Maps/"..Name, "r")
  34.   for line in File:lines() do
  35.     Map[#Map+1] = line.."\n"
  36.   end
  37.   PlayerCords = Map[1]
  38.   PlayerDir = tonumber(string.sub(PlayerCords, 5, 5))
  39.   PlayerX = tonumber(string.sub(PlayerCords, 1, 2))
  40.   PlayerY = tonumber(string.sub(PlayerCords, 3, 4))
  41.   table.remove(Map, 1)
  42.   File:close()
  43.   p = io.open("Debug", "w")
  44.   for n=1,#Map do
  45.     p:write(Map[n])
  46.   end
  47.   for n=1,#Map do
  48.     MapView[n] = Map[n]
  49.   end
  50.   p:close()
  51.   StatusBox()
  52.   reDraw()
  53. end
  54.  
  55. function Load()
  56.   Name = ""
  57.   LoadFile()
  58. end
  59.  
  60. function Tick()
  61.   if Cooldown > 0 then
  62.     Cooldown = Cooldown-1
  63.   end
  64.   if CooldownPulse > 0 then
  65.     CooldownPulse = CooldownPulse-1
  66.   end
  67.   PulseMove()
  68.   LaserMove()
  69.   CheckUnder()
  70.   UpdateExplode()
  71.   reDraw()
  72.   Ticker = os.startTimer(0.1)
  73. end
  74.  
  75. function CheckUnder() -- Check under the player
  76.   if CheckMapView(PlayerX, PlayerY) ~= " " then
  77.     if CheckMapView(PlayerX, PlayerY) == "x" then --Pickup pack!
  78.       StatusBox("Health", 5)
  79.       deleteMapView(PlayerX, PlayerY)
  80.     elseif CheckMapView(PlayerX, PlayerY) == "o" then -- Hit by explosion!
  81.       StatusBox("Health", -3)
  82.       deleteMapView(PlayerX, PlayerY)
  83.     elseif CheckMapView(PlayerX, PlayerY) == "i" or CheckMapView(PlayerX, PlayerY) == "!" or CheckMapView(PlayerX, PlayerY) == "-" then --Laser!
  84.       StatusBox("Health", -5)
  85.       deleteMapView(PlayerX, PlayerY)
  86.       for n=1,#Laser do
  87.         if Laser[n] then
  88.           local LaserX = tonumber(string.sub(Laser[n], 1, 2))
  89.           local LaserY = tonumber(string.sub(Laser[n], 3, 4))
  90.           if LaserX == PlayerX and LaserY == PlayerY then
  91.             table.remove(Laser, n)
  92.             n = n-1
  93.           end
  94.         end
  95.       end
  96.     elseif checkPulse() then --pulse!
  97.       StatusBox("Health", -10)
  98.       deleteMapView(PlayerX, PlayerY)
  99.       PulseExplode(checkPulse())
  100.       for n=1,#Pulse do
  101.         if Pulse[n] then
  102.           local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  103.           local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  104.           if PulseX == PlayerX and PulseY == PlayerY then
  105.             table.remove(Pulse, n)
  106.             n = n-1
  107.           end
  108.         end
  109.       end
  110.     end
  111.   end
  112. end
  113.  
  114. function checkPulse()
  115.   for n=1,#Pulse do
  116.     if Pulse[n] then
  117.       local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  118.       local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  119.       if PulseX == PlayerX and PulseY == PlayerY then
  120.         return PulseX, PulseY
  121.       end
  122.     end
  123.   end
  124.   return false
  125. end
  126.  
  127. function StatusBox(Type, Modifier)
  128.   FlashHP = false
  129.   FlashLives = false
  130.   if Type == "Health" then
  131.     Health = tonumber(Health)+tonumber(Modifier)
  132.     FlashHP = true
  133.     if Health <= 0 then
  134.       if Lives > 1 then
  135.         Lives = Lives-1
  136.         Health = 10
  137.         FlashLives = true
  138.       else
  139.         clear()
  140.         print("You died")
  141.         error()
  142.       end
  143.     end
  144.   end
  145.   DrawStatus()
  146. end
  147.  
  148. function DrawStatus()
  149.   for n=1,3 do
  150.     MapView[n] = string.sub(MapView[n], 14, string.len(MapView[4]))
  151.   end
  152.   local ScoreNum = tonumber("4")-string.len(Score)
  153.   local Spaces = string.rep(" ", ScoreNum)
  154.   MapView[1] = "Score: "..Score..Spaces.." O"..MapView[1]
  155.   if FlashLives == true then
  156.     MapView[2] = "Lives:      O"..MapView[2]
  157.   else
  158.     MapView[2] = "Lives: "..Lives.."    O"..MapView[2]
  159.   end
  160.   local HealthNum = tonumber("4")-string.len(Health)
  161.   local Spaces = string.rep(" ", HealthNum)
  162.   if FlashHP == true then
  163.     MapView[3] = "Health:     O"..MapView[3]
  164.   else
  165.     MapView[3] = "Health: "..Health..Spaces.."O"..MapView[3]
  166.   end
  167.   MapView[4] = string.sub(MapView[4], 14, string.len(MapView[4]))
  168.   MapView[4] = "OOOOOOOOOOOOO"..MapView[4]
  169.   Draw = os.startTimer(0.15)
  170. end
  171.  
  172. function CreateLaser(x, y, dir)
  173.   if CheckMapView(x, y) ~= "O" then
  174.     Laser[#Laser+1] = Num(x)..Num(y)..dir
  175.     EditMapView(x, y, Lasers[dir])
  176.   end
  177.   Cooldown = 5
  178. end
  179.  
  180. function CreatePulse(x, y, dir)
  181.   if CheckMapView(x, y) ~= "O" then
  182.     Pulse[#Pulse+1] = Num(x)..Num(y)..dir.."1".."1"
  183.     EditMapView(x, y, Pulsing[dir])
  184.   end
  185.   CooldownPulse = 30
  186. end
  187.  
  188. function Num(num)
  189.   if string.len(num) == 1 then
  190.     return "0"..num
  191.   else
  192.     return num
  193.   end
  194. end
  195.  
  196. function LaserMove()
  197.   for n=1,#Laser do
  198.     if Laser[n] then
  199.       local LaserX = tonumber(string.sub(Laser[n], 1, 2))
  200.       local LaserY = tonumber(string.sub(Laser[n], 3, 4))
  201.       local LaserDir = tonumber(string.sub(Laser[n], 5, 5))
  202.       deleteMapView(LaserX, LaserY)
  203.       if LaserDir == 1 then
  204.         LaserX = LaserX-1
  205.       elseif LaserDir == 2 then
  206.         LaserX = LaserX+1
  207.       elseif LaserDir == 3 then
  208.         LaserY = LaserY-1
  209.       elseif LaserDir == 4 then
  210.         LaserY = LaserY+1
  211.       else
  212.         error("LaserDir errored")
  213.       end
  214.       if CheckMapView(LaserX, LaserY) == " " then
  215.         EditMapView(LaserX, LaserY, Lasers[LaserDir])
  216.         Laser[n] = Num(LaserX)..Num(LaserY)..LaserDir
  217.       else
  218.         table.remove(Laser, n)
  219.         n = n-1
  220.       end
  221.     end
  222.   end
  223. end
  224.  
  225. function PulseMove()
  226.   for n=1,#Pulse do
  227.     if Pulse[n] then
  228.       local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  229.       local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  230.       local PulseDir = tonumber(string.sub(Pulse[n], 5, 5))
  231.       local PulseState = tonumber(string.sub(Pulse[n], 6, 6))
  232.       local Speed = tonumber(string.sub(Pulse[n], 7, 7))
  233.       deleteMapView(PulseX, PulseY)
  234.       local Sparex = PulseX
  235.       local Sparey = PulseY
  236.       if Speed == 1 then
  237.         if PulseDir == 1 then
  238.           PulseX = PulseX-1
  239.         elseif PulseDir == 2 then
  240.           PulseX = PulseX+1
  241.         elseif PulseDir == 3 then
  242.           PulseY = PulseY-1
  243.         elseif PulseDir == 4 then
  244.           PulseY = PulseY+1
  245.         else
  246.           error("LaserDir errored")
  247.         end
  248.         Speed = 0
  249.       else
  250.         Speed = 1
  251.       end
  252.       if PulseState == 4 then
  253.         PulseState = 1
  254.       else
  255.         PulseState = PulseState+1
  256.       end
  257.       if CheckMap(PulseX, PulseY) == " " then
  258.         EditMapView(PulseX, PulseY, Pulsing[PulseState])
  259.         Pulse[n] = Num(PulseX)..Num(PulseY)..PulseDir..PulseState..Speed
  260.       else
  261.         PulseExplode(Sparex, Sparey)
  262.         table.remove(Pulse, n)
  263.         n = n-1
  264.       end
  265.     end
  266.   end
  267. end
  268.  
  269. function PulseExplode(x, y, Spread)
  270.   if not Spread then Spread = "3" end
  271.   local Nope = 5
  272.   for n=1,#PulseExplodes do
  273.     if PulseExplodes[n] then
  274.       local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
  275.       local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
  276.       if PulseX == x and PulseY == y then -- Matches another explosion. Dont make it! Abort abort!
  277.         Nope = 4
  278.       end
  279.     end
  280.   end
  281.   if Nope == 5 then -- Nothing is here.. right?
  282.     if CheckMapView(x, y) ~= "O" then
  283.       PulseExplodes[#PulseExplodes+1] = Num(x)..Num(y).."3"..Spread
  284.       EditMapView(x, y, "o")
  285.     end
  286.   end
  287. end
  288. fs.delete("Log")
  289. function Logger(loggy)
  290.   p = io.open("Log", "a")
  291.   p:write(loggy.."\n")
  292.   p:close()
  293. end
  294.  
  295. function UpdateExplode()
  296.   for n=1,#PulseExplodes do
  297.     if PulseExplodes[n] then
  298.       local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
  299.       local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
  300.       local PulseStatus = tonumber(string.sub(PulseExplodes[n], 5, 5))
  301.       local Spread = tonumber(string.sub(PulseExplodes[n], 6, 6))
  302.       if PulseStatus < 1 then -- How long it has left in the world
  303.         deleteMapView(PulseX, PulseY)
  304.         table.remove(PulseExplodes, n)
  305.       elseif Spread ~= 0 then
  306.         PulseExplode(PulseX+1, PulseY, Spread-1)
  307.         PulseExplode(PulseX-1, PulseY, Spread-1)
  308.         PulseExplode(PulseX, PulseY+1, Spread-1)
  309.         PulseExplode(PulseX, PulseY-1, Spread-1)
  310.       end
  311.       if PulseExplodes[n] then
  312.         if PulseStatus ~= 0 then
  313.         PulseExplodes[n] = Num(PulseX)..Num(PulseY)..(PulseStatus-1)..(Spread-1)
  314.       end
  315.       end
  316.     end
  317.   end
  318. end
  319.  
  320. function deleteMapView(x,y)
  321.   local First = string.sub(MapView[y], 1, x-1)
  322.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  323.   MapView[y] = First.." "..Second
  324. end
  325.  
  326. function deleteMap(x,y)
  327.   local First = string.sub(MapView[y], 1, x-1)
  328.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  329.   MapView[y] = First.." "..Second
  330. end
  331.  
  332. function Direction(xory, dir)
  333.   if xory == "x" then
  334.     if dir == 1 then --Left
  335.       return -1
  336.     elseif dir == 2 then --Right
  337.       return 1
  338.     elseif dir == 3 or dir == 4 then
  339.       return 0
  340.     else
  341.       error("funtion Direction was handled incorrectly")
  342.     end
  343.   elseif xory == "y" then
  344.     if dir == 3 then --Up
  345.       return -1
  346.     elseif dir == 4 then --Down
  347.       return 1
  348.     elseif dir == 1 or dir == 2 then
  349.       return 0
  350.     else
  351.       error("funtion Direction was handled incorrectly")
  352.     end
  353.   else
  354.     error("You need to use x or y!!!! in function direction!")
  355.   end
  356. end
  357.  
  358. function reDraw()
  359.   clear()
  360.   for n=1,#MapView do
  361.     term.setCursorPos(1, n)
  362.     term.write(MapView[n])
  363.   end
  364.   term.setCursorPos(PlayerX, PlayerY)
  365.   term.write(Player[PlayerDir])
  366. end
  367.  
  368. function CheckMap(x, y)
  369.   if Map[y] then
  370.     local MapString = Map[y]
  371.     MapString = string.sub(MapString, x, x)
  372.     return MapString
  373.   else
  374.     return "O"
  375.   end
  376. end
  377.  
  378. function CheckMapView(x, y)
  379.   if MapView[y] then
  380.     local Lens = string.len(MapView[y])
  381.     if x > 0 and x < Lens then
  382.       local MapString = MapView[y]
  383.       MapString = string.sub(MapString, x, x)
  384.       return MapString
  385.     else
  386.       return "O"
  387.     end
  388.   end
  389.   return "O"
  390. end
  391.  
  392. function EditMapView(x, y, Get)
  393.   local First = string.sub(MapView[y], 1, x-1)
  394.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  395.   MapView[y] = First..Get..Second
  396. end
  397.  
  398. function EditMap(x, y, Get)
  399.   local First = string.sub(Map[y], 1, x-1)
  400.   local Second = string.sub(Map[y], x+1, string.len(Map[y]))
  401.   Map[y] = First..Get..Second
  402. end
  403.  
  404. function MovePlayer()
  405.   Tick()
  406.   while true do
  407.     Events = {os.pullEvent()}
  408.     if Events[1] == "key" then
  409.       if Events[2] == 200 or Events[2] == 17 then -- Up
  410.         PlayerDir = 3
  411.         if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
  412.           PlayerY = PlayerY+Direction("y", PlayerDir)
  413.           CheckUnder()
  414.         end
  415.         reDraw()
  416.       elseif Events[2] == 208 or Events[2] == 31 then -- Down
  417.         PlayerDir = 4
  418.         if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
  419.           PlayerY = PlayerY+Direction("y", PlayerDir)
  420.           CheckUnder()
  421.         end
  422.         reDraw()
  423.       elseif Events[2] == 203 or Events[2] == 30 then -- Left
  424.         PlayerDir = 1
  425.         if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
  426.           PlayerX = PlayerX+Direction("x", PlayerDir)
  427.           CheckUnder()
  428.         end
  429.         reDraw()
  430.       elseif Events[2] == 205 or Events[2] == 32 then -- Right
  431.         PlayerDir = 2
  432.         if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
  433.           PlayerX = PlayerX+Direction("x", PlayerDir)
  434.           CheckUnder()
  435.         end
  436.         reDraw()
  437.       elseif Events[2] == 57 then --Spacebar
  438.         if Cooldown == 0 then
  439.           CreateLaser(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
  440.         end
  441.       elseif Events[2] == 45 then -- x button
  442.         if CooldownPulse == 0 then
  443.           CreatePulse(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
  444.         end
  445.       end
  446.     elseif Events[1] == "timer" and Events[2] == Ticker then --Tick
  447.       Tick()
  448.     elseif Events[1] == "timer" and Events[2] == Draw then
  449.       StatusBox("Nothing", "Ignore")
  450.     end
  451.   end
  452. end
  453. Load()
  454. MovePlayer()
Advertisement
Add Comment
Please, Sign In to add comment