Back to Portfolio

Inspired by Grok 3

Break-Pong Multiplayer

Deep Dive into Code & Game Mechanics

Node.js Socket.io HTML5 Canvas Real-time multiplayer
Break-Pong Multiplayer Screenshot

Project Overview

Break-Pong Multiplayer is an innovative fusion of classic pong and brick-breaking games, where players engage in real-time multiplayer matches controlling paddles to deflect a ball, break bricks, and activate power-ups that dynamically alter gameplay.

The project is structured using a robust server-client architecture. On the server side, Node.js, Express, and Socket.io manage game rooms, real-time physics, and state synchronization. On the client side, HTML5 Canvas renders the game while JavaScript handles user interactions.

Game Screenshot

Take a look at the game in action:

Break-Pong Multiplayer Screenshot

Architecture & Data Flow

The game is divided into two main components:

Server-side Implementation: Room Creation & Game Loop

The server initializes game rooms with default settings and runs a continuous loop (every 30ms) that updates the game state, processes collisions, and synchronizes gameplay via Socket.io.

server.js

// Initialize a new game room with default settings
function createRoom() {
  return {
    players: {},                  // Stores data for Player 1 and Player 2
    spectators: [],               // List for spectators
    playersReady: { player1: false, player2: false },
    gameStarted: false,
    paused: false,
    settings: {
      ballSpeed: DEFAULT_BALL_SPEED,
      paddleHeight: DEFAULT_PADDLE_HEIGHT,
      brickRows: DEFAULT_BRICK_ROWS,
      gameMode: "classic"         // Modes: classic, team, cooperative
    },
    gameState: {
      leftPaddle: { y: 250, height: DEFAULT_PADDLE_HEIGHT },
      rightPaddle: { y: 250, height: DEFAULT_PADDLE_HEIGHT },
      ball: {
        x: DEFAULT_CANVAS_WIDTH / 2,
        y: DEFAULT_CANVAS_HEIGHT / 2,
        vx: DEFAULT_BALL_SPEED,
        vy: DEFAULT_BALL_SPEED
      },
      scores: { left: 0, right: 0 },
      bricks: [],
      powerUps: []
    },
    scoreboard: { player1Wins: 0, player2Wins: 0 },
    chatHistory: []
  };
}
        

The server then runs a game loop every 30ms that updates the ball's position, handles collisions, and broadcasts the updated game state to all clients.

server.js

setInterval(() => {
  for (const roomId in rooms) {
    const room = rooms[roomId];
    if (!room.gameStarted) continue;
    if (room.paused) {
      io.to(roomId).emit("gamePaused", true);
      continue;
    } else {
      io.to(roomId).emit("gamePaused", false);
    }
    let gameState = room.gameState;
    // Update ball position based on its velocity
    gameState.ball.x += gameState.ball.vx;
    gameState.ball.y += gameState.ball.vy;
    // Bounce off top/bottom walls
    if (gameState.ball.y - DEFAULT_BALL_SIZE / 2 < 0 ||
        gameState.ball.y + DEFAULT_BALL_SIZE / 2 > DEFAULT_CANVAS_HEIGHT) {
      gameState.ball.vy *= -1;
    }
    // Additional collision detection for paddles & bricks follows...
    io.to(roomId).emit("gameState", gameState);
  }
}, 30);
        

Client-side Implementation: Joining a Room & Rendering

On the client side, users join game rooms and interact via an HTML5 Canvas interface. The function below demonstrates how a player is connected to a room:

script.js

// Prompt the user for a nickname and join a game room
function joinRoom(roomId) {
  myNickname = prompt("Enter your nickname:", "Player") || "Player";
  socket.emit("joinRoom", { roomId, nickname: myNickname });
  currentRoom = roomId;
  lobbyDiv.style.display = "none";
  gameContainer.style.display = "block";
  readyButton.style.display = "block";
}
        

After joining, the client enters a render loop to continuously update the canvas with game elements such as the ball, paddles, bricks, and power-ups.

script.js

// Render function for drawing game elements on the canvas
function render() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  // Draw intact bricks
  gameState.bricks.forEach(brick => {
    if (brick.intact) {
      ctx.fillStyle = "#E74C3C";
      ctx.fillRect(brick.x, brick.y, 40, 20);
      ctx.strokeStyle = "#C0392B";
      ctx.strokeRect(brick.x, brick.y, 40, 20);
    }
  });
  // Draw active power-ups
  gameState.powerUps.forEach(pu => {
    if (pu.active) {
      ctx.fillStyle = pu.type === "slowBall" ? "#3498DB" : "#9B59B6";
      ctx.beginPath();
      ctx.arc(pu.x, pu.y, 10, 0, Math.PI * 2);
      ctx.fill();
    }
  });
  // Draw paddles and ball
  ctx.fillStyle = "#fff";
  ctx.fillRect(LEFT_PADDLE_X, gameState.leftPaddle.y, PADDLE_WIDTH, parseInt(paddleHeightInput.value) || 100);
  ctx.fillRect(RIGHT_PADDLE_X, gameState.rightPaddle.y, PADDLE_WIDTH, parseInt(paddleHeightInput.value) || 100);
  ctx.fillStyle = "#F1C40F";
  ctx.beginPath();
  ctx.arc(gameState.ball.x, gameState.ball.y, BALL_SIZE / 2, 0, Math.PI * 2);
  ctx.fill();
  // Update score display accordingly...
}
requestAnimationFrame(gameLoop);
        

Detailed Game Mechanics

The game mechanics are designed around realistic physics and real-time interaction:

Future Enhancements & Roadmap

Planned improvements include enhanced collision detection, additional power-up types, improved mobile responsiveness with custom control layouts, and advanced leaderboards.

Experience Break-Pong Multiplayer

Ready to dive in? Try the live demo or run the project locally to experience the full multiplayer action.