Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RPG Game --
- w,h = term.getSize()
- clr = function() term.clear() end
- cp = function(x,y) return term.setCursorPos(x,y) end
- setText = function(col) return term.setTextColor(col) end
- setBack = function(col) return term.setBackgroundColor(col) end
- debug, running, run = false, true, false
- player_x, player_y, direction = math.ceil(w/2), math.ceil(h/2), 1
- gamemode = 0
- console, consoleText = false, ""
- godmode = false
- ai = {}
- speed = 1
- function colDetection()
- if godmode then return 0 end
- for i=1, #ai do
- if player_x == ai[i][1] and player_y == ai[i][2] then return 1 end
- --cp(ai[i][1], ai[i][2]) write("O") --<< DEBUG COLLISION
- end
- return 0
- end
- function drawAI()
- for i=1, #ai do
- if ai[i] ~= nil then
- paintutils.drawPixel(ai[i][1], ai[i][2], colors.black)
- if math.random(1,2) == 1 then -- up and down
- if math.random(1,2) == 1 then
- ai[i][1] = ai[i][1] + 1
- else
- ai[i][1] = ai[i][1] - 1
- end
- else -- left or right
- if math.random(1,2) == 1 then
- ai[i][2] = ai[i][2] + 1
- else
- ai[i][2] = ai[i][2] - 1
- end
- end
- if ai[i][1] > w then ai[i][1] = 1 end
- if ai[i][1] < 1 then ai[i][1] = w end
- if ai[i][2] > h-1 then ai[i][2] = 1 end
- if ai[i][2] < 1 then ai[i][2] = h-1 end
- paintutils.drawPixel(ai[i][1], ai[i][2], colors.red)
- end
- end
- updateBar()
- end
- function AIscript()
- if gamemode == 0 then
- while true do
- if #ai == 0 then
- sleep(3)
- for i=1, 20 do
- ai[i] = {}
- ai[i][1], ai[i][2] = math.random(1,w), math.random(1,h-1)
- end
- end
- drawAI()
- if colDetection() == 1 then run = false break end
- sleep(speed)
- end
- end
- end
- function attack()
- moves = {
- function() -- 3x3
- if direction == 1 then
- --
- elseif direction == 2 then
- --
- elseif direction == 3 then
- --
- else
- --
- end
- end,
- }
- end
- function PLAYERscript()
- if gamemode == 0 then
- function drawPlayer()
- local tab = {string.char(24), string.char(26), string.char(25), string.char(27)}
- --paintutils.drawPixel(player_x,player_y,colors.white)
- setText(colors.gray) setBack(colors.white) cp(player_x,player_y)
- write(tab[direction])
- end
- local update = true
- while run do
- if update then
- update = false
- drawPlayer()
- if colDetection() == 1 then run = false break end
- end
- a,i = os.pullEvent("key")
- if not console then
- if i == keys.w or i == keys.up then
- 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
- elseif i == keys.d or i == keys.right then
- 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
- elseif i == keys.s or i == keys.down then
- 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
- elseif i == keys.a or i == keys.left then
- 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
- elseif i == keys.e or i == keys.space then
- attack()
- elseif i == keys.q then
- run = false
- end
- end
- end
- end
- end
- function displayConsole()
- if console then
- paintutils.drawLine(1,1,w,1,colors.gray) setText(colors.lightGray)
- cp(1,1) write("> ")
- setText(colors.white) write(consoleText)
- else
- paintutils.drawLine(1,1,w,1,colors.black)
- end
- end
- function toggleConsole()
- consoleText = ""
- console = not console
- displayConsole()
- sleep(.2)
- end
- function CONSOLEscript() -- for toggling
- while true do
- a,i = os.pullEvent("char")
- if i == "`" then
- toggleConsole()
- end
- end
- end
- function runCMD()
- cmd = {
- exit = function()
- run = false
- end,
- gm = function()
- godmode = not godmode
- end,
- }
- if cmd[consoleText] ~= nil then cmd[consoleText]() end
- end
- function CONSOLEaction() -- for typing
- while true do
- if console then
- while console do
- a,i = os.pullEvent()
- sleep(.0001)
- if a == "key" then
- if i == keys.enter then
- runCMD()
- toggleConsole()
- elseif i == keys.backspace then
- consoleText = string.sub(consoleText, 1, -2) displayConsole()
- end
- elseif a == "char" then
- if i == "`" then
- --sleep(1)
- else
- if string.len(consoleText) < w-5 then consoleText = consoleText..i end displayConsole()
- end
- end
- end
- else
- sleep(1)
- end
- end
- end
- function CONSOLEupdate() -- for display
- while true do
- repeat sleep(1) until console == true
- repeat displayConsole() sleep(.2) until console == false
- end
- end
- function drawBar()
- paintutils.drawLine(1,h,w,h,colors.gray)
- end
- function updateBar()
- cp(1,h) setText(colors.lightGray) setBack(colors.gray) write(#ai.."/20")
- end
- function rungame()
- run = true
- w,h = term.getSize() -- for updating reasons
- player_x, player_y = math.ceil(w/2),math.ceil(h/2) -- same for updating reasons
- drawBar()
- if gamemode == 0 then
- parallel.waitForAny(AIscript, PLAYERscript, CONSOLEscript, CONSOLEaction, CONSOLEupdate)
- end
- --clr() cp(1,1) -- Terminates
- end
- function main()
- rungame()
- end
- clr()
- main()
- setBack(colors.black) setText(colors.white)
- clr() cp(1,1) print("Exited! :)") sleep(.2)
Advertisement
Add Comment
Please, Sign In to add comment