Here's an example of how to create a multiplayer Pong game for s&box. I'll break it down into multiple files:
- First, create the game entity (
PongGame.cs):
using Sandbox;
public partial class PongGame : GameManager
{
public static PongGame Instance { get; private set; }
public PongBall Ball { get; private set; }
public PongPaddle LeftPaddle { get; private set; }
public PongPaddle RightPaddle { get; private set; }
public int LeftScore { get; private set; }
public int RightScore { get; private set; }
public override void Spawn()
{
base.Spawn();
Instance = this;
// Create the ball
Ball = new PongBall();
Ball.Position = new Vector3( 0, 0, 0 );
// Create paddles
LeftPaddle = new PongPaddle { Position = new Vector3( -300, 0, 0 ) };
RightPaddle = new PongPaddle { Position = new Vector3( 300, 0, 0 ) };
}
public void Score(bool leftScored)
{
if ( leftScored )
LeftScore++;
else
RightScore++;
ResetBall();
}
private void ResetBall()
{
Ball.Position = new Vector3( 0, 0, 0 );
Ball.Velocity = new Vector3( Game.Random.Float( -1, 1 ), Game.Random.Float( -1, 1 ), 0 ).Normal * 300;
}
}
- Create the ball entity (
PongBall.cs):
using Sandbox;
public partial class PongBall : Entity
{
public Vector3 Velocity { get; set; }
private const float Speed = 300f;
public override void Spawn()
{
base.Spawn();
SetModel( "models/ball.vmdl" );
Scale = 0.5f;
// Initialize random direction
Velocity = new Vector3( Game.Random.Float( -1, 1 ), Game.Random.Float( -1, 1 ), 0 ).Normal * Speed;
}
[Event.Tick]
public void Tick()
{
Position += Velocity * Time.Delta;
// Check for collisions with paddles
if ( CheckPaddleCollision( PongGame.Instance.LeftPaddle ) ||
CheckPaddleCollision( PongGame.Instance.RightPaddle ) )
{
Velocity = Velocity.WithX( -Velocity.x );
}
// Check for scoring
if ( Position.x < -400 )
PongGame.Instance.Score( false );
else if ( Position.x > 400 )
PongGame.Instance.Score( true );
// Bounce off top and bottom
if ( Position.y > 300 || Position.y < -300 )
Velocity = Velocity.WithY( -Velocity.y );
}
private bool CheckPaddleCollision( PongPaddle paddle )
{
var paddleBounds = new BBox(
paddle.Position - new Vector3( 5, 50, 5 ),
paddle.Position + new Vector3( 5, 50, 5 ) );
return paddleBounds.Contains( Position );
}
}
- Create the paddle entity (
PongPaddle.cs):
using Sandbox;
public partial class PongPaddle : Entity
{
private const float MoveSpeed = 200f;
private const float BoundaryLimit = 250f;
public override void Spawn()
{
base.Spawn();
SetModel( "models/paddle.vmdl" );
Scale = new Vector3( 0.2f, 1f, 0.2f );
}
public void Move( float direction )
{
var newPos = Position + new Vector3( 0, direction * MoveSpeed * Time.Delta, 0 );
newPos = newPos.WithY( Math.Clamp( newPos.y, -BoundaryLimit, BoundaryLimit ) );
Position = newPos;
}
}
- Create the player controller (
PongPlayer.cs):
using Sandbox;
public partial class PongPlayer : Player
{
private PongPaddle ControlledPaddle;
public override void Spawn()
{
base.Spawn();
// Assign paddle based on player count
if ( Client.All.Count == 1 )
ControlledPaddle = PongGame.Instance.LeftPaddle;
else
ControlledPaddle = PongGame.Instance.RightPaddle;
}
public override void Simulate( Client cl )
{
base.Simulate( cl );
if ( !IsServer )
return;
// Move paddle based on input
if ( Input.Down( InputButton.Forward ) )
ControlledPaddle.Move( 1 );
if ( Input.Down( InputButton.Back ) )
ControlledPaddle.Move( -1 );
}
}
- Create a HUD to display scores (
PongHUD.cs):
using Sandbox;
using Sandbox.UI;
public class PongHUD : HudEntity<RootPanel>
{
public PongHUD()
{
RootPanel.AddChild<ScoreDisplay>();
}
}
public class ScoreDisplay : Panel
{
private Label LeftScore;
private Label RightScore;
public ScoreDisplay()
{
LeftScore = Add.Label( "0", "score-left" );
RightScore = Add.Label( "0", "score-right" );
StyleSheet.Load( "/ui/PongHUD.scss" );
}
public override void Tick()
{
base.Tick();
if ( PongGame.Instance == null )
return;
LeftScore.Text = PongGame.Instance.LeftScore.ToString();
RightScore.Text = PongGame.Instance.RightScore.ToString();
}
}
- Create a stylesheet for the HUD (
PongHUD.scss):
ScoreDisplay {
position: absolute;
width: 100%;
height: 100%;
.score-left {
position: absolute;
left: 25%;
top: 50px;
font-size: 48px;
color: white;
}
.score-right {
position: absolute;
right: 25%;
top: 50px;
font-size: 48px;
color: white;
}
}
To use this game:
- Create the necessary models (ball.vmdl and paddle.vmdl)
- Set up the game mode in your s&box project
- Make sure all the networking is properly set up
- Players can control their paddles using the forward and back inputs
This is a basic implementation and you might want to add:
- Sound effects
- Visual effects
- Better collision detection
- Menu system
- Round system
- More sophisticated paddle movement
- Power-ups
- Better networking code
- Proper game start/end states
Remember to handle proper networking and client/server architecture according to s&box's requirements. You might need to add network attributes and properly sync the entities between clients.
Also, you'll need to create appropriate models for the ball and paddles, or use primitive shapes if available in s&box.