🎮ArcadeLab

贪吃蛇游戏

by MysticMaker96
179 lines4.9 KB
▶ Play
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贪吃蛇游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #1a1a1a;
            color: #fff;
            padding: 20px;
        }
        #gameBox {
            border: 3px solid #4CAF50;
            background-color: #000;
        }
        .info {
            margin-bottom: 10px;
            font-size: 20px;
        }
        button {
            margin: 8px;
            padding: 8px 16px;
            font-size: 16px;
            border: none;
            border-radius: 6px;
            background: #4CAF50;
            color: white;
            cursor: pointer;
        }
        button:hover {
            background: #388E3C;
        }
    </style>
</head>
<body>
    <div class="info">得分:<span id="score">0</span></div>
    <canvas id="gameBox" width="400" height="400"></canvas>
    <div>
        <button id="startBtn">开始 / 重新开始</button>
    </div>
    <div style="margin-top:10px;color:#aaa;">方向键 ↑ ↓ ← → 控制蛇</div>

    <script>
        const canvas = document.getElementById('gameBox');
        const ctx = canvas.getContext('2d');
        const scoreDom = document.getElementById('score');
        const startBtn = document.getElementById('startBtn');

        const size = 20; // 格子大小
        const count = canvas.width / size;
        let snake = [];
        let food = {};
        let dir = {x:1, y:0};
        let nextDir = {x:1, y:0};
        let score = 0;
        let timer = null;
        let isRun = false;

        // 初始化蛇
        function initSnake(){
            snake = [
                {x:5, y:5},
                {x:4, y:5},
                {x:3, y:5}
            ];
            dir = {x:1, y:0};
            nextDir = {x:1, y:0};
            score = 0;
            scoreDom.innerText = score;
            createFood();
        }

        // 生成食物
        function createFood(){
            let ok = false;
            while(!ok){
                food = {
                    x: Math.floor(Math.random()*count),
                    y: Math.floor(Math.random()*count)
                }
                ok = !snake.some(s=>s.x===food.x && s.y===food.y)
            }
        }

        // 绘制
        function draw(){
            ctx.fillStyle="#000";
            ctx.fillRect(0,0,canvas.width,canvas.height);

            // 蛇
            ctx.fillStyle="#4CAF50";
            snake.forEach((s,idx)=>{
                if(idx===0) ctx.fillStyle="#8BC34A"; // 蛇头亮色
                else ctx.fillStyle="#4CAF50";
                ctx.fillRect(s.x*size, s.y*size, size-1, size-1);
            })

            // 食物
            ctx.fillStyle="#f44336";
            ctx.fillRect(food.x*size, food.y*size, size-1, size-1);
        }

        // 游戏更新
        function update(){
            dir = {...nextDir};
            const head = {x: snake[0].x + dir.x, y: snake[0].y + dir.y};

            // 撞墙判定
            if(head.x<0 || head.x>=count || head.y<0 || head.y>=count){
                gameOver();
                return;
            }
            // 撞到自己
            if(snake.some(s=>s.x===head.x && s.y===head.y)){
                gameOver();
                return;
            }

            snake.unshift(head);
            // 吃到食物
            if(head.x === food.x && head.y === food.y){
                score +=10;
                scoreDom.innerText = score;
                createFood();
            }else{
                snake.pop();
            }
            draw();
        }

        function gameOver(){
            clearInterval(timer);
            isRun = false;
            alert(`游戏结束!得分:${score}`);
        }

        function startGame(){
            clearInterval(timer);
            initSnake();
            draw();
            isRun = true;
            timer = setInterval(update,120);
        }

        // 键盘监听
        document.addEventListener('keydown',(e)=>{
            if(!isRun) return;
            switch(e.key){
                case 'ArrowUp':
                    if(dir.y!==1) nextDir={x:0,y:-1};
                    break;
                case 'ArrowDown':
                    if(dir.y!==-1) nextDir={x:0,y:1};
                    break;
                case 'ArrowLeft':
                    if(dir.x!==1) nextDir={x:-1,y:0};
                    break;
                case 'ArrowRight':
                    if(dir.x!==-1) nextDir={x:1,y:0};
                    break;
            }
        })

        startBtn.onclick = startGame;
        draw();
    </script>
</body>
</html>

Game Source: 贪吃蛇游戏

Creator: MysticMaker96

Libraries: none

Complexity: moderate (179 lines, 4.9 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-mysticmaker96-mw5sct2x" to link back to the original. Then publish at arcadelab.ai/publish.