Advertisement
theobjop

LUA Help

Apr 24th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3.  
  4. local currentX = 25
  5. local currentY = 9
  6. local currentX2 = 24
  7. local currentY2 = 8
  8. local sprite = "1"
  9. local sprite2 = "2"
  10. local tries = "0"
  11. local player = 0
  12.  
  13. function drawSprite()
  14.     term.clear()
  15.     term.setCursorPos(currentX,currentY)
  16.     write(sprite)
  17.     movement()
  18. end
  19.  
  20. function drawSpriteMulti()
  21.     term.clear()
  22.     term.setCursorPos(currentX,currentY)
  23.     write(sprite)
  24.     term.setCursorPos(currentX2,currentY2)
  25.     write(sprite2)
  26.     if player == 1 then
  27.         movementMulti()
  28.     elseif player == 2 then
  29.         movementMulti2()
  30.     end
  31. end
  32.  
  33. function connect()
  34.     print "1:) Host"
  35.     print "2:) Join"
  36.     input = read()
  37.     if input == "1" then
  38.         player = 1
  39.         print "OK... Waiting for IP to join"
  40.         while true do
  41.             id, msg = rednet.receive()
  42.             sleep(.1)
  43.             tries = tries + 1
  44.             if tries > 1000 then
  45.                 print "Timed out"
  46.             elseif msg == "connect" then
  47.                 local player2 = id
  48.                 drawSpriteMulti()
  49.             end
  50.         end
  51.     elseif input == "2" then
  52.         player = 2
  53.         write "Enter the ip of who you want to connect to: "
  54.         local c = tonumber(read())
  55.         while true do
  56.             sleep(.1)
  57.             rednet.send(c, "connect")
  58.             sleep(1)
  59.             drawSpriteMulti()
  60.         end
  61.     end
  62. end
  63.  
  64. function open()
  65.     rednet.open("top")
  66.     rednet.open("back")
  67.     rednet.open("left")
  68.     rednet.open("right")
  69.     rednet.open("left")
  70. end
  71.  
  72. function movementMulti()
  73.     while true do
  74.         event, key = os.pullEvent()
  75.         if event == "key" and key == 17 and currentY >= 1 then
  76.             currentY = currentY - 1
  77.             rednet.send(player2, "up")
  78.             drawSpriteMulti()
  79.         elseif event == "key" and key == 31 and currentY <= 18 then
  80.             currentY = currentY + 1
  81.             rednet.send(player2, "down")
  82.             drawSpriteMulti()
  83.         elseif event == "key" and key == 30 and currentX >= 1 then
  84.             currentX = currentX - 1
  85.             rednet.send(player2, "left")
  86.             drawSpriteMulti()
  87.         elseif event == "key" and key == 32 and currentX <= 50 then
  88.             currentX = currentX + 1
  89.             rednet.send(player2, "right")
  90.             drawSpriteMulti()
  91.         end
  92.     end
  93.        
  94.     while true do
  95.         id, command = rednet.receive()
  96.         if id == player2 and command == "up" then
  97.             currentY2 = currentY2 - 1
  98.             drawSpriteMulti()
  99.         elseif id == player2 and command == "down" then
  100.             currentY2 = currentY2 + 1
  101.             drawSpriteMulti()
  102.         elseif id == player2 and command == "left" then
  103.             currentX2 = currentX2 - 1
  104.             drawSpriteMulti()
  105.         elseif id == player2 and command == "right" then
  106.             currentX2 = currentX2 + 1
  107.             drawSpriteMulti()
  108.         else
  109.             drawSpriteMulti()
  110.         end
  111.     end
  112. end
  113.  
  114. function movementMulti2()
  115.     while true do
  116.         event, key = os.pullEvent()
  117.         if event == "key" and key == 17 then
  118.             currentY2 = currentY2 - 1
  119.             rednet.send(c, "up")
  120.             drawSpriteMulti()
  121.         elseif event == "key" and key == 31 then
  122.             currentY2 = currentY2 + 1
  123.             rednet.send(c, "down")
  124.             drawSpriteMulti()
  125.         elseif event == "key" and key == 30 then
  126.             currentX2 = currentX2 - 1
  127.             rednet.send(c, "left")
  128.             drawSpriteMulti()
  129.         elseif event == "key" and key == 32 then
  130.             currentX2 = currentX2 + 1
  131.             rednet.send(c, "right")
  132.             drawSpriteMulti()
  133.         end
  134.     end
  135.    
  136.     while true do
  137.         id, command = rednet.receive()
  138.         if id == c and command == "up" then
  139.             currentY = currentY - 1
  140.             drawSpriteMulti()
  141.         elseif id == c and command == "down" then
  142.             currentY = currentY + 1
  143.             drawSpriteMulti()
  144.         elseif id == c and command == "left" then
  145.             currentX = currentX - 1
  146.             drawSpriteMulti()
  147.         elseif id == c and command == "right" then
  148.             currentX = currentX + 1
  149.             drawSpriteMulti()
  150.         end
  151.     end
  152. end
  153.  
  154. function movement()
  155.     event, key = os.pullEvent()
  156.     if event == "key" and key == 17 and currentY >= 1 then
  157.         currentY = currentY - 1
  158.         drawSprite()
  159.     elseif event == "key" and key == 31 and currentY <= 18 then
  160.         currentY = currentY + 1
  161.         drawSprite()
  162.     elseif event == "key" and key == 30 and currentX >= 1 then
  163.         currentX = currentX - 1
  164.         drawSprite()
  165.     elseif event == "key" and key == 32 and currentX <= 50 then
  166.         currentX = currentX + 1
  167.         drawSprite()
  168.     end
  169. end
  170.  
  171. open()
  172. print "How would you like to play?"
  173. print "1:) Singleplayer (boring)"
  174. print "2:) Multiplayer (EPIC)"
  175. input = read()
  176. if input == "1" then
  177.     drawSprite()
  178. elseif input == "2" then
  179.     connect()
  180. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement