最近在学习前端,看到偶尔看到前端小游戏,就想自己写一个小游戏,奈何水平有限,只能写打气球这种简单的,所有的气球都是动态生成的,气球的颜色也是随机的

html部分

    <div class="container">
</div>
// 只创建了一个div大盒子用于游戏背景,所有的气球都是动态生成的
css部分
// .box位于.container的下一级,动态生成,用于存储气球
.box{
position:absolute;
width: 80px;
height: 80px;
bottom:-10px;
}
// 气球的样式
.box div{
position:absolute;
width:80px;
height:80px;
border-radius:44px 44px 44px 20px;
transform:rotate(-45deg) scale(1);
}
// 气球尾部的样式,.balloon是自动生成的气球,.clipped是为了产生爆炸效果,点击气球时生成的
.balloon:after,.clipped:after{
content:"";
height: 0;
width: 0;
display:block;
border-top:5px solid transparent;
border-right:5px solid transparent;
border-bottom:5px solid transparent;
transform:rotate(-45deg);
position:relative;
top:72px;
left:-7px;
}
// 气球形态是通过css中border来制作的,气球的尾部是在伪元素after中利用border边框完成的,气球的颜色是在在js中利用css属性给气球添加box-shadow属性实现的 js部分
//点击气球产生爆炸效果,并下降
$(function(){
var num=0;
var r,g,b;
function create(){
num++;
    //创建气球,添加颜色
r=Math.floor(Math.random()*256);
g=Math.floor(Math.random()*256);
b=Math.floor(Math.random()*256);
var box=$("<div class='box'>").appendTo(".container");
var ele=$("<div class='balloon'>").appendTo(box).
css({"boxShadow":"-10px 10px 25px"+" rgb("+r+","+g+","+b+") "+"inset",
"border":"1px solid"+" rgb("+r+","+g+","+b+")"}).addClass("balloon"+num);
box.css({"left":Math.random()*($("body").width()-40)});
      //给伪元素添加样式的方法有多种,这里用了给元素添加一个style样式,style标签中是伪元素样式
ele.append("<style>.balloon"+num+":after{border-left:15px solid "+"rgb("+r+","+g+","+b+")}</style>");
}
setInterval(function(){
create();
},2000);
//移动
function move(){
var timer=setInterval(function(){
$(".box").css({"bottom":"+=3"});
},50);
}
move();
//产生任意值
function rand(max,min){
return Math.floor(Math.random()*(max-min)+min)
}
//点击气球爆炸
//创造碎片
  //动态创建的元素添加事件利用$("")是选取不到的,必须凌on来绑定
//$(".container .box").on("click",".box",function(){ 这样是不行的
$(".container").on("click",".box",function(){
var $b = $(".balloon");
var count = 4;
var width = $b.width() / count;
var height = $b.width() / count;
var y = 0;
var color = $(this).find(".balloon").css("boxShadow").split(" ");
//创建16个气球碎片
for (var z = 0; z <= count * width; z += width) {
        // 创建了在一个盒子里创建了16个与盒子中已有的气球一样样式的气球,并利用clip:rect(上,右,下,左)样式对16个气球进行裁剪,构成气球碎片
$(this).append("<div class='clipped' style='clip:rect(" + y + "px," + (z + width) + 'px,' + (y + height) + "px," + z + "px" + ")'>");
if (z === (count * width) - width) {
y += height;
z = -width;
}
if (y === (count * height)) {
z = 99999;
} } $(this).find(".balloon").remove(); //给碎片添加样式 $(this).find(".clipped").each(function (i,val) { $(this).css({"boxShadow": color.join(" ")}); $(this).append("<style>.clipped:after{border-left:15px solid " + color[0]+color[1]+color[2] + "}</style>"); var v=rand(120,90), angle=rand(89,80), theta=(angle*Math.PI)/180, g=-9.8, t=0, nx,ny; var navigate=[-1,0,1]; var direction=navigate[Math.floor(Math.random()*navigate.length)]; var randDeg=rand(10,-5); var randScale=rand(1.1,0.9); var randDeg2=rand(30,5); var self=$(this); $(this).css( {"transform":"rotateZ("+randDeg2+"deg) "+"scale("+randScale+") "+"skew("+randDeg+")"} ); var z=setInterval(function(){ var ux=(Math.cos(theta)*v)*direction; var uy=(Math.sin(theta)*v)-(-g)*t; nx=ux*t; ny=uy*t+0.5*Math.pow(t,2)*g; self.css({"bottom":ny+"px","left":nx+"px"}); t+=0.1; if(t>=15){ clearInterval(z); self.remove(); } },10); }); });});

}
$(this).find(".balloon").remove();
//给碎片添加样式
利用each遍历给每个气球添加不同样式,偏转角度,放大缩小,水平速度,垂直速度等
$(this).find(".clipped").each(function (i,val) {
$(this).css({"boxShadow": color.join(" ")});
$(this).append("<style>.clipped:after{border-left:15px solid " + color[0]+color[1]+color[2] + "}</style>");
var v=rand(120,90), angle=rand(89,80), theta=(angle*Math.PI)/180, g=-9.8, t=0, nx,ny;
var navigate=[-1,0,1];
var direction=navigate[Math.floor(Math.random()*navigate.length)];
var randDeg=rand(10,-5);
var randScale=rand(1.1,0.9);
var randDeg2=rand(30,5);
var self=$(this);
$(this).css(
{"transform":"rotateZ("+randDeg2+"deg) "+"scale("+randScale+") "+"skew("+randDeg+")"}
);
// 添加一个定时器,利用定位是碎片动起来,当时间超过15s时停止定时器,并删除碎片,减少节点数量,减轻页面加载压力
var z=setInterval(function(){
var ux=(Math.cos(theta)*v)*direction;
var uy=(Math.sin(theta)*v)-(-g)*t;
nx=ux*t;
ny=uy*t+0.5*Math.pow(t,2)*g;
self.css({"bottom":ny+"px","left":nx+"px"});
t+=0.1;
if(t>=15){
clearInterval(z);
self.remove();
}
},10);
}); }); });
 
 

