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

AI生成移动端贪吃蛇游戏页面,手机浏览器打开即可玩

贪吃蛇游戏可计分,可穿墙,AI生成适配手机浏览器的游戏,代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>贪吃蛇游戏</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #000;
            color: #fff;
            font-family: Arial, sans-serif;
            touch-action: none; /* 禁用默认触摸行为 */
        }
        canvas {
            background-color: #333;
            border: 2px solid #fff;
            max-width: 100%;
            max-height: 100%;
        }
        #score {
            position: absolute;
            top: 10px;
            left: 10px;
            font-size: 20px;
        }
        #gameOver {
            display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 40px;
            color: red;
        }
    </style>
</head>
<body>
    <div id="score">得分: 0</div>
    <div id="gameOver">游戏结束</div>
    <canvas id="gameCanvas"></canvas>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const scoreElement = document.getElementById('score');
        const gameOverElement = document.getElementById('gameOver');

        const gridSize = 20; // 网格大小
        let tileCount = 20; // 网格数量
        let tileSize; // 每个网格的像素大小
        let snake = [{ x: 10, y: 10 }]; // 蛇的初始位置
        let food = { x: 5, y: 5 }; // 食物的初始位置
        let direction = { x: 0, y: 0 }; // 蛇的移动方向
        let score = 0; // 得分
        let gameOver = false; // 游戏是否结束

        // 初始化画布大小
        function resizeCanvas() {
            const size = Math.min(window.innerWidth, window.innerHeight) * 0.9;
            canvas.width = size;
            canvas.height = size;
            tileSize = canvas.width / tileCount;
        }

        window.addEventListener('resize', resizeCanvas);
        resizeCanvas();

        // 游戏主循环
        function gameLoop() {
            if (gameOver) {
                gameOverElement.style.display = 'block';
                return;
            }
    
            setTimeout(function(){
    update();
                draw();
                requestAnimationFrame(gameLoop); // 使用 requestAnimationFrame 实现高性能动画
            }, 100);

        }

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

            // 边缘穿越逻辑
            if (head.x < 0) head.x = tileCount - 1;
            if (head.x >= tileCount) head.x = 0;
            if (head.y < 0) head.y = tileCount - 1;
            if (head.y >= tileCount) head.y = 0;

            // 吃到食物
            if (head.x === food.x && head.y === food.y) {
                score++;
                scoreElement.textContent = `得分: ${score}`;
                food = { x: Math.floor(Math.random() * tileCount), y: Math.floor(Math.random() * tileCount) };
            } else {
                snake.pop(); // 如果没有吃到食物,移除蛇尾
            }

            snake.unshift(head); // 添加新的蛇头

            // 检查是否撞到自己
            for (let i = 1; i < snake.length; i++) {
                if (snake[i].x === head.x && snake[i].y === head.y) {
                    gameOver = true;
                }
            }
        }

        // 绘制游戏画面
        function draw() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // 绘制蛇
            ctx.fillStyle = 'lime';
            snake.forEach(part => ctx.fillRect(part.x * tileSize, part.y * tileSize, tileSize, tileSize));

            // 绘制食物
            ctx.fillStyle = 'red';
            ctx.fillRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
        }

        // 触摸控制逻辑
        let touchStartX = 0;
        let touchStartY = 0;

        canvas.addEventListener('touchstart', (e) => {
            touchStartX = e.touches[0].clientX;
            touchStartY = e.touches[0].clientY;
        });

        canvas.addEventListener('touchmove', (e) => {
            e.preventDefault();
            const touchEndX = e.touches[0].clientX;
            const touchEndY = e.touches[0].clientY;
            const dx = touchEndX - touchStartX;
            const dy = touchEndY - touchStartY;

            if (Math.abs(dx) > Math.abs(dy)) {
                direction = { x: dx > 0 ? 1 : -1, y: 0 }; // 水平移动
            } else {
                direction = { x: 0, y: dy > 0 ? 1 : -1 }; // 垂直移动
            }
        });

        // 启动游戏
        gameLoop();
    </script>
</body>
</html>


http://www.kler.cn/a/596577.html

相关文章:

  • Linux进程控制(四)之进程程序替换
  • 新能源汽车高压液体加热器总成技术解析及未来发展趋势
  • HashMap学习总结——JDK17
  • 介绍一个测试boostrap表格插件的好网站!
  • LVGL学习1
  • 【云上CPU玩转AIGC】——腾讯云高性能应用服务HAI已支持DeepSeek-R1模型预装环境和CPU算力
  • 基于linux平台的C语言入门教程(4)输入输出
  • SQL中的索引是什么
  • 建筑安全员考试:“实战演练” 关键词助力的答题提升策略
  • ARM架构薄记2——ARM学习架构抓手(以ARMv7为例子)
  • Linux小知识
  • 七桥问题与一笔画问题:图论的奠基石
  • Vue3(自定义指令directive详解)
  • 前端(vue)学习笔记(CLASS 5):自定义指令插槽路由
  • RK3588开发笔记-DDR4降频实战与系统稳定性优化
  • KnowGPT知识图谱整合
  • 深入理解 Spring 框架中的 AOP 技术
  • 2025年3月GESP八级真题解析
  • 收数据花式画图plt实战
  • 【CXX-Qt】2.3 类型