Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

code me in html/css/js a 3D snake game, put all the code in a single html file

  1. User

    code me in html/css/js a 3D snake game, put all the code in a single html file

  2. Assistant
    // Set canvas size
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    
    window.addEventListener('resize', function(){
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
    });
    
    // Game variables
    const gridSize = 20; // Size of the grid
    const cubeSize = 20; // Size of each cube for rendering
    let score = 0;
    
    // Camera position
    const camera = { x: gridSize / 2, y: gridSize / 2 - 5, z: -30 };
    
    // Snake initialization
    let snake = [{ x: Math.floor(gridSize / 2), y: Math.floor(gridSize / 2), z: Math.floor(gridSize / 2) }];
    let direction = { x: 0, y: 0, z: 1 }; // Initial direction moving along z-axis
    
    // Food initialization
    let food = randomPosition();
    
    // Handle keyboard input
    document.addEventListener('keydown', function(e) {
      switch(e.key) {
        case 'w': // Up
          if (direction.y !== 1) direction = { x: 0, y: -1, z: 0 };
          break;
        case 's': // Down
          if (direction.y !== -1) direction = { x: 0, y: 1, z: 0 };
          break;
        case 'a': // Left
          if (direction.x !== 1) direction = { x: -1, y: 0, z: 0 };
          break;
        case 'd': // Right
          if (direction.x !== -1) direction = { x: 1, y: 0, z: 0 };
          break;
        case 'q': // Backward
          if (direction.z !== 1) direction = { x: 0, y: 0, z: -1 };
          break;
        case 'e': // Forward
          if (direction.z !== -1) direction = { x: 0, y: 0, z: 1 };
          break;
      }
    });
    
    // Generate random position for food
    function randomPosition() {
      return {
        x: Math.floor(Math.random() * gridSize),
        y: Math.floor(Math.random() * gridSize),
        z: Math.floor(Math.random() * gridSize)
      };
    }
    
    // Game update function
    function update() {
      let head = snake[0];
      let newHead = {
        x: (head.x + direction.x + gridSize) % gridSize,
        y: (head.y + direction.y + gridSize) % gridSize,
        z: (head.z + direction.z + gridSize) % gridSize
      };
    
      // Check for collision with self
      for (let part of snake) {
        if (part.x === newHead.x && part.y === newHead.y && part.z === newHead.z) {
          // Game over
          alert("Game Over! Your score: " + score);
          // Restart the game
          snake = [{ x: Math.floor(gridSize / 2), y: Math.floor(gridSize / 2), z: Math.floor(gridSize / 2) }];
          direction = { x: 0, y: 0, z: 1 };
          food = randomPosition();
          score = 0;
          document.getElementById('score').innerText = "Score: " + score;
          return;
        }
      }
    
      snake.unshift(newHead);
    
      // Check if food is eaten
      if (newHead.x === food.x && newHead.y === food.y && newHead.z === food.z) {
        score++;
        document.getElementById('score').innerText = "Score: " + score;
        food = randomPosition();
      } else {
        snake.pop();
      }
    }
    
    // Projection function (Isometric projection)
    function project(point) {
      const scale = cubeSize;
      let x = (point.x - point.z) * scale + canvas.width / 2;
      let y = (point.y - point.z) * scale + canvas.height / 2;
      return { x, y };
    }
    
    // Draw cube at a given point with given color
    function drawCube(point, color) {
      const size = 1;
      let vertices = getCubeVertices(point, size);
      let projected = vertices.map(v => project(v));
    
      ctx.fillStyle = color;
      ctx.strokeStyle = '#000';
      ctx.beginPath();
      // Front face
      ctx.moveTo(projected[0].x, projected[0].y);
      ctx.lineTo(projected[1].x, projected[1].y);
      ctx.lineTo(projected[2].x, projected[2].y);
      ctx.lineTo(projected[3].x, projected[3].y);
      ctx.closePath();
      ctx.fill();
      ctx.stroke();
    
      // Top face
      ctx.beginPath();
      ctx.moveTo(projected[0].x, projected[0].y);
      ctx.lineTo(projected[1].x, projected[1].y);
      ctx.lineTo(projected[5].x, projected[5].y);
      ctx.lineTo(projected[4].x, projected[4].y);
      ctx.closePath();
      ctx.fill();
      ctx.stroke();
    
      // Side face
      ctx.beginPath();
      ctx.moveTo(projected[1].x, projected[1].y);
      ctx.lineTo(projected[2].x, projected[2].y);
      ctx.lineTo(projected[6].x, projected[6].y);
      ctx.lineTo(projected[5].x, projected[5].y);
      ctx.closePath();
      ctx.fill();
      ctx.stroke();
    }
    
    // Get cube vertices
    function getCubeVertices(point, size) {
      let x = point.x;
      let y = point.y;
      let z = point.z;
      let s = size;
    
      return [
        { x: x,    y: y,    z: z },
        { x: x+s,  y: y,    z: z },
        { x: x+s,  y: y+s,  z: z },
        { x: x,    y: y+s,  z: z },
        { x: x,    y: y,    z: z+s },
        { x: x+s,  y: y,    z: z+s },
        { x: x+s,  y: y+s,  z: z+s },
        { x: x,    y: y+s,  z: z+s }
      ];
    }
    
    // Game render function
    function render() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    
      // Combine snake and food for rendering
      let objects = [...snake, food];
    
      // Sort objects by depth for proper rendering
      objects.sort((a, b) => (b.x + b.y + b.z) - (a.x + a.y + a.z));
    
      for (let i = 0; i < objects.length; i++) {
        let obj = objects[i];
        let color;
        if (i < snake.length) {
          color = 'limegreen';
        } else {
          color = 'red';
        }
        drawCube(obj, color);
      }
    }
    
    // Game loop
    function gameLoop() {
      update();
      render();
      setTimeout(gameLoop, 100); // Snake speed (adjust as needed)
    }
    
    // Start the game
    gameLoop();