Advertisement
AssortedBrunoz

pong

Oct 17th, 2024 (edited)
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. local max_x, max_y = term.getSize()
  2. local p1 = 1
  3. local p1_score = 0
  4. local p2 = 1
  5. local p2_score = 0
  6. local ball = {
  7.     x = max_x/2,
  8.     y = max_y/2,
  9.     direction_x = -1,
  10.     direction_y = 1
  11. }
  12. local pressing = {}
  13. local win = 0
  14.  
  15. local function reset()
  16.     term.setCursorPos(1,1)
  17.     term.clear()
  18.     term.setBackgroundColor(colors.black)
  19. end
  20.  
  21. local function update()
  22.     while true do
  23.  
  24.         if pressing[keys.w] then
  25.             p1 = (p1 == 1) and p1 or p1 - 1
  26.         end
  27.         if pressing[keys.s] then
  28.             p1 = (p1+5 == max_y) and p1 or p1 + 1
  29.         end
  30.  
  31.         if pressing[keys.up] then
  32.             p2 = (p2 == 1) and p2 or p2 - 1
  33.         end
  34.         if pressing[keys.down] then
  35.             p2 = (p2+5 == max_y) and p2 or p2 + 1
  36.         end
  37.  
  38.         local addx = (ball.x + ball.direction_x) --addx >= max_x or addx <= 1 bounce
  39.         local addy = (ball.y + ball.direction_y)
  40.  
  41.         if (addx < 4 or addx > max_x-3) then -- in bounds
  42.             if (addx < max_x/2 and (p1 <= addy and addy <= p1+6)) or (addx > max_x/2 and (p2 <= addy and addy <= p2+6)) then -- left side
  43.                 ball.direction_x = -ball.direction_x
  44.             end
  45.         end
  46.         if (ball.x < 3) then --out of bounds
  47.             p2_score = p2_score + 1
  48.             ball.x = max_x/2
  49.             ball.y = max_y/2
  50.         elseif (ball.x > max_x-2) then --out of bounds
  51.             p1_score = p1_score + 1
  52.             ball.x = max_x/2
  53.             ball.y = max_y/2
  54.         end
  55.         if p1_score == win or p2_score == win then
  56.             reset()
  57.  
  58.             write(p1_score>p2_score and "PLAYER 1 WINS" or "PLAYER 2 WINS")
  59.  
  60.             os.sleep(5)
  61.             p1_score = 0
  62.             p2_score = 0
  63.  
  64.             return
  65.         end
  66.        
  67.         if addy >= max_y or addy <= 1  then
  68.             ball.direction_y = -ball.direction_y
  69.         end
  70.         ball.x = ball.x + ball.direction_x
  71.         ball.y = ball.y + ball.direction_y
  72.  
  73.  
  74.         paintutils.drawFilledBox(1,1,max_x,max_y,colors.black)
  75.  
  76.         paintutils.drawLine(max_x/2,1,max_x/2,max_y,colors.gray) -- line
  77.  
  78.         paintutils.drawFilledBox(2,p1,3,p1+5,colors.white)
  79.  
  80.         paintutils.drawFilledBox(max_x-2,p2,max_x-3,p2+5,colors.white)
  81.        
  82.         paintutils.drawPixel(ball.x,ball.y,colors.blue)
  83.  
  84.         term.setCursorPos((max_x/2)-#tostring(p1_score),1)
  85.         write(p1_score)
  86.  
  87.         term.setCursorPos((max_x/2)+1,1)
  88.         write(p2_score)
  89.  
  90.         os.sleep(0.1)
  91.     end
  92. end
  93.  
  94. local function input_control()
  95.     while true do
  96.  
  97.         local event, key = os.pullEvent()
  98.         if event == "key" then
  99.             pressing[key] = true
  100.         elseif event == "key_up" then
  101.             pressing[key] = false
  102.         end
  103.     end
  104. end
  105.  
  106. while true do
  107.     reset()
  108.  
  109.     write("PRESS ENTER TO START PONGING\n")
  110.     local _, key = os.pullEvent("key")
  111.  
  112.     if key == keys.enter then
  113.         write("ENTER WIN NUMBER: ")
  114.         win = tonumber(read())
  115.         if win then
  116.             parallel.waitForAny(update, input_control)
  117.         end
  118.     end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement