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

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

图片

源代码

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

相关推荐
Laravel技术社区16 小时前
用PHP8实现斗地主游戏,实现三带一,三带二,四带二,顺子,王炸功能(第二集)
前端·游戏·php
m0_7381207217 小时前
应急响应——知攻善防Web-3靶机详细教程
服务器·前端·网络·安全·web安全·php
hh随便起个名1 天前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
我是小路路呀1 天前
element级联选择器:已选中一个二级节点,随后又点击了一个一级节点(仅浏览,未确认选择),此时下拉框失去焦点并关闭
javascript·vue.js·elementui
程序员爱钓鱼1 天前
Node.js 编程实战:文件读写操作
前端·后端·node.js
PineappleCoder1 天前
工程化必备!SVG 雪碧图的最佳实践:ID 引用 + 缓存友好,无需手动算坐标
前端·性能优化
JIngJaneIL1 天前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码1 天前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web
澄江静如练_1 天前
列表渲染(v-for)
前端·javascript·vue.js