JustDoesGames

game thing

Apr 15th, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.38 KB | None | 0 0
  1. -- RPG Game --
  2.  
  3. w,h = term.getSize()
  4. clr = function() term.clear() end
  5. cp = function(x,y) return term.setCursorPos(x,y) end
  6. setText = function(col) return term.setTextColor(col) end
  7. setBack = function(col) return term.setBackgroundColor(col) end
  8.  
  9. debug, running, run = false, true, false
  10. player_x, player_y, direction = math.ceil(w/2), math.ceil(h/2), 1
  11. gamemode = 0
  12. console, consoleText = false, ""
  13. godmode = false
  14. ai = {}
  15. speed = 1
  16.  
  17. function colDetection()
  18.     if godmode then return 0 end
  19.     for i=1, #ai do
  20.         if player_x == ai[i][1] and player_y == ai[i][2] then return 1 end
  21.         --cp(ai[i][1], ai[i][2]) write("O") --<< DEBUG COLLISION
  22.     end
  23.     return 0
  24. end
  25.  
  26. function drawAI()
  27.     for i=1, #ai do
  28.         if ai[i] ~= nil then
  29.             paintutils.drawPixel(ai[i][1], ai[i][2], colors.black)
  30.            
  31.             if math.random(1,2) == 1 then -- up and down
  32.                 if math.random(1,2) == 1 then
  33.                     ai[i][1] = ai[i][1] + 1
  34.                 else
  35.                     ai[i][1] = ai[i][1] - 1
  36.                 end
  37.             else -- left or right
  38.                 if math.random(1,2) == 1 then
  39.                     ai[i][2] = ai[i][2] + 1
  40.                 else
  41.                     ai[i][2] = ai[i][2] - 1
  42.                 end
  43.             end
  44.             if ai[i][1] > w then ai[i][1] = 1 end
  45.             if ai[i][1] < 1 then ai[i][1] = w end
  46.             if ai[i][2] > h-1 then ai[i][2] = 1 end
  47.             if ai[i][2] < 1 then ai[i][2] = h-1 end
  48.            
  49.             paintutils.drawPixel(ai[i][1], ai[i][2], colors.red)
  50.         end
  51.     end
  52.     updateBar()
  53. end
  54.  
  55. function AIscript()
  56.     if gamemode == 0 then
  57.         while true do
  58.             if #ai == 0 then
  59.                 sleep(3)
  60.                 for i=1, 20 do
  61.                     ai[i] = {}
  62.                     ai[i][1], ai[i][2] = math.random(1,w), math.random(1,h-1)
  63.                 end
  64.             end
  65.             drawAI()
  66.             if colDetection() == 1 then run = false break end
  67.             sleep(speed)
  68.         end
  69.     end
  70. end
  71.  
  72. function attack()
  73.     moves = {
  74.         function() -- 3x3
  75.             if direction == 1 then
  76.                 --
  77.             elseif direction == 2 then
  78.                 --
  79.             elseif direction == 3 then
  80.                 --
  81.             else
  82.                 --
  83.             end
  84.         end,
  85.     }
  86. end
  87.  
  88. function PLAYERscript()
  89.     if gamemode == 0 then
  90.         function drawPlayer()
  91.             local tab = {string.char(24), string.char(26), string.char(25), string.char(27)}
  92.             --paintutils.drawPixel(player_x,player_y,colors.white)
  93.             setText(colors.gray) setBack(colors.white) cp(player_x,player_y)
  94.             write(tab[direction])
  95.         end
  96.         local update = true
  97.         while run do
  98.             if update then
  99.                 update = false
  100.                 drawPlayer()
  101.                 if colDetection() == 1 then run = false break end
  102.             end
  103.             a,i = os.pullEvent("key")
  104.             if not console then
  105.                 if i == keys.w or i == keys.up then
  106.                     paintutils.drawPixel(player_x,player_y,colors.black) if player_y == 1 then player_y = h-1 else player_y = player_y - 1 end direction = 1 update = true
  107.                 elseif i == keys.d or i == keys.right then
  108.                     paintutils.drawPixel(player_x,player_y,colors.black) if player_x == w then player_x = 1 else player_x = player_x + 1 end direction = 2 update = true
  109.                 elseif i == keys.s or i == keys.down then
  110.                     paintutils.drawPixel(player_x,player_y,colors.black) if player_y == h-1 then player_y = 1 else player_y = player_y + 1 end direction = 3 update = true
  111.                 elseif i == keys.a or i == keys.left then
  112.                     paintutils.drawPixel(player_x,player_y,colors.black) if player_x == 1 then player_x = w else player_x = player_x - 1 end direction = 4 update = true
  113.                 elseif i == keys.e or i == keys.space then
  114.                     attack()
  115.                 elseif i == keys.q then
  116.                     run = false
  117.                 end
  118.             end
  119.         end
  120.     end
  121. end
  122.  
  123. function displayConsole()
  124.     if console then
  125.         paintutils.drawLine(1,1,w,1,colors.gray) setText(colors.lightGray)
  126.         cp(1,1) write("> ")
  127.         setText(colors.white) write(consoleText)
  128.     else
  129.         paintutils.drawLine(1,1,w,1,colors.black)
  130.     end
  131. end
  132.  
  133. function toggleConsole()
  134.     consoleText = ""
  135.     console = not console
  136.     displayConsole()
  137.     sleep(.2)
  138. end
  139.  
  140. function CONSOLEscript() -- for toggling
  141.     while true do
  142.         a,i = os.pullEvent("char")
  143.         if i == "`" then
  144.             toggleConsole()
  145.         end
  146.     end
  147. end
  148.  
  149. function runCMD()
  150.     cmd = {
  151.         exit = function()
  152.             run = false
  153.         end,
  154.         gm = function()
  155.             godmode = not godmode
  156.         end,
  157.     }
  158.     if cmd[consoleText] ~= nil then cmd[consoleText]() end
  159. end
  160.  
  161. function CONSOLEaction() -- for typing
  162.     while true do
  163.         if console then
  164.             while console do
  165.                 a,i = os.pullEvent()
  166.                 sleep(.0001)
  167.                 if a == "key" then
  168.                     if i == keys.enter then
  169.                         runCMD()
  170.                         toggleConsole()
  171.                     elseif i == keys.backspace then
  172.                         consoleText = string.sub(consoleText, 1, -2) displayConsole()
  173.                     end
  174.                 elseif a == "char" then
  175.                     if i == "`" then
  176.                         --sleep(1)
  177.                     else
  178.                         if string.len(consoleText) < w-5 then consoleText = consoleText..i end displayConsole()
  179.                     end
  180.                 end
  181.             end
  182.         else
  183.             sleep(1)
  184.         end
  185.     end
  186. end
  187.  
  188. function CONSOLEupdate() -- for display
  189.     while true do
  190.         repeat sleep(1) until console == true
  191.         repeat displayConsole() sleep(.2) until console == false
  192.     end
  193. end
  194.  
  195. function drawBar()
  196.     paintutils.drawLine(1,h,w,h,colors.gray)
  197. end
  198.  
  199. function updateBar()
  200.     cp(1,h) setText(colors.lightGray) setBack(colors.gray) write(#ai.."/20")
  201. end
  202.  
  203. function rungame()
  204.     run = true
  205.     w,h = term.getSize() -- for updating reasons
  206.     player_x, player_y = math.ceil(w/2),math.ceil(h/2) -- same for updating reasons
  207.     drawBar()
  208.     if gamemode == 0 then
  209.         parallel.waitForAny(AIscript, PLAYERscript, CONSOLEscript, CONSOLEaction, CONSOLEupdate)
  210.     end
  211.     --clr() cp(1,1) -- Terminates
  212. end
  213.  
  214. function main()
  215.     rungame()
  216. end
  217.  
  218. clr()
  219. main()
  220. setBack(colors.black) setText(colors.white)
  221. clr() cp(1,1) print("Exited! :)") sleep(.2)
Advertisement
Add Comment
Please, Sign In to add comment