RBS CODE ยท GAMEPAD

Two Controllers

Almost every gamepad helper takes an optional padIndex. Use 0 for the first controller and 1 for the second controller.

The easiest two-player setup

moveWithGamepad(
    "player1",
    8,
    "game",
    0
);

moveWithGamepad(
    "player2",
    8,
    "game",
    1
);

0 means first controller. 1 means second controller.

Buttons for different players

if (
    padButton(
        PAD.A,
        0
    )
) {
    // Player 1 action
}
if (
    padButton(
        PAD.A,
        1
    )
) {
    // Player 2 action
}

Rumble the correct controller

rumble(
    1,
    0.4,
    250,
    1
);

The final 1 says: vibrate Player 2's controller.

Complete working race

COPY WHOLE GAME
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Two Controller Race</title>
<style>
*{box-sizing:border-box}
body{margin:0;background:#eef2f7;color:#172033;font-family:Arial,sans-serif}
.wrap{width:760px;max-width:calc(100% - 24px);margin:22px auto}
h1{margin:0 0 6px}p{color:#64748b}
.hud{display:flex;justify-content:space-between;gap:8px;flex-wrap:wrap;background:white;padding:10px 13px;border-radius:12px 12px 0 0;font-weight:bold}
.message{background:#172033;color:white;text-align:center;padding:10px;font-weight:900}
.game{position:relative;height:410px;overflow:hidden;border:4px solid #172033;background:linear-gradient(135deg,#dff4ff,#f3e8ff)}
.sprite{position:absolute;width:50px;height:50px;font-size:40px;line-height:50px;text-align:center}
#goal{font-size:48px}
button{margin-top:10px;border:0;border-radius:9px;background:#4f46e5;color:white;padding:9px 13px;font-weight:bold}
</style>
</head>
<body>
<div class="wrap">
<h1>Two-Controller Race</h1>
<p>First controller moves Player 1. Second controller moves Player 2. Reach the flag first.</p>
<div class="hud">
<span id="pad1">P1: press controller button</span>
<span id="pad2">P2: press controller button</span>
</div>
<div id="message" class="message">READY</div>
<div id="game" class="game">
<div id="player1" class="sprite" style="left:50px;top:75px">๐Ÿš™</div>
<div id="player2" class="sprite" style="left:50px;top:285px">๐Ÿš—</div>
<div id="goal" class="sprite" style="left:655px;top:180px">๐Ÿ</div>
</div>
<button onclick="restartGame()">RESTART</button>
</div>

<script src="rbs-game-library.js"></script>
<script src="rbs-gamepad-library.js"></script>
<script>
let playing = true;

moveWithGamepad(
    "player1",
    7,
    "game",
    0
);

moveWithGamepad(
    "player2",
    7,
    "game",
    1
);

function gameLoop() {
    if (gamepadConnected(0)) {
        pad1.innerText =
            "PLAYER 1 CONNECTED";
    }

    if (gamepadConnected(1)) {
        pad2.innerText =
            "PLAYER 2 CONNECTED";
    }

    if (!playing) return;

    if (touching(player1, goal)) {
        playing = false;

        setText(
            message,
            "PLAYER 1 WINS!"
        );

        setEmoji(player1, "๐Ÿ†");

        rumble(
            0.8,
            0.3,
            250,
            0
        );
    }

    if (
        playing &&
        touching(player2, goal)
    ) {
        playing = false;

        setText(
            message,
            "PLAYER 2 WINS!"
        );

        setEmoji(player2, "๐Ÿ†");

        rumble(
            0.8,
            0.3,
            250,
            1
        );
    }
}

every(30, gameLoop);
</script>
</body>
</html>