Advertisement
tusKOr661

Basic pong game, (SB only).

Apr 27th, 2022
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. --
  3. --
  4.  
  5. Pong = {
  6.     RUNNING_GAMES = {},
  7.    
  8.     __CLIENT = [=[
  9. user = game:service 'Players'.localPlayer;
  10. mouse = user:getMouse (0);
  11. playerGui = user:findFirstChildOfClass 'PlayerGui';
  12. character = user.Character;
  13. remote = script:waitForChild 'Remote'.Value;
  14.  
  15. pcall (function ()character.Parent = nil; end);
  16. gui = Instance.new 'ScreenGui';
  17. frame = Instance.new 'Frame';
  18. paddleLeft = Instance.new 'Frame';
  19. paddleRight = Instance.new 'Frame';
  20. ball = Instance.new 'Frame';
  21. plrLeft = Instance.new 'TextLabel';
  22. plrRight = Instance.new 'TextLabel';
  23.  
  24. gui.Parent = playerGui;
  25. gui.Name = 'Pong!';
  26.  
  27. frame.Parent = gui;
  28. frame.BackgroundColor3 = Color3.new (1, 1, 1);
  29. frame.Size = UDim2.new (0, 200, 0, 200);
  30. frame.Position = UDim2.new (0.5, -100, 0.5, -100);
  31.  
  32. winner = Instance.new 'TextLabel';
  33. winner.Size = UDim2.new (1, 0, 0, 50);
  34. winner.BackgroundTransparency = 1;
  35. winner.Font = 'SourceSansBold';
  36. winner.FontSize = 'Size24';
  37. winner.Text = ''
  38.  
  39. paddleLeft.Parent = frame;
  40. paddleLeft.Position = UDim2.new (0, 5, 0, 75);
  41. paddleLeft.Size = UDim2.new (0, 2, 0, 50);
  42.  
  43. paddleRight.Parent = frame;
  44. paddleRight.Position = UDim2.new (1, -5, 0, 75);
  45. paddleRight.Size = UDim2.new (0, 2, 0, 50);
  46.  
  47. ball.Parent = frame;
  48. ball.Size = UDim2.new (0, 2, 0, 2);
  49. ball.Position = UDim2.new (0.5, 0, 0.5, 0);
  50. ball.BackgroundColor3 = Color3.new();
  51.  
  52. plrLeft.Text = 'Player1';
  53. plrLeft.Parent = frame;
  54. plrLeft.Rotation = -90;
  55. plrLeft.Position = UDim2.new (-0.5, -20, 0.5, 0);
  56. plrLeft.Size = UDim2.new (1, 0, 0, 10);
  57. plrLeft.TextStrokeTransparency = 0;
  58. plrLeft.TextStrokeColor3 = Color3.new (1, 1, 1);
  59. plrLeft.BackgroundTransparency = 1;
  60.  
  61. plrRight.Text = 'Player2';
  62. plrRight.Parent = frame;
  63. plrRight.Rotation = 90;
  64. plrRight.Position = UDim2.new (0.5, 20, 0.5, 0);
  65. plrRight.Size = UDim2.new (1, 0, 0, 10);
  66. plrRight.TextStrokeTransparency = 0;
  67. plrRight.TextStrokeColor3 = Color3.new (1, 1, 1);
  68. plrRight.BackgroundTransparency = 1;
  69.  
  70. remote.OnClientEvent:connect (function (req, ...)
  71.     if (req == 'set') then
  72.         local data, owner = ...;
  73.        
  74.         if (owner == user) then
  75.             ourPaddle = ...;
  76.            
  77.             if (ourPaddle == 1) then
  78.                 plrLeft.Text = owner.Name;
  79.             else
  80.                 plrRight.Text = owner.Name;
  81.             end;
  82.         else
  83.             if (ourPaddle == 1) then
  84.                 plrRight.Text = owner.Name;
  85.             else
  86.                 plrLeft.Text = owner.Name;
  87.             end;
  88.         end;
  89.        
  90.        
  91.     elseif (... == 'paddle') then
  92.         local owner = req;
  93.         local req, data = ...;
  94.        
  95.         if (owner == user) then
  96.             --[[
  97.             if (ourPaddle == 1) then
  98.                 paddleLeft.Position = UDim2.new (0, 5, 0, data);
  99.             else
  100.                 paddleRight.Position = UDim2.new (1, -5, 0, data);
  101.             end;
  102.             --]]
  103.         else
  104.             if (ourPaddle == 1) then
  105.                 paddleRight.Position = UDim2.new (1, -5, 0, data);
  106.             else
  107.                 paddleLeft.Position = UDim2.new (0, 5, 0, data);
  108.             end;
  109.         end;
  110.     elseif (req == 'ball') then
  111.         local x, y = ...;
  112.         local our = ourPaddle == 1 and paddleLeft or paddleRight;
  113.         ball.Position = UDim2.new (0, x, 0, y);
  114.        
  115.         if ((x < 6 and ourPaddle == 1) or (x > 194 and ourPaddle == 2)) then
  116.             if (y > our.Position.Y.Offset and y < (our.Position.Y.Offset + 50)) then
  117.                 remote:FireServer ('Bump');
  118.             else
  119.                 remote:FireServer ('Eof');
  120.             end;
  121.         end;
  122.     elseif (req == 'over') then
  123.         if (winner.Parent) then return 0 end;
  124.        
  125.         frame:ClearAllChildren ();
  126.         winner.Parent = frame;
  127.         winner.Text = 'You ' .. (... == user and 'lose' or 'win') .. '!';
  128.         character.Parent = workspace;
  129.         character:MakeJoints ();
  130.        
  131.         wait (3);
  132.         gui:destroy ();
  133.     end;
  134. end);
  135.  
  136. keys = {};
  137. mouse.KeyDown:connect (function (s)
  138.     if (not ourPaddle) then return 0 end;
  139.     keys [tostring (s)] = true;
  140. end);
  141.  
  142. mouse.KeyUp:connect (function (s)
  143.     keys [tostring (s)] = false;
  144. end);
  145.  
  146. remote:FireServer();
  147.  
  148. game:service 'RunService'.RenderStepped:connect (function()
  149.     local our = ourPaddle == 1 and paddleLeft or paddleRight;
  150.    
  151.     if (keys.w and not keys.s) then
  152.         if (our.Position.Y.Offset > 0) then
  153.             our.Position = UDim2.new (
  154.                 0, 5, 0, our.Position.Y.Offset - 2
  155.             );
  156.         end;
  157.        
  158.         remote:FireServer ('paddle', our.Position.Y.Offset);
  159.     end;
  160.     if (keys.s and not keys.w) then
  161.         if (our.Position.Y.Offset < 150) then
  162.             our.Position = UDim2.new (
  163.                 0, 5, 0, our.Position.Y.Offset + 2
  164.             );
  165.         end;
  166.        
  167.         remote:FireServer ('paddle', our.Position.Y.Offset);
  168.     end;
  169. end);
  170.     ]=],
  171.    
  172.     __this = {
  173.         users = {},
  174.         ball = {99, 99, math.random (1, 2) == 1 and -1 or 1, math.random (1, 2) == 1 and -1 or 1},
  175.         running = true,
  176.        
  177.         addPlayer = function (self, n)
  178.             if (#self.users >= 2) then 
  179.                 return error ('Game full');
  180.             end;
  181.            
  182.             if (self.remote == nil) then
  183.                 self.remote = Instance.new 'RemoteEvent';
  184.                 self.remote.Parent = game.ReplicatedStorage;
  185.             end;
  186.            
  187.             local p = game:service 'Players':findFirstChild (n);
  188.            
  189.             if (p and p:isA 'Player') then
  190.                 local scr = NLS (Pong.__CLIENT, p.Backpack);
  191.                 local rem = Instance.new 'ObjectValue';
  192.                     rem.Name = 'Remote';
  193.                     rem.Value = self.remote;
  194.                     rem.Parent = scr;
  195.                 while true do
  196.                     local u = self.remote.OnServerEvent:wait ();
  197.                    
  198.                     if (u == p) then break end;
  199.                 end;
  200.                 self.remote:FireAllClients ('set', #self.users + 1, p);
  201.                 self.users [#self.users + 1] = {p, 100};
  202.             else
  203.                 print ('unable to find', n, p);
  204.             end;
  205.         end,
  206.        
  207.         waitForReady = function (self)
  208.             while (#self.users ~= 2) do wait() end;
  209.            
  210.             wait (5);
  211.         end,
  212.        
  213.         start = function (self)
  214.             if (#self.users ~= 2) then 
  215.                 return error ('not enough players');
  216.             end;
  217.            
  218.             self.remote.OnServerEvent:connect (function (client, req, data)
  219.                 if (req == 'paddle') then
  220.                     self.remote:FireAllClients (client, req, data);
  221.                 elseif (req == 'Bump') then
  222.                     self.ball [3] = -self.ball [3];
  223.                 elseif (req == 'Eof') then
  224.                     self.remote:FireAllClients ('over', client);
  225.                     self.running = false;
  226.                 end;
  227.             end);
  228.            
  229.             local lastBeat = tick();
  230.             game:service 'RunService'.Heartbeat:connect (function()
  231.                 if (not self.running or (self.ball [1] < 1 or self.ball [1] > 199)) then
  232.                     return;
  233.                 end;
  234.                 local currentBeat = tick();
  235.                
  236.                 if (not ((currentBeat - lastBeat) > 1/30)) then
  237.                     return;
  238.                 end;
  239.                
  240.                 lastBeat = currentBeat;
  241.                
  242.                 self.ball [1] = self.ball [1] + self.ball [3];
  243.                 self.ball [2] = self.ball [2] + self.ball [4];
  244.                
  245.                 if (self.ball [2] >= 200 or self.ball [2] <= 0) then
  246.                     self.ball [4] = -self.ball [4];
  247.                 end;
  248.                
  249.                 self.remote:FireAllClients ('ball', self.ball [1], self.ball [2]);
  250.             end);
  251.         end,
  252.     },
  253.    
  254.     new = function ()
  255.         local m = {};
  256.        
  257.         for k, v in next, Pong.__this do
  258.             if (type (v) == 'table') then
  259.                 local n = {};
  260.                
  261.                 for k, v in next, v do
  262.                     n [k] = v;
  263.                 end;
  264.                
  265.                 m [k] = n;
  266.             else
  267.                 m [k] = v;
  268.             end;
  269.         end;
  270.        
  271.         return m;
  272.     end
  273. };
  274.  
  275. g = Pong.new();
  276. spawn (function()g:addPlayer (owner.Chatted:wait());end);
  277. spawn (function()g:addPlayer ('tusKOr661');end);
  278. g:waitForReady ();
  279. g:start ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement