paramus

Air hockey

Feb 18th, 2025 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | Gaming | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local width1,width2 = 5,5
  3. local x1,x2=13,12
  4. local bx,by,sx,sy = 15,17,2,1
  5. -- Score
  6. local p1,p2 = 0,0
  7. --Resolution x=29 y=33
  8.  
  9. function Movement()
  10.    
  11.     if (x2<1) then x2=1 end
  12.     if (x2>29-width2) then x2=29-width2 end
  13.    
  14.     -- Ball movement
  15.     bx=bx+sx
  16.     by=by+sy
  17.    
  18.     -- Wall collision
  19.     if (bx<1) then
  20.         sx=(sx*-1)
  21.         bx = 2
  22.     elseif (bx>29) then
  23.         sx=(sx*-1)
  24.         bx = 28
  25.     end
  26.    
  27.     -- Score?
  28.     if (by<=1)then
  29.         bx,by = 15,17
  30.         p2=p2+1
  31.         sy=sy*-1
  32.     elseif (by>=33) then
  33.         bx,by = 15,17
  34.         p1=p1+1
  35.         sy=sy*-1
  36.     end
  37.    
  38.     -- Player 1 collision
  39.     if (bx>=x1 and bx<=x1+width1 and by<=3)then
  40.         sy=(sy*-1) -- + math.random(-2,2)
  41.         bx = bx + math.random(-1,1)
  42.     end
  43.    
  44.     -- Player 2 collision
  45.     if (bx>=x2 and bx<=x2+width2 and by>=31)then
  46.         sy=(sy*-1) -- + math.random(-2,2)
  47.         bx = bx + math.random(-1,1)
  48.     end
  49. end
  50.  
  51. function Paint()
  52.     term.setBackgroundColor(colors.white)
  53.     term.clear()
  54.    
  55.     -- Score
  56.     term.setTextColor(colors.black)
  57.     term.setCursorPos(13,20)
  58.     term.write(tostring(p1) .. " - " .. tostring(p2))
  59.    
  60.     -- Middle bar
  61.     paintutils.drawLine(1,17,12,17,colors.lightGray)
  62.     paintutils.drawBox(13,15,17,19,colors.lightGray)
  63.     paintutils.drawLine(18,17,29,17,colors.lightGray)
  64.    
  65.     -- Areas
  66.     paintutils.drawBox(6,0,24,5,colors.lightGray)
  67.     paintutils.drawBox(6,34,24,29,colors.lightGray)
  68.    
  69.     -- Player1 bar
  70.     paintutils.drawLine(x1,2,x1+width1,2,colors.blue)
  71.    
  72.     -- Player2 bar
  73.     paintutils.drawLine(x2,32,x2+width2,32,colors.red)
  74.    
  75.     -- Ball
  76.     paintutils.drawPixel(bx,by,colors.green)
  77. end
  78.  
  79. -- Main --
  80. term.redirect(monitor)
  81. while(true) do
  82.     sx, sy = 2,1
  83.     Paint()
  84.     term.setBackgroundColor(colors.white)
  85.     term.setTextColor(colors.yellow)
  86.     term.setCursorPos(10,22)
  87.     term.write("INSERT COIN")
  88.     if (redstone.getAnalogInput("bottom") > 0) then
  89.         local loop = true
  90.         while(loop) do
  91.             parallel.waitForAny(function()
  92.                 local event, button, x, y = os.pullEvent("monitor_touch")
  93.                 if (y > 16) then
  94.                     x2 = x-2
  95.                 else
  96.                     x1 = x-2
  97.                 end
  98.             end,
  99.             function()
  100.                 Movement()
  101.                 Paint()
  102.                 sleep(0.2)
  103.                 if(p1>10 or p2>10) then
  104.                     loop=false
  105.                     p1,p2 = 0,0
  106.                 end
  107.             end)
  108.         end
  109.     end
  110.     sleep(0.1)
  111. end
Advertisement
Add Comment
Please, Sign In to add comment