• js实现贪吃蛇游戏


    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>贪吃蛇游戏title>
        <style>
            .game-container {
                width: 300px;
                height: 300px;
                border: 1px solid #000;
                position: relative;
            }
    
            .snake {
                width: 20px;
                height: 20px;
                background-color: green;
                position: absolute;
            }
    
            .food {
                width: 20px;
                height: 20px;
                background-color: red;
                position: absolute;
            }
    
            button {
                display: block;
                margin-top: 10px;
            }
        style>
    head>
    
    <body>
        <div class="game-container">
            <div class="snake" id="snake">div>
            <div class="food" id="food">div>
        div>
        <button id="start">开始游戏button>
        <script>
            const gameContainer = document.querySelector(".game-container");
            const snakeElement = document.getElementById("snake");
            const foodElement = document.getElementById("food");
            const startButton = document.getElementById("start");
    
            let snake = [{ x: 10, y: 10 }];
            let food = { x: 5, y: 5 };
            let direction = "right";
            let isGameRunning = false;
    
            function moveSnake() {
                if (!isGameRunning) return;
    
                const head = { ...snake[0] };
                switch (direction) {
                    case "up":
                        head.y--;
                        break;
                    case "down":
                        head.y++;
                        break;
                    case "left":
                        head.x--;
                        break;
                    case "right":
                        head.x++;
                        break;
                }
    
                snake.unshift(head);
    
                if (head.x === food.x && head.y === food.y) {
                    // 吃到食物
                    food = {
                        x: Math.floor(Math.random() * 15),
                        y: Math.floor(Math.random() * 15),
                    };
                } else {
                    snake.pop();
                }
    
                // 检查碰撞
                if (
                    head.x < 0 ||
                    head.x >= 15 ||
                    head.y < 0 ||
                    head.y >= 15 ||
                    snake.some((segment, index) => index !== 0 && segment.x === head.x && segment.y === head.y)
                ) {
                    isGameRunning = false;
                    alert("游戏结束!");
                    return;
                }
    
                updateGameBoard();
                setTimeout(moveSnake, 200);
            }
    
            function updateGameBoard() {
                snakeElement.innerHTML = "";
                snake.forEach((segment) => {
                    const segmentElement = document.createElement("div");
                    segmentElement.className = "snake";
                    segmentElement.style.left = segment.x * 20 + "px";
                    segmentElement.style.top = segment.y * 20 + "px";
                    snakeElement.appendChild(segmentElement);
                });
    
                foodElement.style.left = food.x * 20 + "px";
                foodElement.style.top = food.y * 20 + "px";
            }
    
            startButton.addEventListener("click", () => {
                if (!isGameRunning) {
                    snake = [{ x: 10, y: 10 }];
                    direction = "right";
                    isGameRunning = true;
                    moveSnake();
                }
            });
    
            document.addEventListener("keydown", (event) => {
                switch (event.key) {
                    case "ArrowUp":
                        if (direction !== "down") direction = "up";
                        break;
                    case "ArrowDown":
                        if (direction !== "up") direction = "down";
                        break;
                    case "ArrowLeft":
                        if (direction !== "right") direction = "left";
                        break;
                    case "ArrowRight":
                        if (direction !== "left") direction = "right";
                        break;
                }
            });
    
            updateGameBoard();
    
        script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
  • 相关阅读:
    【Webpack】提升打包构建速度
    Vue电商项目--详情页面--产品售卖属性
    25G、50G、100G以太网介绍,网络工程师收藏!
    蚁剑webshell动态加密连接分析与实践
    maven依赖问题ClassNotFoundException 和NoClassDefFoundError
    Vmware 扩展硬盘空间后的操作-Ubuntu
    前端js八股文大全
    Ceph — 使用cephadm搭建Ceph集群
    python --阿里云(智能媒体管理/视频点播)
    appium如何连接多台设备?教程详解
  • 原文地址:https://blog.csdn.net/weixin_45791806/article/details/132827500