Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47692
  • 博文数量: 34
  • 博客积分: 831
  • 博客等级: 军士长
  • 技术积分: 310
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-08 11:15
文章分类

全部博文(34)

文章存档

2012年(34)

我的朋友

分类: 系统运维

2012-12-08 14:02:43

Html5也学了一个阶段了,刚把离线缓存学了一下,就想写个游戏,挑了一个简单的来练练手,打地鼠。

这款游戏用到了一些html5的东西算是一点小亮点吧,比如说开场动画了,升级失败的字,还有离线缓存了,主要算法一般般,对JS的面向对象不是很熟悉,JS不支持继承要通过构造函数来实现继承偷懒就没仔细去研究,所以js代码自认为写的不是很好。写游戏嘛用面向对象写起来应该爽吧!下边给大家看一下开场动画的画面吧!

用的是蒙版的原理实现了灯光的效果,用了离线缓存实现的进度条

。再看一下里面的东西吧

一些字体什么的都是用了CSS3的东西看起来很炫啊……地鼠的动作也是用到了CSS3

主要的JS算法流程是:通过一个创建的地鼠的方法来不停的创建地鼠:


 

点击(此处)折叠或打开

  1. /*
  2.         *创建地鼠方法
  3.         *计数器t是管创建的,计时器ts[i]管地鼠逃生
  4.         *t2是创建地鼠需要的时间
  5.         *t1是地鼠逃生在地面待的时间
  6.         */
  7.         function createmouse(){
  8.             var hidemouse=document.querySelectorAll('.mouse[data-show^="0"]');
  9.             var randomNum = Math.round(Math.random()*(hidemouse.length-1));            
  10.             if(hidemouse.length==0){
  11.                 t = setTimeout(createmouse,t2);
  12.                 return;                
  13.             }
  14.             hidemouse[randomNum].setAttribute('data-show','1');
  15.             
  16.             for(var i=0;i<mouses.length;i++){
  17.                 if(hidemouse[randomNum]==mouses[i]){
  18.                     tsnum[i]=i;
  19.                     break;
  20.                 }
  21.             }
  22.             mouseboolean[i]=true;
  23.             (function(i){ts[i]=setTimeout(function(){shibai(i)},t1);})(i);
  24.             t = setTimeout(createmouse,t2);        
  25.         }
  26. 通过一个打到地鼠的方法来判断是否打中地鼠:
  27. /*
  28.         *地鼠被揍的方法
  29.         *
  30.         */
  31.         function disappear(e){            
  32.             if(!suspendnum){
  33.                 if(!shengjiboolean){
  34.                     if(e.target.className=='mouse'){                    
  35.                         for(var i=0;i<mouses.length;i++){
  36.                             if(e.target==mouses[i]){
  37.                             tsnum[i]=undefined;                            
  38.                                 break;
  39.                             }
  40.                         }
  41.                         
  42.                         if(mouseboolean[i]){
  43.                             clearTimeout(ts[i]);
  44.                             e.target.setAttribute('data-show','1_1');
  45.                             shengjinumall++;
  46.                             scores+=score;
  47.                             mouseboolean[i]=false;
  48.                             scoreall.innerHTML='分数:'+scores+'    级别:'+jibie+'    机会:'+hp;
  49.                             tall[i]=setTimeout(function(){e.target.setAttribute('data-show','0_1');},220);            
  50.                             if(shengjinumall==shengjinum){t1=1500;t2=3000;score=2000;jibie=2;shengji1();}
  51.                             else if(shengjinumall==2*shengjinum){t1=1000;t2=2000;score=3000;jibie=3;shengji1();}
  52.                             else if(shengjinumall==3*shengjinum){t1=900;t2=1500;score=4000;jibie=4;shengji1();}
  53.                             else if(shengjinumall==4*shengjinum){t1=800;t2=1000;score=5000;jibie=5;shengji1();}
  54.                             else if(shengjinumall==5*shengjinum){t1=1200;t2=700;score=6000;jibie=6;shengji1();}
  55.                         }                                                                                
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.         mousediv.addEventListener('click',function(e){disappear(e);},false);
  61. 还有一个是没有打中地鼠的方法:
  62. /*
  63.         *地鼠逃生方法
  64.         *hp地鼠逃生次数
  65.         */
  66.         function shibai(num){
  67.             if(hp==1){
  68.                 suspendnum=true;                
  69.                 startnum=false;    
  70.                 hp=0;
  71.                 mouses[num].setAttribute('data-show','1_2');
  72.                 tall[num]=setTimeout(function(){
  73.                     mouses[num].setAttribute('data-show','0_2');
  74.                     if(musicboolean){jianxiao.play();}                    
  75.                     },220);                
  76.                 scoreall.innerHTML='分数:'+scores+'    级别:'+jibie+'    机会:'+hp;
  77.                 cleart();
  78.                 tfail=setTimeout(function(){
  79.                     if(musicboolean){shibai_bg.play();}                    
  80.                     fail.style.display='block';
  81.                     setTimeout(function(){fail.style.top='300px';},17);                    
  82.                     fail.innerHTML='你输了!!!分数:'+scores+' 级别:'+jibie+'打死的地鼠:'+shengjinumall;    
  83.                     for(var i=0;i<mouses.length;i++){                        
  84.                         mouses[i].setAttribute('data-show','2');
  85.                     }                                                                                        
  86.                 },1000);
  87.                                                 
  88.             }
  89.             else if(hp<=3&&hp>1){
  90.                 hp--;                
  91.                 mouses[num].setAttribute('data-show','1_2');
  92.                 tall[num]=setTimeout(function(){
  93.                     mouses[num].setAttribute('data-show','0_2');
  94.                     if(musicboolean){jianxiao.play();}
  95.                 },220);
  96.                 scoreall.innerHTML='分数:'+scores+'    级别:'+jibie+'    机会:'+hp;
  97.                 tsnum[num]=undefined;                
  98.             }            
  99.         }


 

主要就是这三个方法,当然还有其他很多方法来实现一些边边角角,是游戏完整起来。通过这次游戏的编写发现JS真的跟那些专门的后台语言java,c++差远了啊。当然它也有它的优势哈!继续努力中,我也是菜鸟把具体代码放到附件中了供大家参考,一起努力啊!

 打地鼠服务器版.rar    打地鼠普通版.rar   

阅读(1395) | 评论(0) | 转发(0) |
0

上一篇:音乐歌词

下一篇:锅大灰太狼

给主人留下些什么吧!~~