html编写贪吃蛇页面小游戏(可以玩)

html 复制代码
<!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>
        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
            margin: 0;
        }
        #gameCanvas {
            border: 1px solid black;
        }
        .buttons {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <canvas id="gameCanvas" width="400" height="400"></canvas>
    <div class="buttons">
        <button id="startButton">开始游戏</button>
        <button id="endButton" disabled>结束游戏</button>
    </div>
    <script>
        // 获取画布和上下文
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const boxSize = 20;
        let snake = [{x: 10, y: 10}];
        let food = {x: 15, y: 15};
        let dx = boxSize;
        let dy = 0;
        let gameLoop;
 
        // 绘制游戏画面
        function draw() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
 
            // 移动蛇
            let head = {x: snake[0].x + dx / boxSize, y: snake[0].y + dy / boxSize};
 
            // 检查是否撞墙
            if (head.x < 0 || head.x >= canvas.width / boxSize || head.y < 0 || head.y >= canvas.height / boxSize) {
                endGame();
                return;
            }
 
            // 检查是否撞到自己
            if (snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
                endGame();
                return;
            }
 
            snake.unshift(head);
 
            // 检查是否吃到食物
            if (head.x === food.x && head.y === food.y) {
                food = {x: Math.floor(Math.random() * (canvas.width / boxSize)), y: Math.floor(Math.random() * (canvas.height / boxSize))};
            } else {
                snake.pop();
            }
 
            // 画蛇
            snake.forEach(segment => {
                ctx.fillStyle = 'green';
                ctx.fillRect(segment.x * boxSize, segment.y * boxSize, boxSize, boxSize);
            });
 
            // 画食物
            ctx.fillStyle = 'red';
            ctx.fillRect(food.x * boxSize, food.y * boxSize, boxSize, boxSize);
        }
 
        // 开始游戏
        function startGame() {
            snake = [{x: 10, y: 10}];
            food = {x: 15, y: 15};
            dx = boxSize;
            dy = 0;
            document.getElementById('startButton').disabled = true;
            document.getElementById('endButton').disabled = false;
            gameLoop = setInterval(draw, 200); // 增加时间间隔以降低速度
        }
 
        // 结束游戏
        function endGame() {
            clearInterval(gameLoop);
            alert('游戏结束');
            document.getElementById('startButton').disabled = false;
            document.getElementById('endButton').disabled = true;
        }
 
        document.getElementById('startButton').addEventListener('click', startGame);
        document.getElementById('endButton').addEventListener('click', endGame);
 
        document.addEventListener('keydown', (e) => {
            if (e.key === 'ArrowUp' && dy === 0) {
                dx = 0;
                dy = -boxSize;
            } else if (e.key === 'ArrowDown' && dy === 0) {
                dx = 0;
                dy = boxSize;
            } else if (e.key === 'ArrowLeft' && dx === 0) {
                dx = -boxSize;
                dy = 0;
            } else if (e.key === 'ArrowRight' && dx === 0) {
                dx = boxSize;
                dy = 0;
            }
        });
    </script>
</body>
</html>

(AI教授的,算是自娱自乐吧。这代码还挺有意思的。)

相关推荐
weixin-a1530030831624 分钟前
vue疑难解答
前端·javascript·vue.js
Bellafu6663 小时前
selenium 常用xpath写法
前端·selenium·测试工具
blackorbird6 小时前
Edge 浏览器 IE 模式成攻击突破口:黑客借仿冒网站诱导攻击
前端·edge
谷歌开发者7 小时前
Web 开发指向标 | Chrome 开发者工具学习资源 (一)
前端·chrome·学习
名字越长技术越强7 小时前
Chrome和IE获取本机ip地址
前端
天***88967 小时前
Chrome 安装失败且提示“无可用的更新” 或 “与服务器的连接意外终止”,Chrome 离线版下载安装教程
前端·chrome
半梦半醒*7 小时前
zabbix安装
linux·运维·前端·网络·zabbix
大怪v8 小时前
【搞发🌸活】不信书上那套理论!亲测Javascript能卡浏览器Reader一辈子~
javascript·html·浏览器
清羽_ls8 小时前
React Hooks 核心规则&自定义 Hooks
前端·react.js·hooks
你的人类朋友8 小时前
“签名”这个概念是非对称加密独有的吗?
前端·后端·安全