贪吃蛇 html css js 实现

贪吃蛇 html css js 实现

学js时写的
界面没有优化 而且是粗糙实现 还有一些bug 感兴趣可以看看,若是优化了可要评论告诉我一下哟,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>贪吃蛇-完成</title>
    <script src="js/jquery-3.6.0.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .score {
            margin:0 auto;
            height: 40px;
            text-align:center;
            line-height:40px;
            color: #ec6565;
        }

        .w {
            position: relative;
            left: 50%;
            transform: translateX(-50%);
            margin-top: 10px;
            width: 800px;
            height: 600px;
            border:2px solid #484848;
            background-color: pink;
        }

        .w div {
            position: absolute;
            width: 10px;
            height: 10px;
            background-color: #d6d6d6;
        }

        .w .t {
            background-color: #888888;
        }

        .w div:last-child {
            background-color: #cf5353;
        }
    </style>

</head>
<body>
<div class="score">0</div>
<div class="w">
    <div class="t" style="left:30px"></div>
    <div style="left:20px"></div>
    <div style="left:10px"></div>
    <div></div>

</div>
<script>
    var s = document.querySelector('.score');
    var w = document.querySelector('.w');
    var all = w.querySelectorAll('div');
    var count = 39;
    var ml = null;
    var md = null;
    var mr = null;
    var mu = null;
    var arr = new Array();

    //初始食物
    food();
    var last = w.querySelectorAll('div')[all.length];


    function moveLeft(objs) {
        count = 39;
        ml = setInterval(() => {
            for (var j = 0; j < objs.length - 1; j++) {
                arr[j] = {l: objs[j].offsetLeft, t: objs[j].offsetTop}
            }

            for (var i = 0; i < objs.length; i++) {
                if (i == 0) {
                    if (objs[0].offsetLeft == 790) {
                        // objs[0].style.left = '0px';
                        over();
                    }
                    judge(objs[0].offsetLeft+10, objs[0].offsetTop);
                    objs[0].style.left = (objs[0].offsetLeft + 10) + 'px';
                } else {
                    objs[i].style.left = arr[i - 1].l + 'px';
                    objs[i].style.top = arr[i - 1].t + 'px';
                }
            }
        }, 50)
    }

    function moveDown(objs) {
        count = 40;
        md = setInterval(() => {
            for (var j = 0; j < objs.length - 1; j++) {
                arr[j] = {l: objs[j].offsetLeft, t: objs[j].offsetTop}
            }

            for (var i = 0; i < objs.length; i++) {
                if (i == 0) {
                    if (objs[0].offsetTop == 590) {
                        // objs[0].style.top = '0px';
                        over();
                    }
                    judge(objs[0].offsetLeft, objs[0].offsetTop+10);
                    objs[0].style.top = (objs[0].offsetTop + 10) + 'px';
                } else {
                    console.log(i);
                    objs[i].style.left = arr[i - 1].l + 'px';
                    objs[i].style.top = arr[i - 1].t + 'px';
                }
            }

        }, 50)
    }

    function moveRight(objs) {
        count = 37;
        mr = setInterval(() => {
            for (var j = 0; j < objs.length - 1; j++) {
                arr[j] = {l: objs[j].offsetLeft, t: objs[j].offsetTop}
            }

            for (var i = 0; i < objs.length; i++) {
                if (i == 0) {
                    if (objs[0].offsetLeft == 0) {
                        // objs[0].style.left = '790px';
                        over();
                    }
                    judge(objs[0].offsetLeft-10, objs[0].offsetTop);
                    objs[0].style.left = (objs[0].offsetLeft - 10) + 'px';
                } else {
                    objs[i].style.left = arr[i - 1].l + 'px';
                    objs[i].style.top = arr[i - 1].t + 'px';
                }
            }
        }, 50)
    }

    function moveUp(objs) {
        count = 38;
        mu = setInterval(() => {
            for (var j = 0; j < objs.length - 1; j++) {
                arr[j] = {l: objs[j].offsetLeft, t: objs[j].offsetTop}
            }

            for (var i = 0; i < objs.length; i++) {
                if (i == 0) {
                    if (objs[0].offsetTop == 0) {
                        // objs[0].style.top = '590px';
                        over();
                    }
                    judge(objs[0].offsetLeft, objs[0].offsetTop-10);
                    objs[0].style.top = (objs[0].offsetTop - 10) + 'px';
                } else {
                    objs[i].style.left = arr[i - 1].l + 'px';
                    objs[i].style.top = arr[i - 1].t + 'px';
                }
            }

        }, 50)
    }


    // move(all);

    //清除定时器
    function clear() {
        switch (count) {
            case 37 :
                clearInterval(mr);
                break;
            case 38 :
                clearInterval(mu);
                break;
            case 39 :
                clearInterval(ml);
                break;
            case 40 :
                clearInterval(md);
        }
    }

    //键盘监听
    window.onkeydown = (e) => {
            console.log(11111);
        if(Math.abs(e-count)==2){
            return;
        }
        
        clear();
        switch (e.keyCode) {
            case 37 :
                moveRight(all);
                break;
            case 38 :
                moveUp(all);
                break;
            case 39 :
                moveLeft(all);
                break;
            case 40 :
                moveDown(all);
        }
    }

    //生成食物
    function food() {
        var d = document.createElement('DIV');
        w.appendChild(d);
        d.style.left = Math.floor(Math.random() * 79) * 10 + 'px';
        d.style.top = Math.floor(Math.random() * 59) * 10 + 'px';
    }

    function judge(le, to) {
        var l = last.offsetLeft;
        var t = last.offsetTop;
        console.log(l, t);
        // console.log(le,to);

        //撞自身死亡判断
        for (var i = 3; i < all.length; i++) {
            if (le == all[i].offsetLeft && to == all[i].offsetTop) {
                over();
            }
        }

        //吃食物判断
        if (l == le && t == to) {
            all = w.querySelectorAll('div');
            w = document.querySelector('.w');
            food();
            last = w.querySelectorAll('div')[all.length];
            console.log(food);
            s.innerHTML=`${all.length-4}`;
        }
    }

    //游戏结束提示
    function over() {
        clear();
        alert('游戏结束,得分:'+s.innerHTML);
    }

