Please bookmark this page to avoid losing your image tool!

Battleship Image Game

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
function processImage(originalImg, gridSizeStr = "10") {
    // Parse grid size safely
    const gridSize = Math.max(8, Math.min(30, parseInt(gridSizeStr, 10) || 10));

    // Create the canvas and context
    const canvas = document.createElement("canvas");
    const ctx = canvas.getContext("2d");
    
    // Set canvas dimensions equal to the original image dimensions
    canvas.width = originalImg.width;
    canvas.height = originalImg.height;

    // Game state: 0 = hidden sea, 1 = hidden ship, 2 = miss, 3 = hit
    const board = Array.from({ length: gridSize }, () => Array(gridSize).fill(0));
    
    let hits = 0;
    let moves = 0;
    let totalHits = 0;
    let gameOver = false;

    // Standard Battleship ship sizes
    const shipSizes = [5, 4, 3, 3, 2];

    // Place ships randomly on the grid
    for (let size of shipSizes) {
        let placed = false;
        let attempts = 0;
        
        while (!placed && attempts < 1000) {
            attempts++;
            let isVertical = Math.random() > 0.5;
            let startX = Math.floor(Math.random() * gridSize);
            let startY = Math.floor(Math.random() * gridSize);

            // Check boundaries
            if (isVertical && startY + size > gridSize) continue;
            if (!isVertical && startX + size > gridSize) continue;

            // Check collisions with other ships (including a 1-cell buffer)
            let collision = false;
            for (let i = 0; i < size; i++) {
                let checkX = startX + (isVertical ? 0 : i);
                let checkY = startY + (isVertical ? i : 0);

                // Surrounding bounds check
                for (let dx = -1; dx <= 1; dx++) {
                    for (let dy = -1; dy <= 1; dy++) {
                        let nx = checkX + dx;
                        let ny = checkY + dy;
                        if (nx >= 0 && nx < gridSize && ny >= 0 && ny < gridSize) {
                            if (board[nx][ny] === 1) {
                                collision = true;
                            }
                        }
                    }
                }
            }

            if (collision) continue;

            // Place the ship
            for (let i = 0; i < size; i++) {
                let px = startX + (isVertical ? 0 : i);
                let py = startY + (isVertical ? i : 0);
                board[px][py] = 1;
            }
            placed = true;
        }
    }

    // Count total ship cells placed successfully
    for (let x = 0; x < gridSize; x++) {
        for (let y = 0; y < gridSize; y++) {
            if (board[x][y] === 1) totalHits++;
        }
    }

    // Drawing function to refresh the view
    function drawGame() {
        // Draw the background image
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

        const cw = canvas.width / gridSize;
        const ch = canvas.height / gridSize;

        // Draw grid and markers
        for (let x = 0; x < gridSize; x++) {
            for (let y = 0; y < gridSize; y++) {
                let cellX = x * cw;
                let cellY = y * ch;

                if (board[x][y] === 0 || board[x][y] === 1) {
                    // Hidden cells (dark blue overlay simulating sea/radar, alternating for a checkerboard effect)
                    ctx.fillStyle = ((x + y) % 2 === 0) ? 'rgba(15, 30, 45, 0.95)' : 'rgba(20, 35, 55, 0.95)';
                    ctx.fillRect(cellX, cellY, cw, ch);
                    
                    // Subtle border
                    ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
                    ctx.lineWidth = 1;
                    ctx.strokeRect(cellX, cellY, cw, ch);

                } else if (board[x][y] === 2) {
                    // Miss (reveals image beneath + draws white dot)
                    ctx.fillStyle = 'rgba(255, 255, 255, 0.15)';
                    ctx.fillRect(cellX, cellY, cw, ch);
                    
                    ctx.beginPath();
                    ctx.arc(cellX + cw / 2, cellY + ch / 2, Math.min(cw, ch) * 0.15, 0, Math.PI * 2);
                    ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
                    ctx.fill();

                } else if (board[x][y] === 3) {
                    // Hit (tints cell red + draws red X)
                    ctx.fillStyle = 'rgba(255, 0, 0, 0.4)';
                    ctx.fillRect(cellX, cellY, cw, ch);
                    
                    let padX = cw * 0.25;
                    let padY = ch * 0.25;
                    ctx.beginPath();
                    ctx.moveTo(cellX + padX, cellY + padY);
                    ctx.lineTo(cellX + cw - padX, cellY + ch - padY);
                    ctx.moveTo(cellX + cw - padX, cellY + padY);
                    ctx.lineTo(cellX + padX, cellY + ch - padY);
                    
                    ctx.strokeStyle = 'rgba(255, 0, 0, 0.95)';
                    ctx.lineWidth = Math.max(3, Math.min(cw, ch) * 0.12);
                    ctx.lineCap = 'round';
                    ctx.stroke();
                }
            }
        }

        // Draw HUD
        ctx.save();
        ctx.fillStyle = '#ffffff';
        ctx.font = 'bold 24px sans-serif';
        ctx.textAlign = 'left';
        ctx.textBaseline = 'top';
        ctx.shadowColor = '#000000';
        ctx.shadowBlur = 4;
        ctx.fillText(`Target Ships: ${hits} / ${totalHits}   Moves: ${moves}`, 15, 15);
        ctx.restore();

        // Draw Game Over sequence
        if (gameOver) {
            ctx.save();
            ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            ctx.fillStyle = '#ffcc00';
            ctx.font = 'bold ' + Math.max(20, Math.floor(canvas.width / 12)) + 'px sans-serif';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.shadowColor = '#000000';
            ctx.shadowBlur = 8;
            ctx.fillText('MISSION ACCOMPLISHED!', canvas.width / 2, canvas.height / 2 - 20);
            
            ctx.fillStyle = '#ffffff';
            ctx.font = 'bold ' + Math.max(16, Math.floor(canvas.width / 24)) + 'px sans-serif';
            ctx.fillText(`All ships destroyed in ${moves} moves`, canvas.width / 2, canvas.height / 2 + 30);
            ctx.restore();
        }
    }

    // Click logic
    canvas.addEventListener("click", function(e) {
        if (gameOver) return;

        // Calculate click coordinates relative to canvas internal scaling
        const rect = canvas.getBoundingClientRect();
        const scaleX = canvas.width / rect.width;
        const scaleY = canvas.height / rect.height;
        
        const cx = (e.clientX - rect.left) * scaleX;
        const cy = (e.clientY - rect.top) * scaleY;

        const cw = canvas.width / gridSize;
        const ch = canvas.height / gridSize;

        const gridX = Math.floor(cx / cw);
        const gridY = Math.floor(cy / ch);

        // Prevent acting on out of bounds
        if (gridX < 0 || gridX >= gridSize || gridY < 0 || gridY >= gridSize) return;

        // Check the cell if it hasn't been clicked yet
        if (board[gridX][gridY] === 0 || board[gridX][gridY] === 1) {
            moves++;
            if (board[gridX][gridY] === 1) {
                board[gridX][gridY] = 3; // Hit
                hits++;
                if (hits >= totalHits) {
                    gameOver = true;
                }
            } else {
                board[gridX][gridY] = 2; // Miss
            }
            drawGame();
        }
    });

    // Make canvas cursor interactive
    canvas.style.cursor = "crosshair";

    // Initialize graphic state
    drawGame();

    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Battleship Image Game tool transforms any uploaded image into an interactive, grid-based hidden object game. It overlays a customizable grid onto your image, concealing it with a radar-like interface where virtual ships are randomly placed. Users can play by clicking on the grid to find the hidden ships, receiving visual feedback for hits and misses. This tool is ideal for creating engaging social media content, personalized digital games, or interactive educational activities using custom imagery.

Leave a Reply

Your email address will not be published. Required fields are marked *