html+css+js气球消除小游戏

气球消除小游戏 消除15个就成功 源码在图片后 点赞加关注,谢谢 左上角的数字显示消除气球的数量 定时随机生成气球 🎈🎈🎈

图片

8e02c484e8dd4057813e546dca1228c3.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: #f0f0f0;
        margin: 0;
        font-family: Arial, sans-serif;
    }
    #score {
        position: absolute;
        top: 10px;
        left: 10px;
        font-size: 2rem;
    }
    .balloon {
        width: 160px; /* 调整宽度以匹配高度,使气球更圆 */
        height: 160px; /* 保持高度不变 */
        border-radius: 50%; /* 调整为50%,使气球主体更圆 */
        background: linear-gradient(#ffdad6, #ff8080);
        position: absolute;
        cursor: pointer;
    }
    .balloon:after {
        content: "";
        width: 4px;
        height: 40px;
        background-color: darkgray;
        position: absolute;
        bottom: -40px;
        left: calc(50% - 2px);
    }
</style>
</head>
<body>
<div id="score">0</div>
<script>
    let score = 0;
    const scoreElement = document.getElementById('score');
    const colors = ["#ff8080", "#80ff80", "#8080ff", "#ffff80", "#80ffff"];

    function createBalloon() {
        const balloon = document.createElement('div');
        balloon.className = 'balloon';
        balloon.style.backgroundColor = `linear-gradient(#e6e6fa, ${colors[Math.floor(Math.random() * colors.length)]})`;
        balloon.style.left = Math.random() * (window.innerWidth - 160) + 'px'; /* 调整left值以避免超出屏幕 */
        balloon.style.top = Math.random() * (window.innerHeight - 160) + 'px';
        document.body.appendChild(balloon);

        balloon.addEventListener('click', () => {
            balloon.remove();
            score++;
            scoreElement.textContent = score;
            if (score > 15) {
                alert('恭喜你!你赢了!');
                location.reload(); // 重新加载页面
            }
        });
    }

    window.onload = function() {
        setInterval(createBalloon, 1000);
        createBalloon();
    };
</script>
</body>
</html>

  • 16
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值