</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用HTMLCSSJavaScript来生成贪吃蛇小游戏。首先,需要创建一个HTML页面,然后使用CSS来设计游戏界面,最后使用JavaScript编写游戏逻辑。以下是一个简单的示例代码: HTML代码: ``` <!DOCTYPE html> <html> <head> <title>贪吃蛇小游戏</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div id="game"></div> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` #game { width: 400px; height: 400px; border: 1px solid black; position: relative; } .snake { width: 10px; height: 10px; background-color: green; position: absolute; } .food { width: 10px; height: 10px; background-color: red; position: absolute; } ``` JavaScript代码: ``` var game = document.getElementById("game"); var snake = [{x: 0, y: 0}]; var food = {x: 0, y: 0}; var direction = "right"; var score = 0; function createSnake() { for (var i = 0; i < snake.length; i++) { var div = document.createElement("div"); div.className = "snake"; div.style.left = snake[i].x + "px"; div.style.top = snake[i].y + "px"; game.appendChild(div); } } function createFood() { food.x = Math.floor(Math.random() * 40) * 10; food.y = Math.floor(Math.random() * 40) * 10; var div = document.createElement("div"); div.className = "food"; div.style.left = food.x + "px"; div.style.top = food.y + "px"; game.appendChild(div); } function moveSnake() { var head = {x: snake[0].x, y: snake[0].y}; switch (direction) { case "right": head.x += 10; break; case "left": head.x -= 10; break; case "up": head.y -= 10; break; case "down": head.y += 10; break; } snake.unshift(head); if (head.x == food.x && head.y == food.y) { score++; createFood(); } else { snake.pop(); } for (var i = 0; i < snake.length; i++) { var div = document.getElementsByClassName("snake")[i]; div.style.left = snake[i].x + "px"; div.style.top = snake[i].y + "px"; } } function checkCollision() { var head = snake[0]; if (head.x < 0 || head.x >= 400 || head.y < 0 || head.y >= 400) { clearInterval(interval); alert("游戏结束,得分:" + score); } for (var i = 1; i < snake.length; i++) { if (head.x == snake[i].x && head.y == snake[i].y) { clearInterval(interval); alert("游戏结束,得分:" + score); } } } createSnake(); createFood(); var interval = setInterval(function() { moveSnake(); checkCollision(); }, 100); document.addEventListener("keydown", function(event) { switch (event.keyCode) { case 37: if (direction != "right") { direction = "left"; } break; case 38: if (direction != "down") { direction = "up"; } break; case 39: if (direction != "left") { direction = "right"; } break; case 40: if (direction != "up") { direction = "down"; } break; } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值