JS+H5flappybird游戏

效果图  (无素材贴图)

e3c7a19887f64363a6c102690df616ed.jpg

 源代码

<!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;

            justify-content: center;

            align-items: center;

            height: 100vh;

            margin: 0;

            background-color: #f0f0f0;

        }

        #gameCanvas {

            border: 1px solid #000;

        }

        #controlButton {

            position: absolute;

            bottom: 20px;

            padding: 10px 20px;

            font-size: 16px;

            color: #fff;

            background-color: #ff4d4d;

            border: none;

            border-radius: 20px;

            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);

            cursor: pointer;

        }

    </style>

</head>

<body>

    <canvas id="gameCanvas" width="400" height="600"></canvas>

    <button id="controlButton">点击飞起</button>

 

    <script>

        const canvas = document.getElementById('gameCanvas');

        const ctx = canvas.getContext('2d');

        const button = document.getElementById('controlButton');

 

        let bird = {

            x: 50,

            y: canvas.height / 2,

            radius: 15,

            gravity: 0.1,

            lift: -15,

            velocity: -1

        };

 

        let pipes = [];

        let score = 0;

        let frame = 0;

        let gameOver = false;

 

        function drawBird() {

            ctx.beginPath();

            ctx.arc(bird.x, bird.y, bird.radius, 0, Math.PI * 2);

            ctx.fillStyle = 'red';

            ctx.fill();

            ctx.closePath();

        }

 

        function drawPipe(pipe) {

            ctx.fillStyle = 'green';

            ctx.fillRect(pipe.x, 0, pipe.width, pipe.top);

            ctx.fillRect(pipe.x, canvas.height - pipe.bottom, pipe.width, pipe.bottom);

        }

 

        function updateBird() {

            bird.velocity += bird.gravity;

            bird.y += bird.velocity;

 

            if (bird.y + bird.radius > canvas.height) {

                bird.y = canvas.height - bird.radius;

                bird.velocity = 0;

            }

 

            if (bird.y - bird.radius < 0) {

                bird.y = bird.radius;

                bird.velocity = 0;

            }

        }

 

        function updatePipes() {

            frame++;

            if (frame % 100 === 0) {

                let gap = 150;

                let top = Math.random() * (canvas.height - gap);

                let bottom = canvas.height - top - gap;

                pipes.push({ x: canvas.width, width: 50, top: top, bottom: bottom });

            }

 

            for (let i = pipes.length - 1; i >= 0; i--) {

                pipes[i].x -= 2;

 

                if (pipes[i].x + pipes[i].width < 0) {

                    pipes.splice(i, 1);

                    score++;

                }

 

                if (bird.x + bird.radius > pipes[i].x && bird.x - bird.radius < pipes[i].x + pipes[i].width) {

                    if (bird.y - bird.radius < pipes[i].top || bird.y + bird.radius > canvas.height - pipes[i].bottom) {

                        gameOver = true;

                    }

                }

            }

        }

 

        function resetGame() {

            bird.y = canvas.height / 2;

            bird.velocity = 0;

            pipes = [];

            score = 0;

            frame = 0;

            gameOver = false;

        }

 

        function drawScore() {

            ctx.font = '20px Arial';

            ctx.fillStyle = 'black';

            ctx.fillText('Score: ' + score, 10, 30);

        }

 

        function gameLoop() {

            if (!gameOver) {

                ctx.clearRect(0, 0, canvas.width, canvas.height);

                drawBird();

                pipes.forEach(drawPipe);

                drawScore();

                updateBird();

                updatePipes();

                requestAnimationFrame(gameLoop);

            } else {

                ctx.clearRect(0, 0, canvas.width, canvas.height);

                ctx.font = '30px Arial';

                ctx.fillStyle = 'black';

                ctx.fillText('Game Over', canvas.width / 2 - 70, canvas.height / 2 - 30);

                ctx.fillText('Final Score: ' + score, canvas.width / 2 - 90, canvas.height / 2 + 30);

            }

        }

 

        button.addEventListener('click', () => {

            if (!gameOver) {

                bird.velocity += bird.lift;

                bird.lift *= 1; // 每次弹起距离降低一点

            }

        });

 

        gameLoop();

    </script>

</body>

</html>

 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
HTML部分: ```html <div class="progress-ring"> <div class="progress-bar"></div> <div class="progress-value">0%</div> </div> ``` CSS部分: ```css .progress-ring { position: relative; width: 200px; height: 200px; } .progress-bar { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1); } .progress-bar:before { position: absolute; content: ""; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; background-color: #1abc9c; transform: rotate(-90deg); transform-origin: center; } .progress-value { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px; font-weight: bold; color: #1abc9c; } ``` JavaScript部分: ```javascript const progressBar = document.querySelector(".progress-bar"); const progressValue = document.querySelector(".progress-value"); let progress = 0; const interval = setInterval(() => { progress += 1; progressBar.style.backgroundImage = "linear-gradient(" + (90 + (progress / 100) * 360) + "deg, transparent 50%, #1abc9c 50%), linear-gradient(90deg, #1abc9c 50%, transparent 50%)"; progressValue.textContent = progress + "%"; if (progress >= 100) { clearInterval(interval); } }, 50); ``` 这段 JavaScript 代码会使进度条的进度从 0% 逐渐增加到 100%。具体实现方式是通过设置 `progressBar` 元素的背景图片,利用 `linear-gradient` 渐变来实现环形进度条的效果。同时,也会动态更新进度值,将其显示在 `progressValue` 元素中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

睿智的海鸥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值