Cobra_Tomtrein-temp-

Untitled

Oct 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. local isServer = true
  4.  
  5. local player1 = {}
  6. local player2 = {}
  7.  
  8. local w,h = term.getSize()
  9.  
  10.  
  11. player1.snake = {}
  12. player1.size = 3
  13. player2.snake = {}
  14.  
  15.  
  16. local apples = {}
  17.  
  18. local numApples = 2
  19.  
  20. function spawn()
  21. local w,h = term.getSize()
  22. player1.snake[1] = {x=math.floor(w/2)+4,y=math.floor(h/2)}
  23. player2.snake[1] = {x=math.floor(w/2)-4,y=math.floor(h/2)}
  24. end
  25.  
  26. function draw()
  27. term.setBackgroundColor(colors.black)
  28. term.clear()
  29. term.setBackgroundColor(colors.red)
  30. for i,apple in pairs(apples) do
  31. term.setCursorPos(apple.x,apple.y)
  32. term.write(" ")
  33. end
  34. term.setBackgroundColor(colors.green)
  35. for _,pos in pairs(player1.snake) do
  36. term.setCursorPos(pos.x,pos.y)
  37. term.write(" ")
  38. end
  39. term.setBackgroundColor(colors.orange)
  40. for _,pos in pairs(player2.snake) do
  41. term.setCursorPos(pos.x,pos.y)
  42. term.write(" ")
  43. end
  44. end
  45.  
  46. function testApple()
  47. if isServer then
  48. local sync = false
  49. if numApples > #apples then
  50. table.insert(apples,{x = math.random(1,w),y = math.random(1,h)})
  51. sync = true
  52. end
  53. for i,apple in pairs(apples) do
  54. if apple.x == player1.snake[1].x and apple.y == player1.snake[1].y then
  55. table.remove(apples,i)
  56. player1.size = player1.size+1
  57. sync = true
  58. end
  59. end
  60. if sync then
  61. rednet.broadcast({id="apples",data=apples}, "Snake: "..tostring(isServer))
  62. end
  63. else
  64. for i,apple in pairs(apples) do
  65. if apple.x == player1.snake[1].x and apple.y == player1.snake[1].y then
  66. rednet.broadcast({id="removeapple",data=i}, "Snake: "..tostring(isServer))
  67. player1.size = player1.size+1
  68. end
  69. end
  70. end
  71. end
  72.  
  73. function move(dir)
  74. if dir == 0 then
  75. local cur = player1.snake[1]
  76. local newy = cur.y-1
  77. if newy == 0 then error("Out of bounds") end
  78. table.insert(player1.snake,1,{x=cur.x,y=newy})
  79. elseif dir == 2 then
  80. local cur = player1.snake[1]
  81. local newy = cur.y+1
  82. if newy > h then error("Out of bounds") end
  83. table.insert(player1.snake,1,{x=cur.x,y=newy})
  84. elseif dir == 1 then
  85. local cur = player1.snake[1]
  86. local newx = cur.x+1
  87. if newx > w then error("Out of bounds") end
  88. table.insert(player1.snake,1,{x=newx,y=cur.y})
  89. else
  90. local cur = player1.snake[1]
  91. local newx = cur.x-1
  92. if newx == 0 then error("Out of bounds") end
  93. table.insert(player1.snake,1,{x=newx,y=cur.y})
  94. end
  95. if #player1.snake > player1.size then
  96. player1.snake[#player1.snake]=nil
  97. end
  98. for i,pos in pairs(player1.snake) do
  99. if i ~= 1 then
  100. if pos.x == player1.snake[1].x and pos.y == player1.snake[1].y then
  101. error("Ded")
  102. end
  103. end
  104. end
  105. for i,pos in pairs(player2.snake) do
  106. if pos.x == player1.snake[1].x and pos.y == player1.snake[1].y then
  107. error("Ded")
  108. end
  109. end
  110. rednet.broadcast({id="snake",data=player1.snake}, "Snake: "..tostring(isServer))
  111. end
  112.  
  113. local curDir = 0
  114.  
  115. function getKey()
  116. print("Running")
  117. while true do
  118. local event,key = os.pullEvent("key")
  119. if key == keys.w then
  120. curDir = 0
  121. elseif key == keys.d then
  122. curDir = 1
  123. elseif key == keys.s then
  124. curDir = 2
  125. elseif key == keys.a then
  126. curDir = 3
  127. end
  128. end
  129. end
  130.  
  131. function recieveSnake()
  132. while true do
  133. local senderId, message, protocol = rednet.receive("Snake: "..tostring(not isServer))
  134. if message.id == "snake" then
  135. player2.snake = message.data
  136. elseif message.id == "apples" then
  137. apples = message.data
  138. elseif isServer == true and message.id == "removeapple" then
  139. table.remove(apples,i)
  140. rednet.broadcast({id="apples",data=apples}, "Snake: "..tostring(isServer))
  141. end
  142. end
  143. end
  144.  
  145. function ServerDoTurn()
  146. while true do
  147. os.sleep(0.1)
  148. testApple()
  149. move(curDir)
  150. rednet.broadcast(1,"Snake: TURN")
  151. draw()
  152. end
  153. end
  154.  
  155. function doClientTurn()
  156. while true do
  157. local senderId, message, protocol = rednet.receive("Snake: TURN")
  158. testApple()
  159. move(curDir)
  160. draw()
  161. end
  162. end
  163.  
  164.  
  165. function start()
  166. spawn()
  167. local func1 = getKey
  168. local func2 = recieveSnake
  169. local func3 = nil
  170. if isServer then
  171. func3 = ServerDoTurn
  172. else
  173. func3 = doClientTurn
  174. end
  175. parallel.waitForAny(func1,func2,func3)
  176. end
  177.  
  178. start()
Advertisement
Add Comment
Please, Sign In to add comment