Advertisement
paramus

Air hockey

Feb 18th, 2025 (edited)
118
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.     -- Player 1
  11.     if (redstone.getAnalogInput("back") > 0) then --P1 left
  12.         x1=x1+2
  13.     end
  14.    
  15.     if (redstone.getAnalogInput("left") > 0) then --P1 right
  16.         x1=x1-2
  17.     end
  18.    
  19.     if (x1<1) then x1=1 end
  20.     if (x1>29-width1) then x1=29-width1 end
  21.    
  22.     -- Player 2
  23.     if (redstone.getAnalogInput("right") > 0) then --P2 left
  24.         x2=x2+2
  25.     end
  26.    
  27.     if (redstone.getAnalogInput("front") > 0) then --P2 right
  28.         x2=x2-2
  29.     end
  30.    
  31.     if (x2<1) then x2=1 end
  32.     if (x2>29-width2) then x2=29-width2 end
  33.    
  34.     -- Ball movement
  35.     bx=bx+sx
  36.     by=by+sy
  37.    
  38.     -- Wall collision
  39.     if (bx<=1 or bx>=29)then
  40.         sx=sx*-1
  41.     end
  42.    
  43.     if (by<=1)then
  44.         bx,by = 15,17
  45.         p2=p2+1
  46.         sy=sy*-1
  47.     elseif (by>=33) then
  48.         bx,by = 15,17
  49.         p1=p1+1
  50.         sy=sy*-1
  51.     end
  52.    
  53.     -- Player 1 collision
  54.     if (bx>=x1 and bx<=x1+width1 and by<=3)then
  55.         sy=sy*-1
  56.     end
  57.    
  58.     -- Player 2 collision
  59.     if (bx>=x2 and bx<=x2+width2 and by>=31)then
  60.         sy=sy*-1
  61.     end
  62.    
  63.     -- Random movement
  64.     if (math.random(0,50) == 0) then
  65.         bx=bx+1
  66.     end
  67. end
  68.  
  69. function Paint()
  70.     term.setBackgroundColor(colors.white)
  71.     term.clear()
  72.    
  73.     -- Score
  74.     term.setTextColor(colors.black)
  75.     term.setCursorPos(13,20)
  76.     term.write(tostring(p1) .. " - " .. tostring(p2))
  77.    
  78.     -- Middle bar
  79.     paintutils.drawLine(1,17,12,17,colors.lightGray)
  80.     paintutils.drawBox(13,15,17,19,colors.lightGray)
  81.     paintutils.drawLine(18,17,29,17,colors.lightGray)
  82.    
  83.     -- Areas
  84.     paintutils.drawBox(6,0,24,5,colors.lightGray)
  85.     paintutils.drawBox(6,34,24,29,colors.lightGray)
  86.    
  87.     -- Player1 bar
  88.     paintutils.drawLine(x1,2,x1+width1,2,colors.blue)
  89.    
  90.     -- Player2 bar
  91.     paintutils.drawLine(x2,32,x2+width2,32,colors.red)
  92.    
  93.     -- Ball
  94.     paintutils.drawPixel(bx,by,colors.green)
  95. end
  96.  
  97. -- Main --
  98. term.redirect(monitor)
  99. while(true) do
  100.     Paint()
  101.     term.setBackgroundColor(colors.white)
  102.     term.setTextColor(colors.yellow)
  103.     term.setCursorPos(10,22)
  104.     term.write("INSERT COIN")
  105.     if (redstone.getAnalogInput("bottom") > 0) then
  106.         local loop = true
  107.         while(loop) do
  108.             Movement()
  109.             Paint()
  110.             sleep(0.2)
  111.             if(p1>10 or p2>10) then
  112.                 loop=false
  113.                 p1,p2 = 0,0
  114.             end
  115.         end
  116.     end
  117.     sleep(0.1)
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement