chatGPT写的网页版贪吃蛇小游戏

chatGPT写的网页版贪吃蛇小游戏

前言

之前无聊,让ChatGPT写了一段基于html语言的贪吃蛇小游戏代码

网页版贪吃蛇小游戏

将以下内容复制到记事本,重命名为xxx.html即可打开浏览器游玩

这里是一个使用HTML、CSS和JavaScript编写的可以在浏览器中直接运行的贪吃蛇游戏代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Snake Game by OpenAI</title>
    <style>
        canvas {
            display: block;
            margin: 0 auto;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <canvas id="gameCanvas" width="400" height="400"></canvas>
    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');

        const snakeSize = 10;
        let snake = [{x: 150, y: 150}, {x: 140, y: 150}, {x: 130, y: 150}, {x: 120, y: 150}];
        let food = {x: 0, y: 0};
        let dx = 10;
        let dy = 0;
        let changingDirection = false;

        function drawSnakePart(snakePart) {
            ctx.fillStyle = 'green';
            ctx.strokeStyle = 'black';
            ctx.fillRect(snakePart.x, snakePart.y, snakeSize, snakeSize);
            ctx.strokeRect(snakePart.x, snakePart.y, snakeSize, snakeSize);
        }

        function drawSnake() {
            snake.forEach(drawSnakePart);
        }

        function moveSnake() {
            const head = {x: snake[0].x + dx, y: snake[0].y + dy};
            snake.unshift(head);

            const didEatFood = snake[0].x === food.x && snake[0].y === food.y;
            if (didEatFood) {
                createFood();
            } else {
                snake.pop();
            }
        }

        function changeDirection(event) {
            const LEFT_KEY = 37;
            const RIGHT_KEY = 39;
            const UP_KEY = 38;
            const DOWN_KEY = 40;

            if (changingDirection) return;
            changingDirection = true;

            const keyPressed = event.keyCode;
            const goingUp = dy === -10;
            const goingDown = dy === 10;
            const goingRight = dx === 10;
            const goingLeft = dx === -10;

            if (keyPressed === LEFT_KEY && !goingRight) {
                dx = -10;
                dy = 0;
            }

            if (keyPressed === UP_KEY && !goingDown) {
                dx = 0;
                dy = -10;
            }

            if (keyPressed === RIGHT_KEY && !goingLeft) {
                dx = 10;
                dy = 0;
            }

            if (keyPressed === DOWN_KEY && !goingUp) {
                dx = 0;
                dy = 10;
            }
        }

        function createFood() {
            food.x = Math.floor(Math.random() * canvas.width / 10) * 10;
            food.y = Math.floor(Math.random() * canvas.height / 10) * 10;
            snake.forEach(part => {
                if (part.x === food.x && part.y === food.y) {
                    createFood();
                }
            });
        }

        function drawFood() {
            ctx.fillStyle = 'red';
            ctx.strokeStyle = 'black';
            ctx.fillRect(food.x, food.y, snakeSize, snakeSize);
            ctx.strokeRect(food.x, food.y, snakeSize, snakeSize);
        }

        function checkCollision() {
            for (let i = 4; i < snake.length; i++) {
                if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
                    return true;
                }
            }
            const hitLeftWall = snake[0].x < 0;
            const hitRightWall = snake[0].x > canvas.width - snakeSize;
            const hitTopWall = snake[0].y < 0;
            const hitBottomWall = snake[0].y > canvas.height - snakeSize;

            return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall;
        }

        function gameLoop() {
            if (checkCollision()) {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.fillStyle = 'black';
                ctx.font = '30px Arial';
                ctx.fillText('Game Over!', 150, 200);
                return;
            }

            changingDirection = false;
            setTimeout(() => {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                drawFood();
                moveSnake();
                drawSnake();
                gameLoop();
            }, 100);
        }

        createFood();
        document.addEventListener('keydown', changeDirection);
        gameLoop();
    </script>
</body>
</html>

将上面的代码保存为一个.html文件,然后用浏览器打开这个文件,就可以在浏览器中直接运行这个贪吃蛇游戏了。按方向键控制蛇的移动,吃到食物后蛇会变长。玩得开心!

  • end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘剑客_CN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值