Can you create a multiplayer pong game for s&box?
- User
Can you create a multiplayer pong game for s&box?
Assistant
What a blast from the past! Pong is a classic game that can be a lot of fun in a multiplayer setting. Here's a basic implementation of a multiplayer Pong game for S&box:
Note: This is a simplified version of the game, and you can always add more features, such as scoring, sound effects, and smoother animations.
Game Logic:
- Create a new S&box project and add a
pongfolder to theaddonsdirectory. - Inside the
pongfolder, create a new file calledclient.lua. This file will contain the client-side logic for the game. - Create another file called
server.lua. This file will contain the server-side logic for the game.
Client-Side Logic (client.lua):
-- Import necessary libraries local entity = entity local input = input local render = render -- Define the Pong game variables local paddleWidth = 10 local paddleHeight = 100 local ballSize = 10 local ballSpeed = 500 -- Create the paddles and ball entities local paddle1 = entity.Create("paddle1") paddle1:SetModel("models/props_c17/bench01a.mdl") paddle1:SetPos(Vector(-200, 0, 0)) paddle1:SetAngles(Angle(0, 0, 0)) paddle1:SetScale(Vector(paddleWidth, paddleHeight, 1)) local paddle2 = entity.Create("paddle2") paddle2:SetModel("models/props_c17/bench01a.mdl") paddle2:SetPos(Vector(200, 0, 0)) paddle2:SetAngles(Angle(0, 0, 0)) paddle2:SetScale(Vector(paddleWidth, paddleHeight, 1)) local ball = entity.Create("ball") ball:SetModel("models/props_c17/concrete_barrier001a.mdl") ball:SetPos(Vector(0, 0, 0)) ball:SetAngles(Angle(0, 0, 0)) ball:SetScale(Vector(ballSize, ballSize, ballSize)) -- Define the input handlers for the paddles input.AddButton("paddle1_up", "w") input.AddButton("paddle1_down", "s") input.AddButton("paddle2_up", "uparrow") input.AddButton("paddle2_down", "downarrow") -- Update the paddle positions based on user input function UpdatePaddles() if input.IsButtonDown("paddle1_up") then paddle1:SetPos(paddle1:GetPos() + Vector(0, 10, 0)) elseif input.IsButtonDown("paddle1_down") then paddle1:SetPos(paddle1:GetPos() - Vector(0, 10, 0)) end if input.IsButtonDown("paddle2_up") then paddle2:SetPos(paddle2:GetPos() + Vector(0, 10, 0)) elseif input.IsButtonDown("paddle2_down") then paddle2:SetPos(paddle2:GetPos() - Vector(0, 10, 0)) end end -- Update the ball position and velocity function UpdateBall() -- Get the current ball position and velocity local ballPos = ball:GetPos() local ballVel = ball:GetVelocity() -- Update the ball position based on its velocity ball:SetPos(ballPos + ballVel * FrameTime()) -- Check for collisions with the paddles if ballPos.x < paddle1:GetPos().x + paddleWidth / 2 and ballPos.y > paddle1:GetPos().y - paddleHeight / 2 and ballPos.y < paddle1:GetPos().y + paddleHeight / 2 then -- Ball collided with paddle 1, reverse the x velocity ballVel.x = -ballVel.x ball:SetVelocity(ballVel) elseif ballPos.x > paddle2:GetPos().x - paddleWidth / 2 and ballPos.y > paddle2:GetPos().y - paddleHeight / 2 and ballPos.y < paddle2:GetPos().y + paddleHeight / 2 then -- Ball collided with paddle 2, reverse the x velocity ballVel.x = -ballVel.x ball:SetVelocity(ballVel) end -- Check for collisions with the top and bottom of the screen if ballPos.y < -100 or ballPos.y > 100 then -- Ball collided with the top or bottom of the screen, reverse the y velocity ballVel.y = -ballVel.y ball:SetVelocity(ballVel) end end -- Draw the game elements function DrawGame() render.SetColor(1, 1, 1) render.DrawModel(paddle1) render.DrawModel(paddle2) render.DrawModel(ball) end -- Main game loop function Think() UpdatePaddles() UpdateBall() DrawGame() end -- Call the Think function every frame hook.Add("Think", "PongGame", Think)Server-Side Logic (server.lua):
-- Import necessary libraries local entity = entity local player = player -- Define the Pong game variables local paddleWidth = 10 local paddleHeight = 100 local ballSize = 10 local ballSpeed = 500 -- Create the paddles and ball entities local paddle1 = entity.Create("paddle1") paddle1:SetModel("models/props_c17/bench01a.mdl") paddle1:SetPos(Vector(-200, 0, 0)) paddle1:SetAngles(Angle(0, 0, 0)) paddle1:SetScale(Vector(paddleWidth, paddleHeight, 1)) local paddle2 = entity.Create("paddle2") paddle2:SetModel("models/props_c17/bench01a.mdl") paddle2:SetPos(Vector(200, 0, 0)) paddle2:SetAngles(Angle(0, 0, 0)) paddle2:SetScale(Vector(paddleWidth, paddleHeight, 1)) local ball = entity.Create("ball") ball:SetModel("models/props_c17/concrete_barrier001a.mdl") ball:SetPos(Vector(0, 0, 0)) ball:SetAngles(Angle(0, 0, 0)) ball:SetScale(Vector(ballSize, ballSize, ballSize)) -- Define the player input handlers for the paddles player.AddButton("paddle1_up", "w") player.AddButton("paddle1_down", "s") player.AddButton("paddle2_up", "uparrow") player.AddButton("paddle2_down", "downarrow") -- Update the paddle positions based on user input function UpdatePaddles() for _, ply in pairs(player.GetAll()) do if ply:KeyDown("paddle1_up") then paddle1:SetPos(paddle1:GetPos() + Vector(0, 10, 0)) elseif ply:KeyDown("paddle1_down") then paddle1:SetPos(paddle1:GetPos() - Vector(0, 10, 0)) end if ply:KeyDown("paddle2_up") then paddle2:SetPos(paddle2:GetPos() + Vector(0, 10, 0)) elseif ply:KeyDown("paddle2_down") then paddle2:SetPos(paddle2:GetPos() - Vector(0, 10, 0)) end end end -- Update the ball position and velocity function UpdateBall() -- Get the current ball position and velocity local ballPos = ball:GetPos() local ballVel = ball:GetVelocity() -- Update the ball position based on its velocity ball:SetPos(ballPos + ballVel * FrameTime()) -- Check for collisions with the paddles if ballPos.x < paddle1:GetPos().x + paddleWidth / 2 and ballPos.y > paddle1:GetPos().y - paddleHeight / 2 and ballPos.y < paddle1:GetPos().y + paddleHeight / 2 then -- Ball collided with paddle 1, reverse the x velocity ballVel.x = -ballVel.x ball:SetVelocity(ballVel) elseif ballPos.x > paddle2:GetPos().x - paddleWidth / 2 and ballPos.y > paddle2:GetPos().y - paddleHeight / 2 and ballPos.y < paddle2:GetPos().y + paddleHeight / 2 then -- Ball collided with paddle 2, reverse the x velocity ballVel.x = -ballVel.x ball:SetVelocity(ballVel) end -- Check for collisions with the top and bottom of the screen if ballPos.y < -100 or ballPos.y > 100 then -- Ball collided with the top or bottom of the screen, reverse the y velocity ballVel.y = -ballVel.y ball:SetVelocity(ballVel) end end -- Send the game state to the clients function SendGameState() for _, ply in pairs(player.GetAll()) do ply:Send("PongGameState", { paddle1Pos = paddle1:GetPos(), paddle2Pos = paddle2:GetPos(), ballPos = ball:GetPos(), ballVel = ball:GetVelocity() }) end end -- Main game loop function Think() UpdatePaddles() UpdateBall() SendGameState() end -- Call the Think function every frame- Create a new S&box project and add a
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.