Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

pls code me a full underwater submarine game with levels and start screen and lighting…

  1. Assistant

    Hello! How can I assist you today?

  2. User

    pls code me a full underwater submarine game with levels and start screen and lighting and everything in just html pls (:

  3. Assistant

    Creating a full-featured underwater submarine game with levels, start screen, lighting effects, and everything purely in a single HTML file is quite ambitious due to the complexity involved. However, I can provide you with a relatively complete and simple 2D submarine game built with just HTML, CSS, and JavaScript in a single HTML file. It will include:

    • A start screen
    • Basic gameplay underwater
    • Multiple levels
    • Basic lighting effect
    • Score display

    You can open this in any modern browser.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Underwater Submarine Game</title>
    <style>
      html, body {
        margin: 0; padding: 0; height: 100%; background: #001f3f; /* deep ocean */
        overflow: hidden;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        color: white;
      }
      #gameCanvas {
        display: block;
        margin: 0 auto;
        background: #00264d; /* Ocean blue */
        box-shadow: 0 0 20px #005580 inset;
      }
      #ui {
        position: fixed;
        top: 10px;
        left: 50%;
        transform: translateX(-50%);
        user-select: none;
        text-align: center;
        pointer-events: none;
      }
      #startScreen, #gameOverScreen, #levelScreen {
        position: fixed;
        top: 0; left: 0; bottom: 0; right: 0;
        background: rgba(0, 0, 30, 0.85);
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        pointer-events: auto;
        z-index: 10;
      }
      button {
        font-size: 18px;
        padding: 10px 25px;
        background: #005580;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        color: white;
        margin-top: 15px;
        transition: background 0.3s;
      }
      button:hover {
        background: #0077cc;
      }
      h1 {
        font-weight: 900;
        margin: 0 0 10px 0;
      }
      p {
        margin: 5px 0 0 0;
      }
    </style>
    </head>
    <body>
    <canvas id="gameCanvas" width="800" height="600"></canvas>
    
    <div id="ui">
      <div>Level: <span id="level">1</span> | Score: <span id="score">0</span></div>
    </div>
    
    <div id="startScreen">
      <h1>Underwater Submarine</h1>
      <p>Use arrow keys or WASD to move</p>
      <p>Collect treasures, avoid mines</p>
      <button id="startBtn">Start Game</button>
    </div>
    
    <div id="levelScreen" style="display:none;">
      <h1>Level <span id="levelNumber">1</span></h1>
      <p>Get ready!</p>
      <button id="nextLevelBtn">Start Level</button>
    </div>
    
    <div id="gameOverScreen" style="display:none;">
      <h1>Game Over</h1>
      <p>Your final score is <span id="finalScore">0</span></p>
      <button id="restartBtn">Restart Game</button>
    </div>
    
    <script>
    (() => {
      const canvas = document.getElementById('gameCanvas');
      const ctx = canvas.getContext('2d');
      const width = canvas.width;
      const height = canvas.height;
    
      const startScreen = document.getElementById('startScreen');
      const startBtn = document.getElementById('startBtn');
    
      const gameOverScreen = document.getElementById('gameOverScreen');
      const restartBtn = document.getElementById('restartBtn');
      const finalScoreElem = document.getElementById('finalScore');
    
      const levelScreen = document.getElementById('levelScreen');
      const nextLevelBtn = document.getElementById('nextLevelBtn');
      const levelNumberElem = document.getElementById('levelNumber');
      
      const levelDisplay = document.getElementById('level');
      const scoreDisplay = document.getElementById('score');
    
      // Game variables
      let keys = {};
      const submarine = {
        x: width/2,
        y: height/2,
        width: 60,
        height: 30,
        speed: 3,
        angle: 0,
      };
    
      let treasures = [];
      let mines = [];
      const treasureSize = 20;
      const mineSize = 25;
    
      let bubbles = [];
    
      let level = 1;
      let score = 0;
    
      let gameState = 'start'; // 'start', 'playing', 'levelIntro', 'gameOver'
      let levelIntroTimeoutId = null;
    
      // Lighting parameters
      // We'll do a simple light circle around sub
    
      // Utility
      function randomInt(min, max) {
        return Math.floor(Math.random()*(max-min+1)) + min;
      }
    
      function dist(x1,y1,x2,y2) {
        return Math.sqrt((x1-x2)**2 + (y1-y2)**2);
      }
    
      // Entity generation
    
      function placeTreasures(amount) {
        treasures = [];
        for(let i=0; i<amount; i++) {
          let t = {
            x: randomInt(treasureSize, width - treasureSize),
            y: randomInt(treasureSize, height - treasureSize),
            collected: false,
          };
          treasures.push(t);
        }
      }
    
      function placeMines(amount) {
        mines = [];
        for(let i=0; i<amount; i++) {
          let m = {
            x: randomInt(mineSize, width - mineSize),
            y: randomInt(mineSize, height - mineSize),
          };
          mines.push(m);
        }
      }
    
      // Bubbles for visual effect
      function createBubble() {
        const b = {
          x: submarine.x - submarine.width/2 + 10,
          y: submarine.y + randomInt(5, submarine.height),
          radius: randomInt(2,5)/2,
          speed: Math.random()*1 + 0.5,
          alpha: 1,
        };
        bubbles.push(b);
      }
      function updateBubbles() {
        for(let i=bubbles.length-1; i>=0; i--) {
          let b = bubbles[i];
          b.x -= b.speed*1.5;
          b.y -= b.speed/2;
          b.alpha -= 0.02;
          if(b.alpha <= 0 || b.x < 0) bubbles.splice(i,1);
        }
      }
      function drawBubbles() {
        bubbles.forEach(b => {
          ctx.beginPath();
          ctx.fillStyle = `rgba(255, 255, 255, ${b.alpha*0.5})`;
          ctx.arc(b.x, b.y, b.radius, 0, 2*Math.PI);
          ctx.fill();
        });
      }
    
      // Input handlers
      window.addEventListener('keydown', e => {
        keys[e.key.toLowerCase()] = true;
        if(e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'ArrowLeft' || e.key === 'ArrowRight'){
          e.preventDefault(); // prevent scroll
        }
      });
      window.addEventListener('keyup', e => {
        keys[e.key.toLowerCase()] = false;
      });
    
      // Draw submarine
      function drawSubmarine() {
        ctx.save();
        ctx.translate(submarine.x, submarine.y);
        ctx.rotate(submarine.angle);
    
        // Submarine body
        const bodyGradient = ctx.createLinearGradient(-30, 0, 30, 0);
        bodyGradient.addColorStop(0, '#0a6c95');
        bodyGradient.addColorStop(0.5, '#005580');
        bodyGradient.addColorStop(1, '#003f5c');
    
        ctx.fillStyle = bodyGradient;
        ctx.strokeStyle = '#002d40';
        ctx.lineWidth = 3;
    
        ctx.beginPath();
        ctx.ellipse(0, 0, submarine.width/2, submarine.height/2, 0, 0, 2*Math.PI);
        ctx.fill();
        ctx.stroke();
    
        // Window
        ctx.fillStyle = '#a0d8ff';
        ctx.beginPath();
        ctx.arc(10, 0, 10, 0, 2*Math.PI);
        ctx.fill();
        ctx.strokeStyle = '#7ec0ee';
        ctx.lineWidth = 2;
        ctx.stroke();
    
        // Propeller
        ctx.fillStyle = '#004060';
        ctx.beginPath();
        ctx.moveTo(-submarine.width/2 - 10, 0);
        ctx.lineTo(-submarine.width/2 - 30, -10);
        ctx.lineTo(-submarine.width/2 - 30, 10);
        ctx.closePath();
        ctx.fill();
    
        ctx.restore();
      }
    
      // Draw treasure
      function drawTreasure(t) {
        if(t.collected) return;
        ctx.save();
        ctx.translate(t.x, t.y);
        // Twinkle effect via alpha
        const twinkle = 0.7 + 0.3*Math.sin(Date.now()/200 + t.x);
        ctx.fillStyle = `rgba(255, 215, 0, ${twinkle})`; // gold color
        ctx.beginPath();
        // Simple star shape
        for(let i=0; i<5; i++) {
          ctx.lineTo(0, treasureSize/2);
          ctx.translate(0, treasureSize/2);
          ctx.rotate((Math.PI*2)/10);
          ctx.lineTo(0, -treasureSize/2);
          ctx.translate(0, -treasureSize/2);
          ctx.rotate(-(Math.PI*6)/10);
        }
        ctx.closePath();
        ctx.fill();
        ctx.restore();
      }
    
      // Draw mines
      function drawMine(m) {
        ctx.save();
        ctx.translate(m.x, m.y);
    
        // Circle base
        let grad = ctx.createRadialGradient(0, 0, 5, 0, 0, mineSize/2);
        grad.addColorStop(0, '#660000');
        grad.addColorStop(1, '#330000');
        ctx.fillStyle = grad;
    
        ctx.beginPath();
        ctx.arc(0, 0, mineSize/2, 0, 2*Math.PI);
        ctx.fill();
    
        // Spikes
        ctx.strokeStyle = '#990000';
        ctx.lineWidth = 3;
        for(let i=0; i<8; i++) {
          let angle = (Math.PI*2/8)*i;
          let x1 = Math.cos(angle)*(mineSize/2 - 5);
          let y1 = Math.sin(angle)*(mineSize/2 - 5);
          let x2 = Math.cos(angle)*(mineSize/2 + 7);
          let y2 = Math.sin(angle)*(mineSize/2 + 7);
          ctx.beginPath();
          ctx.moveTo(x1,y1);
          ctx.lineTo(x2,y2);
          ctx.stroke();
        }
        ctx.restore();
      }
    
      // Draw lighting effect - dark environment with light around submarine
      function drawLighting() {
        const gradient = ctx.createRadialGradient(submarine.x, submarine.y, 40, submarine.x, submarine.y, 200);
        gradient.addColorStop(0, 'rgba(0, 128, 255, 0.8)');
        gradient.addColorStop(1, 'rgba(0, 16, 40, 0.95)');
    
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, width, height);
      }
    
      // Draw ocean floor for ambiance
      function drawOceanFloor() {
        ctx.fillStyle = '#003344';
        ctx.fillRect(0, height - 60, width, 60);
    
        // Simple stones/rocks
        for(let i=0; i<15; i++) {
          const x = i*55 + (Date.now()/100)%55;
          const y = height - 30 + 10*Math.sin(x/10 + Date.now()/400);
          ctx.beginPath();
          ctx.fillStyle = '#004466';
          ctx.ellipse(x, y, 20, 10, 0, 0, 2*Math.PI);
          ctx.fill();
    
          ctx.beginPath();
          ctx.fillStyle = '#002833';
          ctx.ellipse(x + 10, y + 2, 10, 6, 0, 0, 2*Math.PI);
          ctx.fill();
        }
      }
    
      // Collision detections
      function isCollidingCircleRect(cx, cy, radius, rx, ry, rw, rh) {
        // Find closest point to circle center within the rectangle
        let closestX = Math.max(rx, Math.min(cx, rx + rw));
        let closestY = Math.max(ry, Math.min(cy, ry + rh));
    
        // Calculate distance from closest point to circle center
        let distanceX = cx - closestX;
        let distanceY = cy - closestY;
    
        return (distanceX*distanceX + distanceY*distanceY) < (radius*radius);
      }
    
      function isCollidingCircles(x1, y1, r1, x2, y2, r2) {
        return dist(x1,y1,x2,y2) < (r1 + r2);
      }
    
      // Update game objects and game logic
      function update() {
        if(gameState !== 'playing') return;
    
        // Movement
        let dx = 0, dy = 0;
        if(keys['arrowup'] || keys['w']) dy -= 1;
        if(keys['arrowdown'] || keys['s']) dy += 1;
        if(keys['arrowleft'] || keys['a']) dx -= 1;
        if(keys['arrowright'] || keys['d']) dx += 1;
    
        // Normalize diagonal speed
        if(dx !== 0 && dy !== 0) {
          dx /= Math.sqrt(2);
          dy /= Math.sqrt(2);
        }
        submarine.x += dx * submarine.speed;
        submarine.y += dy * submarine.speed;
    
        // Clamp submarine inside canvas
        submarine.x = Math.min(Math.max(submarine.width/2, submarine.x), width - submarine.width/2);
        submarine.y = Math.min(Math.max(submarine.height/2, submarine.y), height - submarine.height/2);
    
        // Update submarine angle to facing direction
        if(dx !== 0 || dy !== 0) {
          submarine.angle = Math.atan2(dy, dx);
        }
    
        // Bubble creation
        if(Math.random() < 0.15) {
          createBubble();
        }
        updateBubbles();
    
        // Check treasure collection
        treasures.forEach(t => {
          if(!t.collected && dist(submarine.x, submarine.y, t.x, t.y) < submarine.width/2 + treasureSize/2) {
            t.collected = true;
            score += 10;
            scoreDisplay.textContent = score;
          }
        });
    
        // Check mine collisions
        for(let m of mines) {
          if(isCollidingCircles(submarine.x, submarine.y, submarine.width/2, m.x, m.y, mineSize/2)) {
            // Game Over
            endGame();
            break;
          }
        }
    
        // Check level completion
        if(treasures.every(t => t.collected)) {
          // Go to next level
          startLevelIntro(level+1);
        }
      }
    
      // Draw all game elements
      function draw() {
        // Dark background first
        ctx.fillStyle = '#001a33';
        ctx.fillRect(0, 0, width, height);
    
        // Draw ocean floor
        drawOceanFloor();
    
        // Draw treasures
        treasures.forEach(drawTreasure);
    
        // Draw mines
        mines.forEach(drawMine);
    
        // Draw bubbles
        drawBubbles();
    
        // Draw submarine on top
        drawSubmarine();
    
        // Draw lighting on top for effect
        drawLighting();
      }
    
      // Start the game playing state at a level
      function startLevel(levelNum) {
        level = levelNum;
        levelDisplay.textContent = level;
        scoreDisplay.textContent = score;
        // Reset submarine position center-left
        submarine.x = width * 0.2;
        submarine.y = height / 2;
    
        // Increase difficulty with each level
        // More treasures and more mines as level goes up
        const treasureCount = Math.min(5 + level, 20);
        const mineCount = Math.min(level * 3, 25);
        placeTreasures(treasureCount);
        placeMines(mineCount);
      }
    
      // Start the intro screen for a level
      function startLevelIntro(levelNum) {
        gameState = 'levelIntro';
        levelNumberElem.textContent = levelNum;
    
        levelScreen.style.display = 'flex';
        startScreen.style.display = 'none';
        gameOverScreen.style.display = 'none';
    
        nextLevelBtn.onclick = () => {
          levelScreen.style.display = 'none';
          gameState = 'playing';
          startLevel(levelNum);
        };
      }
    
      // Start the game from beginning
      function startGame() {
        score = 0;
        level = 1;
        levelDisplay.textContent = level;
        scoreDisplay.textContent = score;
    
        startScreen.style.display = 'none';
        levelScreen.style.display = 'none';
        gameOverScreen.style.display = 'none';
    
        startLevelIntro(level);
      }
    
      // End the game and show game over screen
      function endGame() {
        gameState = 'gameOver';
        finalScoreElem.textContent = score;
    
        gameOverScreen.style.display = 'flex';
        startScreen.style.display = 'none';
        levelScreen.style.display = 'none';
      }
    
      startBtn.onclick = startGame;
      restartBtn.onclick = startGame;
    
      // Game loop
      function loop() {
        update();
        draw();
        requestAnimationFrame(loop);
      }
    
      loop();
    
    })();
    </script>
    </body>
    </html>
    

    Instructions:

    • Open the file in a modern web browser like Chrome, Firefox, or Edge.
    • On the start screen, click Start Game.
    • Move the submarine using arrow keys or WASD.
    • Collect all gold treasures on a level to proceed to the next.
    • Avoid mines — touching a mine ends the game.
    • You can restart after Game Over from the button.
    • The light gradient effect simulates underwater lighting around the submarine.
    • Score increases by 10 for each treasure collected.
    • Each new level adds more mines and treasures, increasing difficulty.

    This is a minimal yet complete example. If you want to enhance it (e.g., add sound, better graphics, more complex physics), I'd recommend using separate JS files or game frameworks. But this works all inside one HTML file with clear code to learn and expand!

    Let me know if you'd like me to help add anything else!