Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- local isServer = true
- local player1 = {}
- local player2 = {}
- local w,h = term.getSize()
- player1.snake = {}
- player1.size = 3
- player2.snake = {}
- local apples = {}
- local numApples = 2
- function spawn()
- local w,h = term.getSize()
- player1.snake[1] = {x=math.floor(w/2)+4,y=math.floor(h/2)}
- player2.snake[1] = {x=math.floor(w/2)-4,y=math.floor(h/2)}
- end
- function draw()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setBackgroundColor(colors.red)
- for i,apple in pairs(apples) do
- term.setCursorPos(apple.x,apple.y)
- term.write(" ")
- end
- term.setBackgroundColor(colors.green)
- for _,pos in pairs(player1.snake) do
- term.setCursorPos(pos.x,pos.y)
- term.write(" ")
- end
- term.setBackgroundColor(colors.orange)
- for _,pos in pairs(player2.snake) do
- term.setCursorPos(pos.x,pos.y)
- term.write(" ")
- end
- end
- function testApple()
- if isServer then
- local sync = false
- if numApples > #apples then
- table.insert(apples,{x = math.random(1,w),y = math.random(1,h)})
- sync = true
- end
- for i,apple in pairs(apples) do
- if apple.x == player1.snake[1].x and apple.y == player1.snake[1].y then
- table.remove(apples,i)
- player1.size = player1.size+1
- sync = true
- end
- end
- if sync then
- rednet.broadcast({id="apples",data=apples}, "Snake: "..tostring(isServer))
- end
- else
- for i,apple in pairs(apples) do
- if apple.x == player1.snake[1].x and apple.y == player1.snake[1].y then
- rednet.broadcast({id="removeapple",data=i}, "Snake: "..tostring(isServer))
- player1.size = player1.size+1
- end
- end
- end
- end
- function move(dir)
- if dir == 0 then
- local cur = player1.snake[1]
- local newy = cur.y-1
- if newy == 0 then error("Out of bounds") end
- table.insert(player1.snake,1,{x=cur.x,y=newy})
- elseif dir == 2 then
- local cur = player1.snake[1]
- local newy = cur.y+1
- if newy > h then error("Out of bounds") end
- table.insert(player1.snake,1,{x=cur.x,y=newy})
- elseif dir == 1 then
- local cur = player1.snake[1]
- local newx = cur.x+1
- if newx > w then error("Out of bounds") end
- table.insert(player1.snake,1,{x=newx,y=cur.y})
- else
- local cur = player1.snake[1]
- local newx = cur.x-1
- if newx == 0 then error("Out of bounds") end
- table.insert(player1.snake,1,{x=newx,y=cur.y})
- end
- if #player1.snake > player1.size then
- player1.snake[#player1.snake]=nil
- end
- for i,pos in pairs(player1.snake) do
- if i ~= 1 then
- if pos.x == player1.snake[1].x and pos.y == player1.snake[1].y then
- error("Ded")
- end
- end
- end
- for i,pos in pairs(player2.snake) do
- if pos.x == player1.snake[1].x and pos.y == player1.snake[1].y then
- error("Ded")
- end
- end
- rednet.broadcast({id="snake",data=player1.snake}, "Snake: "..tostring(isServer))
- end
- local curDir = 0
- function getKey()
- print("Running")
- while true do
- local event,key = os.pullEvent("key")
- if key == keys.w then
- curDir = 0
- elseif key == keys.d then
- curDir = 1
- elseif key == keys.s then
- curDir = 2
- elseif key == keys.a then
- curDir = 3
- end
- end
- end
- function recieveSnake()
- while true do
- local senderId, message, protocol = rednet.receive("Snake: "..tostring(not isServer))
- if message.id == "snake" then
- player2.snake = message.data
- elseif message.id == "apples" then
- apples = message.data
- elseif isServer == true and message.id == "removeapple" then
- table.remove(apples,i)
- rednet.broadcast({id="apples",data=apples}, "Snake: "..tostring(isServer))
- end
- end
- end
- function ServerDoTurn()
- while true do
- os.sleep(0.1)
- testApple()
- move(curDir)
- rednet.broadcast(1,"Snake: TURN")
- draw()
- end
- end
- function doClientTurn()
- while true do
- local senderId, message, protocol = rednet.receive("Snake: TURN")
- testApple()
- move(curDir)
- draw()
- end
- end
- function start()
- spawn()
- local func1 = getKey
- local func2 = recieveSnake
- local func3 = nil
- if isServer then
- func3 = ServerDoTurn
- else
- func3 = doClientTurn
- end
- parallel.waitForAny(func1,func2,func3)
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment