Advertisement
bryceio

Computercraft Pong

Jan 27th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. local args = {...}
  2. term.clear()
  3. --width = 51
  4. --height = 19
  5. local ballXPos = 19
  6. local ballYPos = 9
  7. local lPos = 9
  8. local rPos = 9
  9. local lScore = 0
  10. local rScore = 0
  11. local ballDir = 1
  12. local ballAngle = 1
  13. local waits = 0
  14. local game = true
  15. local monitor = nil
  16. local textSize = 1
  17.  
  18. if args[1] == nil then
  19.     maxScore = 15
  20. else
  21.     maxScore = args[1]*1
  22. end
  23.  
  24. if args[2] == "mon" or args[2] == "monitor" then
  25.     textSize = args[3]
  26.     monitor = peripheral.find("monitor")
  27.     if not monitor then
  28.         term.clear()
  29.         term.setCursorPos(1, 1)
  30.         print("No monitor found")
  31.         error()
  32.     end
  33.     monitor.setTextScale(textSize)
  34. end
  35.  
  36. local drawField = function()
  37.     term.setCursorPos(1, 1)
  38.     term.write("Bryceio Industries: PONG =======================================")
  39.     term.setCursorPos(ballXPos, ballYPos)  
  40.     term.write("O")
  41.     for i = (lPos-2),(lPos+2) do
  42.         term.setCursorPos(4, i)
  43.         term.write("|")
  44.     end
  45.     for i = (rPos-2),(rPos+2) do
  46.         term.setCursorPos(36, i)   
  47.         term.write("|")
  48.     end
  49.     for i = 2, 18 do
  50.         term.setCursorPos(39, i)
  51.         term.write("||")
  52.     end
  53.     term.setCursorPos(41, 4)
  54.     print("Left:  "..lScore)
  55.     term.setCursorPos(41, 6)
  56.     print("Right: "..rScore)
  57.     term.setCursorPos(41, 9)
  58.     print("Max:   "..maxScore)
  59.     for i = 1, 51 do
  60.         term.setCursorPos(i, 19)
  61.         term.write("=")
  62.     end
  63. end
  64.  
  65. local controls = function()
  66.     keytimer = os.startTimer(0.4)
  67.     event, key = os.pullEvent()
  68.     if event == "key" then
  69.         sleep(0.2)
  70.         if key == 200 and (lPos > 4)then
  71.             lPos = lPos - 1
  72.         end
  73.         if key == 208 and (lPos < 16) then
  74.             lPos = lPos + 1
  75.         end
  76.     elseif event == "timer" and key == keytimer then
  77.     end
  78. end
  79.  
  80. local drawFieldMonitor = function()
  81.     monitor.setCursorPos(1, 1)
  82.     monitor.write("Bryceio Industries: PONG =======================================")
  83.     monitor.setCursorPos(ballXPos, ballYPos)
  84.     monitor.write("O")
  85.     for i = (lPos-2),(lPos+2) do
  86.         monitor.setCursorPos(4, i) 
  87.         monitor.write("|")
  88.     end
  89.     for i = (rPos-2),(rPos+2) do
  90.         monitor.setCursorPos(36, i)
  91.         monitor.write("|")
  92.     end
  93.     for i = 2, 18 do
  94.         monitor.setCursorPos(39, i)
  95.         monitor.write("||")
  96.     end
  97.     monitor.setCursorPos(41, 4)
  98.     monitor.write("Left:  "..lScore)
  99.     monitor.setCursorPos(41, 6)
  100.     monitor.write("Right: "..rScore)
  101.     monitor.setCursorPos(41, 9)
  102.     monitor.write("Max:   "..maxScore)
  103.     for i = 1, 51 do
  104.         monitor.setCursorPos(i, 19)
  105.         monitor.write("=")
  106.     end
  107. end
  108.  
  109. while game do
  110.     term.clear()
  111.     drawField()
  112.     if monitor ~= nil then
  113.         drawFieldMonitor() 
  114.     end
  115.     controls()
  116.     ballXPos = ballXPos + ballDir
  117.     ballYPos = ballYPos + ballAngle
  118.     if ballYPos > 18 or ballYPos < 2 then
  119.         ballYPos = ballYPos - ballAngle
  120.         ballAngle = ballAngle * -1
  121.     end
  122.     if ballXPos == 4 then
  123.         if ballYPos == lPos-3 then
  124.             ballDir =  1
  125.             ballAngle = -2
  126.             ballXPos = ballXPos + 1
  127.         elseif ballYPos >= lPos-2 and ballYPos <= lPos then
  128.             ballDir = 1
  129.             ballAngle = -1
  130.             ballXPos = ballXPos + 1
  131.         elseif ballYPos <= lPos+2 and ballYPos > lPos then
  132.             ballDir = 1
  133.             ballAngle = 1
  134.             ballXPos = ballXPos + 1
  135.         elseif ballYPos == lPos+3 then
  136.             ballDir = 1
  137.             ballAngle = 2
  138.             ballXPos = ballXPos + 1
  139.         end
  140.     elseif ballXPos == 36 then
  141.         if ballYPos == rPos-3 then
  142.             ballDir =  -1
  143.             ballAngle = -2
  144.             ballXPos = ballXPos - 1
  145.         elseif ballYPos >= rPos-2 and ballYPos <= rPos then
  146.             ballDir = -1
  147.             ballAngle = -1
  148.             ballXPos = ballXPos - 1
  149.         elseif ballYPos <= rPos+2 and ballYPos > rPos then
  150.             ballDir = -1
  151.             ballAngle = 1
  152.             ballXPos = ballXPos - 1
  153.         elseif ballYPos == rPos+3 then
  154.             ballDir = -1
  155.             ballAngle = 2
  156.             ballXPos = ballXPos - 1
  157.         end
  158.     end
  159.     if rPos < ballYPos and waits > 5 then
  160.         rPos = rPos + 1
  161.         waits = 0
  162.     elseif rPos > ballYPos and waits > 5 then
  163.         rPos = rPos - 1
  164.         waits = 0
  165.     elseif waits <= 5 then
  166.         waits = waits + 1  
  167.     end
  168.     if ballXPos < 1 then
  169.         ballXPos = 19
  170.         rScore = rScore + 1
  171.         lPos = 9
  172.         rPos = 9
  173.     end
  174.     if ballXPos > 38 then
  175.         ballXPos = 19
  176.         ballYPos = 9
  177.         lScore = lScore + 1
  178.         lPos = 9
  179.         rPos = 9
  180.     end
  181.     if rScore >= maxScore then
  182.         term.setCursorPos(41, 12)
  183.         term.write("Right Wins")
  184.         term.setCursorPos(1, 19)
  185.         game = false
  186.     elseif lScore >= maxScore then
  187.         term.setCursorPos(41, 12)
  188.         term.write("Left Wins")
  189.         term.setCursorPos(1, 19)
  190.         game = false
  191.     end
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement