当前位置: 首页 > news >正文

html+css+js贪吃蛇游戏

贪吃蛇游戏🕹四个按钮控制方向🎮

源代码在图片后面     点赞❤️关注🙏收藏⭐️

互粉必回🙏🙏😍😍😍

c4f905286c4d4f818c5c0277a93028b0.jpg

 源代码📟

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>贪吃蛇游戏</title>

<style>

    body {

        display: flex;

        justify-content: center;

        align-items: center;

        height: 100vh;

        background-color: #f5f5f5;

        margin: 0;

        font-family: Arial, sans-serif;

    }

    .game-container {

        width: 300px;

        text-align: center;

    }

    table {

        border-collapse: collapse;

        margin-top: 20px;

        border: 1px solid gray;

    }

    td {

        width: 10px;

        height: 10px;

        border: 1px solid gray;

    }

    .snake {

        background-color: green;

    }

    .food {

        background-color: red;

    }

    .control-buttons {

        display: flex;

        justify-content: space-around;

        margin-bottom: 10px;

    }

    button {

        width: 50px;

        height: 50px;

        border-radius: 10px;

        border: none;

        box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);

        cursor: pointer;

    }

    #score {

        font-size: 1.5em;

        margin-bottom: 10px;

    }

</style>

</head>

<body>

<div class="game-container">

    <div id="score">当前分数: 0</div>

    <div class="control-buttons">

        <button id="up-btn">&#9650;</button>

        <button id="right-btn">&#9654;</button>

        <button id="down-btn">&#9660;</button>

        <button id="left-btn">&#9664;</button>

    </div>

    <table id="game-board">

    </table>

</div>

 

<script>

    const board = document.getElementById('game-board');

    const scoreDisplay = document.getElementById('score');

 

    let snake = [{x: 10, y: 10}];

    let food = {x: 15, y: 15};

    let direction = 'right';

    let score = 0;

 

    function drawBoard() {

        board.innerHTML = '';

        for (let i = 0; i < 20; i++) {

            let row = '<tr>';

            for (let j = 0; j < 20; j++) {

                if (snake.some(part => part.x === j && part.y === i)) {

                    row += '<td class="snake"></td>';

                } else if (food.x === j && food.y === i) {

                    row += '<td class="food"></td>';

                } else {

                    row += '<td></td>';

                }

            }

            row += '</tr>';

            board.innerHTML += row;

        }

    }

 

    function updateSnake() {

        const head = {x: snake[0].x + (direction === 'right' ? 1 : direction === 'left' ? -1 : 0),

                      y: snake[0].y + (direction === 'down' ? 1 : direction === 'up' ? -1 : 0)};

        snake.unshift(head);

        if (head.x === food.x && head.y === food.y) {

            score++;

            scoreDisplay.textContent = `当前分数: ${score}`;

            food = {x: Math.floor(Math.random() * 20), y: Math.floor(Math.random() * 20)};

        } else {

            snake.pop();

        }

    }

 

    function checkCollision() {

        const head = snake[0];

        if (head.x < 0 || head.x >= 20 || head.y < 0 || head.y >= 20 || snake.slice(1).some(part => part.x === head.x && part.y === head.y)) {

            alert('Game Over!');

            location.reload();

        }

    }

 

    function gameLoop() {

        updateSnake();

        checkCollision();

        drawBoard();

        setTimeout(gameLoop, 200);

    }

 

    document.getElementById('up-btn').addEventListener('click', () => direction = 'up');

    document.getElementById('right-btn').addEventListener('click', () => direction = 'right');

    document.getElementById('down-btn').addEventListener('click', () => direction = 'down');

    document.getElementById('left-btn').addEventListener('click', () => direction = 'left');

 

    gameLoop();

</script>

</body>

</html>

http://www.lryc.cn/news/394591.html

相关文章:

  • 新手必学:掌握Excel中这些常用公式,轻松提升数据处理能力
  • 经济寒冬:竞品凶猛,你的产品如何求生?
  • 信号量——Linux并发之魂
  • 自动驾驶中的逆透视变换(Inverse Perspective Mapping,IPM)详解
  • Python地震波逆问题解构算法复杂信号分析
  • C语言 -- 深入理解指针(二)
  • HTTP协议详解
  • 一年时间业绩增长2倍,茅台保健酒业公司在川销售的“三板斧”
  • 土豆炒肉做法
  • VPS拨号服务器:独享的高效与安全
  • 网络安全设备——防火墙
  • Redis 管道技术
  • 使用vue3-treeselect问题
  • 每日直播分享车载知识:硬件在环、UDS诊断、OTA升级、TBOX测试、CANoe、ECU刷写、CAN一致性测试:物理层、数据链路层等
  • flex布局---子元素未设置高度,默认与父元素同高---侧轴方向的拉伸
  • 资源分享—2021版三调符号库
  • 解决selenium手动下载驱动问题
  • 使用fifo IP核,给fifo写数据,当检测到ALMOST_EMPTY时,为什么不能立即赋值
  • 【Python123题库】#汽车迷 #编写函数输出自除数 #身份证号基本信息
  • 普通人怎么利用GPT赚钱之SEO优化内容
  • LeetCode热题100刷题8:54. 螺旋矩阵、73. 矩阵置零、48. 旋转图像
  • 景联文科技打造高质量图文推理问答数据集,赋能大语言模型提升推理能力
  • 用网络编程完成windows和linux跨平台之间的通信(服务器)
  • 力扣3148.矩阵中的最大得分
  • 解决数据库PGSQL,在Mybatis中创建临时表报错TODO IDENTIFIER,连接池用的Druid。更换最新版本Druid仍然报错解决
  • 【WPF】桌面程序开发之xaml页面基础布局方式详解
  • 第十五章 Nest Pipe(内置及自定义)
  • 实战篇(八):使用Processing创建动态图形:诡异八爪鱼
  • 大模型成为软件和数据工程师
  • 【鸿蒙学习笔记】页面布局