jQuery 打气球小游戏 点击气球爆炸效果的更多相关文章

  1. 自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药

    自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm ...

  2. js实现点气球小游戏

    二话不说直接贴代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. jQuery实现拼图小游戏

    小熊维尼拼图                                                                                    2017-07-23 ...

  4. js、jQuery实现2048小游戏

    2048小游戏 一.游戏简介:  2048是一款休闲益智类的数字叠加小游戏 二. 游戏玩法: 在4*4的16宫格中,您可以选择上.下.左.右四个方向进行操作,数字会按方向移动,相邻的两个数字相同就会合 ...

  5. JQuery实现1024小游戏

    最近用Jqery写了一个1024小游戏,由于是第一次写小游戏,所以就选了一个基础的没什么难度游戏.具体实现如下: 首先在开发时将整个游戏分成两层(自认为),底层是游戏的数据结构以及对数据的操作,上层是 ...

  6. 基于jQuery的2048小游戏设计(网页版)

    上周模仿一个2048小游戏,总结一下自己在编写代码的时候遇到的一些坑. 游戏规则:省略,我想大部分人都玩过,不写了 源码地址:https://github.com/xinhua6/2048game.g ...

  7. JS实现植物大战僵尸小游戏,代码记录及效果展示

    前几天看到了一个很有趣的demo,用js制作植物大战僵尸小游戏,本着学习的心态,对照着做了一下,发现这里面的一些代码设计的确很精妙,这里分享下源码和效果,如果有需要,可以看下. 效果如下: 下载地址

  8. js 面向对象 打气球小游戏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. jquery实现抽奖小游戏

    在很多网站或游戏活动中我们都会看到有关抽奖的活动或界面: 下面我将给大家介绍下如何通过javascript来实现这样的一个抽奖功能,主要从下面三个步骤入手(文中着重介绍第三部分有关功能的实现): 1. ...

随机推荐

  1. height百分比以及高度自适应问题

    1.  你曾经是否说想要 高度占页面或者占div百分比无效的问题,相信你也搜索过了,就是说 需要 设置父亲父亲一直到祖宗html都要设置百分比,才有效果. 总之一句话:想用百分比设置他的高度,则它的父 ...

  2. 原生js的math对象

    Math对象方法 //返回最大值 var max=Math.max(95,93,90,94,98); console.log(max); //返回最小值 var min=Math.min(95,93, ...

  3. flash8中利用遮罩制作图片切换效果

    http://www.56.com/w73/play_album-aid-8642763_vid-NDY5ODU2Mzg.html

  4. Word中划线的方法(五种)

    Word中划线的方法(五种): 1. 按CTRL+F9,在出现的黑底花括号内,如图输入内容, 最后按SHIFT+F9(或者右键菜单点切换域代码),以后可以反复按ALT+F9在代码与结果之间切换. 注: ...

  5. git 无法忽略Android Studio 生成的 .idea目录解决办法

    在Android Studio中导入了别的人Gradle项目,产生了 .idea文件夹, 然后git 发现了这个变动,修改了 .gitignore不起作用,仍然不能忽略这个文件夹 在项目目录里面 右键 ...

  6. php nginx 获取header信息

    nginx中可能没有getallheaders函数 因此编写新函数 function NginxGetAllHeaders(){//获取请求头 $headers = []; foreach ($_SE ...

  7. TELNET_COMMAND

    TELENT COMMAND DEFINE:Telnet is a command control your cmd windows of remote computer. step: 1.Open ...

  8. 字段处理rtrim去掉结尾的特殊字符和空格

    SQL> create table ycrtest2 (name varchar2(50)); Table created. SQL> insert into ycrtest2 value ...

  9. Java并发程序基础

    Thread.stop() 直接终止线程,并且会立即释放这个线程所持有的锁. Thread.interrupt() 并不会是线程立即退出,而是给线程发送一个通知,告知目标线程,有人希望你退出啦,至于目 ...

  10. EntityFramework Code-First教程(一)

    前言:学习了EF框架这么久,还没有好好总结一番,正好遇到一国外的网站,发现不错,随即翻译过来,一是让自己复习一遍,二是供广大初学者学习,翻译过程中加入了一些自己的理解,如有错误,还请指出,多谢多谢.好 ...