paramus

Brick breaker

Aug 25th, 2025 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | Gaming | 0 0
  1. local Bricks = {{1,1,colors.orange}, {2,1,colors.orange}, {3,1,colors.orange}, {4,1,colors.orange}}
  2. local MaxX,MaxY = term.getSize()
  3. local x,y = 10, 10
  4. local pX,pY = 20, MaxY-3
  5. local sx,sy = 1,-1
  6.  
  7. function Draw()
  8.     term.setBackgroundColour(colors.black)
  9.     term.clear()
  10.    
  11.     --Bricks
  12.     for i,u in pairs(Bricks) do
  13.         paintutils.drawPixel(u[1], u[2], u[3])
  14.     end
  15.    
  16.     --Ball
  17.     paintutils.drawPixel(x, y, colors.white)
  18.    
  19.     --Player bar
  20.     paintutils.drawLine(pX,pY, pX+4,pY, colors.blue)
  21. end
  22.  
  23. function Physics()
  24.     x = x+sx
  25.     y = y+sy
  26.    
  27.     -- Check brick colision
  28.     local counter = 1
  29.     for i,u in pairs(Bricks) do
  30.         --print(x .. "," .. y .. "," .. u[1] .. "," .. u[2])
  31.         if (x==u[1] and y==u[2]) then
  32.             table.remove(Bricks, counter)
  33.             sx=sx*-1
  34.             sy=sy*-1
  35.             break
  36.         end
  37.         counter = counter+1
  38.     end
  39.    
  40.     --check ball-player collision
  41.     if (pY == y and (pX <= x and (pX+4)>= x)) then
  42.         y = pX-2
  43.         sx=sx*-1
  44.         sy=sy*-1
  45.     end
  46.    
  47.     --X Boundries
  48.     if (x < 1) then
  49.         x=2
  50.         sx=sx*-1
  51.     elseif (x > MaxX) then
  52.         x=MaxX-1
  53.         sx=sx*-1
  54.     end
  55.    
  56.     --Y Boundries
  57.     if (y < 1) then
  58.         y=2
  59.         sy=sy*-1
  60.     elseif (y > MaxY) then
  61.         -- Loose live
  62.         y=MaxY-1
  63.         sy=sy*-1
  64.     end
  65. end
  66.  
  67. while (true) do
  68.     parallel.waitForAny(function()
  69.         Physics()
  70.         Draw()
  71.         sleep(0.2)
  72.     end,
  73.     function()
  74.         local event, key = os.pullEvent("key")
  75.         if (key == keys.left) then
  76.             pX = pX-1
  77.         elseif (key == keys.right) then
  78.             pX = pX+1
  79.         end
  80.     end)
  81. end
Advertisement
Add Comment
Please, Sign In to add comment