theninthbit

computercraft_pong_host

Jan 24th, 2021 (edited)
625
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- https://pastebin.com/nyMwe2HK
  2. -- pastebin get nyMwe2HK startup
  3.  
  4. -- autoloader config
  5. -- pastebin get XcX1FQmw startup
  6. -- {pastebin='nyMwe2HK ', program='stacker'}
  7.  
  8. pcall(require, 'computercraft')
  9. local HEIGHT = 19
  10. local WIDTH = 18
  11. local MAX_SCORE = 7
  12.  
  13. local click_stack = {}
  14. local ball = {}
  15. local paddle2 = {}
  16. local paddle1 = {}
  17. local predict2 = 1
  18. local predict1 = 1
  19. local display = {}
  20. local state = 0
  21. local round = 0
  22. local countin = 0
  23. local score1 = 0
  24. local score2 = 0
  25. local ballstart = HEIGHT-1
  26.  
  27. function connectMonitor()
  28.   for k,v in pairs(redstone.getSides()) do
  29.     if peripheral.getType(v) == 'monitor' then  
  30.         return peripheral.wrap(v)
  31.     end
  32.   end
  33.   print('monitor not found')
  34.   return false
  35. end
  36.  
  37. function delay(seconds)
  38.     -- safe version of 'sleep' - will requeue dropped events
  39.     local timer = os.startTimer(seconds)
  40.     local q = {}
  41.     while true do
  42.         local data = {os.pullEvent()}
  43.         if data[1] == "timer" and data[2] == timer then
  44.             break
  45.         else
  46.             table.insert(q, data)
  47.         end
  48.     end
  49.     for i,v in ipairs(q) do
  50.         os.queueEvent(unpack(v))
  51.     end
  52. end
  53.  
  54. function drawBackground(display)
  55.   display.setTextScale(1)
  56.   display.setCursorPos(1,1)
  57.   for i=1,HEIGHT do
  58.     display.setBackgroundColor(colors.white)
  59.     display.setCursorPos(1,i)
  60.     display.write(string.rep(" ",WIDTH))
  61.  
  62.   end
  63. end
  64. function drawball()
  65.   drawBackground(display)
  66.   display.setCursorPos(ball.x, ball.y)
  67.   display.setBackgroundColor(colors.red)
  68.   display.write(" ")
  69.  
  70. end
  71.  
  72. function drawpaddle(paddle)
  73.   display.setCursorPos(paddle.x, paddle.y)
  74.   display.setBackgroundColor(colors.blue)
  75.   display.write(string.rep(" ",paddle.width))
  76.  
  77. end
  78.  
  79. function drawMessage(msg)
  80.   display.setBackgroundColor(colors.green)
  81.   display.setCursorPos(1, HEIGHT/2)
  82.   display.write(string.rep(" ",WIDTH))
  83.   display.setTextColor(colors.white)
  84.   display.setCursorPos(2, HEIGHT/2)
  85.   display.write(msg)
  86. end
  87.  
  88. function parseStartClick()
  89.   while #click_stack > 0 do
  90.     local pos = table.remove(click_stack)
  91.     -- if close to paddle1, move paddel1
  92.     return 1
  93.   end
  94.   return 0
  95. end
  96.  
  97. function parseClicks()
  98.   while #click_stack > 0 do
  99.     local pos = table.remove(click_stack)
  100.     -- if close to paddle1, move paddel1
  101.     if pos.y <= 3 then
  102.       if pos.x < paddle1.x then
  103.         paddle1.x = paddle1.x - 1
  104.       end
  105.       if pos.x >= (paddle1.x + paddle1.width) then
  106.         paddle1.x = paddle1.x + 1
  107.       end
  108.     end
  109.     -- if close to paddle2, move paddel2
  110.     if pos.y >= HEIGHT-3 then
  111.       if pos.x < paddle2.x then
  112.         paddle2.x = paddle2.x - 1
  113.       end
  114.       if pos.x >= (paddle2.x + paddle2.width) then
  115.         paddle2.x = paddle2.x + 1
  116.       end
  117.     end
  118.    
  119.   end
  120. end
  121.  
  122. function moveBall()
  123.   if ball.y == 1 or ball.y == HEIGHT then
  124.     return ball.y==1 and 1 or 2;
  125.   else
  126.     if ball.diry == 1 then
  127.       ball.y = ball.y + .5
  128.     else
  129.       ball.y = ball.y - .5
  130.     end
  131.     if ball.dirx == 1 then
  132.       ball.x = ball.x + 1
  133.     else
  134.       ball.x = ball.x - 1
  135.     end
  136.     --x bounce
  137.     if ball.x == WIDTH then
  138.       ball.dirx = -1
  139.     end
  140.     if ball.x == 1 then
  141.       ball.dirx = 1
  142.     end
  143.   end
  144.   --y bounce
  145.   if ball.y == HEIGHT-1 and ball.diry == 1 then
  146.     print("2)", ball.x, paddle2.x, paddle2.x+paddle2.width-1)
  147.     if ball.x >= paddle2.x and ball.x <= (paddle2.x+paddle2.width-1) then
  148.       ball.diry = 1
  149.     end
  150.   end
  151.   if ball.y == 2 and ball.diry == -1 then
  152.     print("1)", ball.x, paddle1.x, paddle1.x+paddle1.width)
  153.     if ball.x >= paddle1.x and ball.x <= (paddle1.x+paddle1.width-1) then
  154.       ball.diry = -1
  155.     end
  156.   end
  157.   return 0
  158. end
  159.  
  160. function predictBall()
  161.   local targetPosition = 0
  162.  
  163.   if ball.y == 2 and ball.diry == 1 then
  164.     if ball.dirx == 1 then
  165.        targetPosition = ball.x - 38
  166.     else
  167.        targetPosition = ball.x + 38
  168.     end
  169.     predict2 = (targetPosition ) % WIDTH
  170.     if predict2 == 0 then
  171.       predict2 = WIDTH
  172.     end
  173.     print(predict2)
  174.   end
  175.   if ball.y == HEIGHT-1 and ball.diry == -1 then
  176.     if ball.dirx == 1 then
  177.        targetPosition = ball.x - 38
  178.     else
  179.        targetPosition = ball.x + 38
  180.     end
  181.     predict1 = (targetPosition ) % WIDTH
  182.     if predict2 == 0 then
  183.       predict2 = WIDTH
  184.     end
  185.     print(predict1)
  186.   end
  187. end
  188.  
  189. function autoPlay()
  190.   if predict1 < paddle1.x then
  191.     paddle1.x = paddle1.x - 1
  192.     print("1 go left")
  193.   elseif predict1 >= (paddle1.x+paddle1.width-1) then
  194.     paddle1.x = paddle1.x + 1
  195.     print("1 go right")
  196.   end
  197.  
  198.   if predict2 < paddle2.x then
  199.     paddle2.x = paddle2.x - 1
  200.     print("2 go left")
  201.   elseif predict2 >= (paddle2.x+paddle2.width-1) then
  202.     paddle2.x = paddle2.x + 1
  203.     print("2 go right")
  204.   end
  205. end
  206. function init()
  207.   -- which side the ball starts on
  208.   local startDir=1
  209.   if ballstart == HEIGHT-1 then
  210.     ballstart = 2
  211.     startDir = 1
  212.   else
  213.     ballstart = HEIGHT-1
  214.     startDir = -1
  215.   end
  216.   print(ballstart)
  217.  
  218.   ball = {
  219.     x=2,
  220.     y=ballstart,
  221.     dirx=1,
  222.     diry=startDir,
  223.   }
  224.   paddle1 = {
  225.     x=1,
  226.     y=1,
  227.     width=5,
  228.   }
  229.   paddle2 = {
  230.     x=1,
  231.     y=HEIGHT,
  232.     width=5,
  233.   }
  234. end
  235.  
  236. function tick()
  237.   -- table is started
  238.   if state == 0 then
  239.     init()
  240.     state = 1
  241.   -- is waiting to start game
  242.   elseif state == 1 then
  243.     moveBall()
  244.     predictBall()
  245.     autoPlay()
  246.    
  247.     drawball()
  248.     drawpaddle(paddle1)
  249.     drawpaddle(paddle2)
  250.     drawMessage("Click to start")
  251.    
  252.     if parseStartClick() == 1 then
  253.       init()
  254.       state = 2
  255.       countin = 10
  256.     end
  257.    
  258.   -- countdown to start
  259.   elseif state == 2 then
  260.     drawball()
  261.     drawpaddle(paddle1)
  262.     drawpaddle(paddle2)
  263.     drawMessage("Starting.. "..countin)
  264.     -- drawScore(score1, score2)
  265.    
  266.     countin = countin - 1
  267.     if countin == 0 then
  268.       parseStartClick() -- resets the clicks stack
  269.       state = 3
  270.     end
  271.  
  272.   -- game is running
  273.   elseif state == 3 then
  274.     -- moves the paddle
  275.     parseClicks()
  276.     -- moves the ball and returns if the ball is scored
  277.     local pointScored = moveBall()
  278.     -- if no point is scored
  279.     if pointScored == 0 then
  280.       drawball()
  281.       drawpaddle(paddle1)
  282.       drawpaddle(paddle2)
  283.       -- drawScore(score1, score2)
  284.     else
  285.       if pointScored == 1 then
  286.         score1 = score1+1
  287.       else
  288.         score2 = score2+1
  289.       end
  290.       -- pause to show the scored point
  291.       delay(1)
  292.       -- go back to countdown to start
  293.       round = round + 1
  294.      
  295.       -- either end game OR new round
  296.       if score1 == MAX_SCORE or score2 == MAX_SCORE then
  297.         state = 4
  298.       else
  299.         init()
  300.         countin = 10
  301.         state = 2
  302.       end
  303.     end
  304.   elseif state == 4 then
  305.     -- show win screen
  306.    
  307.     delay(1)
  308.     -- go back to init
  309.     state = 1
  310.    
  311.   end
  312.    
  313.  
  314.   delay(.1)
  315. end
  316.  
  317.  
  318. function waitforclicks()
  319.   event, button, x, y = os.pullEvent("monitor_touch")
  320.   table.insert(click_stack, {x=x, y=y})
  321. end
  322.  
  323. function main()
  324.   display = connectMonitor()
  325.   init()
  326.   while true do
  327.     parallel.waitForAny(tick, waitforclicks)
  328.   end
  329.  
  330. end
  331.  
  332. main()
RAW Paste Data