双人贪吃蛇 HTML5 版

  • Player 1 uses W, A, S, D keys to control their snake (blue).

  • Player 2 uses U, H, J, K keys to control their snake (red).

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Two-Player Snake Game</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; font-family: Arial, sans-serif; } #gameCanvas { border: 2px solid #333; } #scoreBoard { position: absolute; top: 10px; left: 10px; font-size: 18px; } </style> </head> <body>
    Player 1 Score: 0
    Player 2 Score: 0
    <canvas id="gameCanvas" width="800" height="800"></canvas>
    复制代码
      <script>
          const canvas = document.getElementById('gameCanvas');
          const ctx = canvas.getContext('2d');
          const gridSize = 20;
          const tileCount = canvas.width / gridSize;
    
          let snake1 = [
              {x: 10, y: 10},
              {x: 9, y: 10},
              {x: 8, y: 10}
          ];
          let snake2 = [
              {x: 30, y: 30},
              {x: 31, y: 30},
              {x: 32, y: 30}
          ];
    
          let dx1 = 1;
          let dy1 = 0;
          let dx2 = -1;
          let dy2 = 0;
    
          let food = getRandomFood();
          let score1 = 0;
          let score2 = 0;
    
          function getRandomFood() {
              return {
                  x: Math.floor(Math.random() * tileCount),
                  y: Math.floor(Math.random() * tileCount)
              };
          }
    
          function drawGame() {
              clearCanvas();
              moveSnake(snake1, dx1, dy1);
              moveSnake(snake2, dx2, dy2);
              checkCollision();
              drawFood();
              drawSnake(snake1, '#00f');
              drawSnake(snake2, '#f00');
              updateScore();
          }
    
          function clearCanvas() {
              ctx.fillStyle = 'white';
              ctx.fillRect(0, 0, canvas.width, canvas.height);
          }
    
          function moveSnake(snake, dx, dy) {
              const head = {x: snake[0].x + dx, y: snake[0].y + dy};
              snake.unshift(head);
    
              if (head.x === food.x && head.y === food.y) {
                  food = getRandomFood();
                  if (snake === snake1) score1++;
                  else score2++;
              } else {
                  snake.pop();
              }
          }
    
          function checkCollision() {
              const head1 = snake1[0];
              const head2 = snake2[0];
    
              // Check wall collision
              if (head1.x < 0 || head1.x >= tileCount || head1.y < 0 || head1.y >= tileCount) gameOver();
              if (head2.x < 0 || head2.x >= tileCount || head2.y < 0 || head2.y >= tileCount) gameOver();
    
              // Check self-collision
              for (let i = 1; i < snake1.length; i++) {
                  if (head1.x === snake1[i].x && head1.y === snake1[i].y) gameOver();
              }
              for (let i = 1; i < snake2.length; i++) {
                  if (head2.x === snake2[i].x && head2.y === snake2[i].y) gameOver();
              }
    
              // Check collision between snakes
              for (let part of snake2) {
                  if (head1.x === part.x && head1.y === part.y) gameOver();
              }
              for (let part of snake1) {
                  if (head2.x === part.x && head2.y === part.y) gameOver();
              }
          }
    
          function drawFood() {
              ctx.fillStyle = 'green';
              ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
          }
    
          function drawSnake(snake, color) {
              ctx.fillStyle = color;
              for (let part of snake) {
                  ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize, gridSize);
              }
          }
    
          function updateScore() {
              document.getElementById('score1').textContent = score1;
              document.getElementById('score2').textContent = score2;
          }
    
          function gameOver() {
              alert('Game Over! Scores: Player 1 - ' + score1 + ', Player 2 - ' + score2);
              resetGame();
          }
    
          function resetGame() {
              snake1 = [{x: 10, y: 10}, {x: 9, y: 10}, {x: 8, y: 10}];
              snake2 = [{x: 30, y: 30}, {x: 31, y: 30}, {x: 32, y: 30}];
              dx1 = 1;
              dy1 = 0;
              dx2 = -1;
              dy2 = 0;
              food = getRandomFood();
              score1 = 0;
              score2 = 0;
          }
    
          document.addEventListener('keydown', (e) => {
              switch(e.key) {
                  case 'w': if (dy1 === 0) { dx1 = 0; dy1 = -1; } break;
                  case 's': if (dy1 === 0) { dx1 = 0; dy1 = 1; } break;
                  case 'a': if (dx1 === 0) { dx1 = -1; dy1 = 0; } break;
                  case 'd': if (dx1 === 0) { dx1 = 1; dy1 = 0; } break;
                  case 'u': if (dy2 === 0) { dx2 = 0; dy2 = -1; } break;
                  case 'j': if (dy2 === 0) { dx2 = 0; dy2 = 1; } break;
                  case 'h': if (dx2 === 0) { dx2 = -1; dy2 = 0; } break;
                  case 'k': if (dx2 === 0) { dx2 = 1; dy2 = 0; } break;
              }
          });
    
          setInterval(drawGame, 150);
      </script>
    </body> </html>
相关推荐
Bamtone20251 分钟前
PCB切片分析新方案:Bamtone MS90集成AI的智能测量解决方案
人工智能
Warren2Lynch3 分钟前
2026年专业软件工程与企业架构的智能化演进
人工智能·架构·软件工程
jin1233223 分钟前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos
_waylau11 分钟前
【HarmonyOS NEXT+AI】问答08:仓颉编程语言是中文编程语言吗?
人工智能·华为·harmonyos·鸿蒙·仓颉编程语言·鸿蒙生态·鸿蒙6
YuTaoShao14 分钟前
【LeetCode 每日一题】3010. 将数组分成最小总代价的子数组 I——(解法二)排序
算法·leetcode·排序算法
攻城狮7号23 分钟前
Kimi 发布并开源 K2.5 模型:开始在逻辑和干活上卷你了
人工智能·ai编程·视觉理解·kimi code·kimi k2.5·agent 集群
szxinmai主板定制专家26 分钟前
基于 PC 的控制技术+ethercat+linux实时系统,助力追踪标签规模化生产,支持国产化
arm开发·人工智能·嵌入式硬件·yolo·fpga开发
阿狸OKay40 分钟前
einops 库和 PyTorch 的 einsum 的语法
人工智能·pytorch·python
低调小一1 小时前
Google AI Agent 白皮书拆解(1):从《Introduction to Agents》看清 Agent 的工程底座
人工智能
feasibility.1 小时前
混元3D-dit-v2-mv-turbo生成3D模型初体验(ComfyUI)
人工智能·3d·aigc·三维建模·comfyui