jay5476

pong

Sep 9th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. os.loadAPI("game")
  2. mode = {...}
  3. x = -1
  4. y = -1
  5. num2 = 0
  6. w,h = term.getSize()
  7. ball = game.createChar(26,8+math.random(4),"O")
  8. pad1 = game.createChar(1,10,"#")
  9. pad2 = game.createChar(w,10,"#")
  10.  
  11. function bounce()
  12. time = 0.15
  13.  
  14. sleep(time)
  15. cx,cy = ball:getPos() cx2,cy2 = pad1:getPos() cx3,cy3 = pad2:getPos()
  16. term.clear()
  17. for i = -2,2 do
  18. num1 = cy2 + i
  19. num2 = cy3 + i
  20. term.setCursorPos(1,num1)
  21. write("#")
  22. term.setCursorPos(w,num2)
  23. write("#")
  24. ball:draw()
  25. if cy == 1 then y = 1 end
  26. if cy == h then y = -1 end
  27. if cx == 2 and cy == num1 then x = 1 elseif cx == 1 then error("Right Side Won!",0) end
  28. if cx == w-1 and cy == num2 then x = -1 elseif cx == w then error("Left Side Won!",0) end
  29.  
  30. end
  31. ball:move(x,y)
  32. if mode[1] == "single" then pad2:move(w,y) end
  33. end
  34.  
  35. function move()
  36. _, k = os.pullEvent("key")
  37. cx1,cy1 = pad1:getPos() cx2,cy2 = pad2:getPos()
  38. if k == 31 and cy1 < h-2 then pad1:move(0,1) end
  39. if k == 17 and cy1 > 3 then pad1:move(0,-1) end
  40. if mode[1] ~= "single" then if k == 200 and cy2 > 3 then pad2:move(0,-1) end end
  41. if mode[1] ~= "single" then if k == 208 and cy2 < h - 2 then pad2:move(0,1) end end
  42.  
  43. end
  44. while not pause do
  45. parallel.waitForAny(bounce,move)
  46. end
Advertisement
Add Comment
Please, Sign In to add comment