简介:
这个游戏是我在今年课余时闲着无聊做的一个冒险类小游戏,总共花了5个工作日才完成,为了游戏的效率,做了很多优化,目前在IE8以上浏览器能够流畅运行,运行时如果屏幕分辨率不兼容,你可以缩放到最佳显示效果。大家可以学习学习,有不足的地方你们可以自己改改,该款游戏是模仿最火爆的游戏“Flappy Bird”的风格。
授权:
本软件使用的是“GPL”开源协议。
截图:
主界面(日间模式):
运行时效果(日间模式):
结束时效果(日间模式):
主界面(夜间模式):
关于界面(夜间模式):
结束时效果(夜间模式):
代码说明:
这个游戏是基于HTML4开发的,所以兼容性也挺不错的,电脑配置一般的就可以流畅玩耍了。下面介绍下核心的代码。
<script type="text/javascript" language="javascript"> <!-- //分享到腾讯微博 function postToQQWB(integral,docurl){ var _t = encodeURI('你在"淘气的小鸟(Elvish Bird)"中取得了最高'+integral+'分!欢迎小朋友们来体验!'); var _url = encodeURI(docurl); var _u = 'http://v.t.qq.com/share/share.php?title='+_t+'&url='+_url; window.open( _u,'转播到腾讯微博', 'width=630, height=380, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' ); } //分享到新浪微博 function postToSINAWB(integral,docurl){ var _t = encodeURI('你在"淘气的小鸟(Elvish Bird)"中取得了最高'+integral+'分!欢迎小朋友们来体验!'); var _url = encodeURI(docurl); var _u = 'http://service.weibo.com/share/share.php?url='+_url+'&title='+_t; window.open( _u,'转播到新浪微博', 'width=630, height=380, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' ); } //写cookies function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } //读取cookies function getCookie(name) { var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)){ return unescape(arr[2]); } else{ return null; } } //删除cookies function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null){ document.cookie= name + "="+cval+";expires="+exp.toGMTString(); } } //退出效果 var quitOpc=1; function openQuitOpc(v){ if(quitOpc<=0){ v.style.display='none'; quitOpc=1; v.style.opacity=quitOpc; } else{ quitOpc=quitOpc-0.05; v.style.opacity=quitOpc; window.setTimeout(function(){openQuitOpc(v);},20); } } //进入效果 var enterOpc=0; function openEnterOpc(v){ if(enterOpc>=1){ enterOpc=0; v.style.opacity=1; } else{ v.style.display='block'; enterOpc=enterOpc+0.05; v.style.opacity=enterOpc; window.setTimeout(function(){openEnterOpc(v);},20); } } //按下键效果 function downBtn(v){ v.style.backgroundPosition='0px 2px'; } //释放键效果 function upBtn(v){ v.style.backgroundPosition='0px 0px'; } window.onload=loading; window.onblur=bluring;//页面失去焦点 window.onfocus=focusing;//页面获取焦点 window.onkeydown=keyPlaying;//按键事件 var IsStart=false;//开始正式玩耍游戏时 var IsEnterGM=false;//进入操作指南界面时 var IsPause=false;//是否暂停 var IsEndGM=false;//是否结束游戏 var IsNewShow=false;//是否显示new图 var integral=0;//当前分数 //历史分数 var oldintegral1=0; var oldintegral2=0; var oldintegral3=0; var oldintegral4=0; var maxIntegral=0;//最高分数 var main,mainBG,welcomePage,wall,logoBird,mainMenu,startPage,btnStartGM,btnRate ,btnRanking,playingPage,birdPlayer,aboutPage,lefthide,righthide,bottomhide,gameover2,gameover1,btnRetry2,btnRanking2; var obstacle1,obstacle2,obstacle3,obstacle4,obstacle5,obstacle6;//6个障碍物 var gNO1,gNO2,gNO3,gNO4; var igNO1,igNO2,igNO3,igNO4; var igNOB1,igNOB2,igNOB3,igNOB4; var bgs2,bgs; var soundclass; var medalimg,newimg; var rankinglist; var historylist1,historylist2,historylist3,historylist4,btnok,btnshare; var hisNO01,hisNO02,hisNO03,hisNO04; var hisNO11,hisNO12,hisNO13,hisNO14; var hisNO21,hisNO22,hisNO23,hisNO24; var hisNO31,hisNO32,hisNO33,hisNO34; //初始化对象 function initObj(){ main=document.getElementById('main'); mainBG=document.getElementById('mainBG'); welcomePage=document.getElementById('welcomePage'); wall=document.getElementById('wall'); logoBird=document.getElementById('logoBird'); mainMenu=document.getElementById('mainMenu'); startPage=document.getElementById('startPage'); btnStartGM=document.getElementById('btnStartGM'); btnRate=document.getElementById('btnRate'); btnRanking=document.getElementById('btnRanking'); playingPage=document.getElementById('playingPage'); birdPlayer=document.getElementById('birdPlayer'); aboutPage=document.getElementById('aboutPage'); obstacle1=document.getElementById('obstacle1'); obstacle2=document.getElementById('obstacle2'); obstacle3=document.getElementById('obstacle3'); obstacle4=document.getElementById('obstacle4'); obstacle5=document.getElementById('obstacle5'); obstacle6=document.getElementById('obstacle6'); lefthide=document.getElementById('lefthide'); righthide=document.getElementById('righthide'); bottomhide=document.getElementById('bottomhide'); gNO1=document.getElementById('gNO1'); gNO2=document.getElementById('gNO2'); gNO3=document.getElementById('gNO3'); gNO4=document.getElementById('gNO4'); igNO1=document.getElementById('igNO1'); igNO2=document.getElementById('igNO2'); igNO3=document.getElementById('igNO3'); igNO4=document.getElementById('igNO4'); igNOB1=document.getElementById('igNOB1'); igNOB2=document.getElementById('igNOB2'); igNOB3=document.getElementById('igNOB3'); igNOB4=document.getElementById('igNOB4'); bgs=document.getElementById('bgs'); bgs2=document.getElementById('bgs2'); soundclass=document.getElementById('soundclass'); gameover2=document.getElementById('gameover2'); gameover1=document.getElementById('gameover1'); btnRetry2=document.getElementById('btnRetry2'); btnRanking2=document.getElementById('btnRanking2'); medalimg=document.getElementById('medalimg'); newimg=document.getElementById('newimg'); rankinglist=document.getElementById('rankinglist'); historylist1=document.getElementById('historylist1') historylist2=document.getElementById('historylist2') historylist3=document.getElementById('historylist3') historylist4=document.getElementById('historylist4') btnok=document.getElementById('btnok') btnshare=document.getElementById('btnshare') hisNO01=document.getElementById('hisNO01'); hisNO02=document.getElementById('hisNO02'); hisNO03=document.getElementById('hisNO03'); hisNO04=document.getElementById('hisNO04'); hisNO11=document.getElementById('hisNO11'); hisNO12=document.getElementById('hisNO12'); hisNO13=document.getElementById('hisNO13'); hisNO14=document.getElementById('hisNO14'); hisNO21=document.getElementById('hisNO21'); hisNO22=document.getElementById('hisNO22'); hisNO23=document.getElementById('hisNO23'); hisNO24=document.getElementById('hisNO24'); hisNO31=document.getElementById('hisNO31'); hisNO32=document.getElementById('hisNO32'); hisNO33=document.getElementById('hisNO33'); hisNO34=document.getElementById('hisNO34'); } //初次加载 function loading(){ initObj(); hideScroll(); closeWelcomePage(); randomBirdType(); startWhile(); get_Config_SoundS(); get_Config_MaxIntegral(); get_Config_Integrallist(); } //配置管理 function set_Config_SoundS(no){ setCookie("soundoff",no); } function get_Config_SoundS(){ var no=getCookie("soundoff"); if(no=='1'){ openOrCloseSound(); } } function set_Config_MaxIntegral(maxIntegral){ setCookie("maxIntegral",maxIntegral); } function get_Config_MaxIntegral(){ var no=getCookie("maxIntegral"); if(no!=null){ maxIntegral=parseInt(no); } } function set_Config_Integrallist(){ setCookie("oldintegral1",oldintegral1); setCookie("oldintegral2",oldintegral2); setCookie("oldintegral3",oldintegral3); setCookie("oldintegral4",oldintegral4); } function get_Config_Integrallist(){ var a1=getCookie("oldintegral1"); var a2=getCookie("oldintegral2"); var a3=getCookie("oldintegral3"); var a4=getCookie("oldintegral4"); if(a1!=null){ oldintegral1=parseInt(a1); } if(a2!=null){ oldintegral2=parseInt(a2); } if(a3!=null){ oldintegral3=parseInt(a3); } if(a4!=null){ oldintegral4=parseInt(a4); } } //打开积分墙的分数 function openMedalwall(){ closeGateNO(); refreshMedalwall(); openEnterOpc(gameover1); } function refreshMedalwall(){ updateMedal(integral); setGateNO2(integral,igNO1,igNO2,igNO3,igNO4); setGateNO2(maxIntegral,igNOB1,igNOB2,igNOB3,igNOB4); if(IsNewShow){ showNewimg(); } else{ hideNewimg(); } } //显示现在new这个记号 function showNewimg(){ newimg.style.display='block'; } //隐藏new这个记号 function hideNewimg(){ newimg.style.display='none'; } //更新奖牌 function updateMedal(integral){ if(integral<10){ medalimg.style.backgroundImage='url(image/no.png)'; } else if(integral<20){ medalimg.style.backgroundImage='url(image/medal4.png)'; } else if(integral<30){ medalimg.style.backgroundImage='url(image/medal3.png)'; } else if(integral<40){ medalimg.style.backgroundImage='url(image/medal2.png)'; } else if(integral<10000){ medalimg.style.backgroundImage='url(image/medal1.png)'; } } //关闭积分墙的分数 function closeMedalwall(){ openQuitOpc(gameover1); } //打开积分显示 function openGateNO(){ gateNO.style.display='block'; } //关闭积分显示 function closeGateNO(){ gateNO.style.display='none'; } //关闭欢迎页面 function closeWelcomePage(){ openQuitOpc(welcomePage); w_CloseWelcomePage=setInterval(openMainPage,1000);//不停止检测主界面 } //打开主界面 function openMainPage(){ mainBG.style.display='block'; if(isDaytime()){//根据白天晚上 main.style.backgroundColor='#5CE172'; mainBG.style.backgroundImage="url(image/bg1.png)"; rankinglist.style.backgroundImage="url(image/bg1.png)"; lefthide.style.backgroundColor='#5CE172';//障碍物屏蔽 righthide.style.backgroundColor='#5CE172';//障碍物屏蔽 bottomhide.style.backgroundColor='#5CE172';//障碍物屏蔽 } else{ main.style.backgroundColor='#00A300'; mainBG.style.backgroundImage="url(image/bg2.png)"; rankinglist.style.backgroundImage="url(image/bg2.png)"; lefthide.style.backgroundColor='#00A300';//障碍物屏蔽 righthide.style.backgroundColor='#00A300';//障碍物屏蔽 bottomhide.style.backgroundColor='#00A300';//障碍物屏蔽 } } //离开页面事件 function bluring(){ if(IsStart){ stop_w_SendObs();//停止发送障碍物,防止堆积过多 IsPause=true; } } //进入页面事件 function focusing(){ if(IsStart){ IsPause=false; start_w_SendObs();//继续发送障碍物工作 } } //关闭主菜单 function closeMainMenu(){ openQuitOpc(mainMenu); } //开启主菜单 function openMainMenu(){ openEnterOpc(mainMenu); } //开启说明指南 function openStartPage(){ openEnterOpc(startPage); emptyGateNO(); } //关闭说明指南 var finishCloseStartPage=true;//确定是完成当前方法 function closeStartPage(){ if(finishCloseStartPage){ finishCloseStartPage=false; refreshObstacle(); openQuitOpc(startPage); IsStart=true; startUpstroke(); finishCloseStartPage=true; } } //开启关于 function openAboutPage(){ if(!IsEnterGM){//没进入准备开始前,允许打开关于界面 closeMainMenu(); openEnterOpc(aboutPage); } else{ btnPlaying(); } } //关闭关于 function closeAboutPage(){ if(!IsEnterGM){//没进入准备开始前,允许打开关于界面 openQuitOpc(aboutPage); openMainMenu(); } } //开始游戏 var finishStartGM=true;//确定是完成当前方法 function startGM(){ if(finishStartGM){ finishStartGM=false; closeMainMenu(); openStartPage(); enterGMPage(); finishStartGM=true; } } function downStartGM(){ downBtn(btnStartGM); } function upStartGM(){ upBtn(btnStartGM); } function outStartGM(){ upBtn(btnStartGM); } //btnRate function downBtnRate(){ downBtn(btnRate); } function upBtnRate(){ upBtn(btnRate); } function outBtnRate(){ upBtn(btnRate); } //btnRanking function downBtnRanking(){ downBtn(btnRanking); } function upBtnRanking(){ upBtn(btnRanking); } function outBtnRanking(){ upBtn(btnRanking); } //积分墙的btnRanking2 function downBtnRanking2(){ downBtn(btnRanking2); } function upBtnRanking2(){ upBtn(btnRanking2); } function outBtnRanking2(){ upBtn(btnRanking2); } //积分墙的btnRetry2 function retryGM(){ closeMedalwall();//关闭积分墙 refreshObstacle();//刷新道具到默认位置 birdPlayer.style.top="240px";//重置小鸟位置 integral=0; emptyGateNO();//初始化积分显示 openGateNO();//打开积分显示 IsEndGM=false; IsPause=false; IsStart=true; startUpstroke();//默认上升 start_w_SendObs();//开始发送道具 } function downBtnRetry2(){ downBtn(btnRetry2); } function upBtnRetry2(){ upBtn(btnRetry2); } function outBtnRetry2(){ upBtn(btnRetry2); } //打开成绩排行榜 function openRankinglist(){ initRankinglist(); openEnterOpc(rankinglist); } //初始化排行榜数据 function initRankinglist(){ setGateNO2(oldintegral1,hisNO01,hisNO02,hisNO03,hisNO04); setGateNO2(oldintegral2,hisNO11,hisNO12,hisNO13,hisNO14); setGateNO2(oldintegral3,hisNO21,hisNO22,hisNO23,hisNO24); setGateNO2(oldintegral4,hisNO31,hisNO32,hisNO33,hisNO34); } //关闭成绩排行榜 function closeRankinglist(){ openQuitOpc(rankinglist); } //btnok function btn_OK(){ closeRankinglist(); } function downBtnOK(){ downBtn(btnok); } function upBtnOK(){ upBtn(btnok); } function outBtnOK(){ upBtn(btnok); } //Btnshare function shareNow(){ postToQQWB(maxIntegral,'http://my.oschina.net/BruceWan/blog'); } function downBtnshare(){ downBtn(btnshare); } function upBtnshare(){ upBtn(btnshare); } function outBtnshare(){ upBtn(btnshare); } var birdPlayerMotionState=0;//0 正常 1 上扬 2 下俯 3 撞地 var birdPlayerBG=1;//小鸟背景序号 var birdPlayerFX=0;//初始动画移位方向 var NowBirdPlayerLoc=240;//默认位置 //进入游戏运行界面 function enterGMPage(){ birdPlayer.style.backgroundImage='url(image/bird'+birdtype+'_1_0.png)'; playingPage.style.display='block'; IsEnterGM=true; } //通过小鸟状态,颜色改变小鸟 function changeBirdPlayer(){ if(!IsPause&&!IsEndGM){ if(IsEnterGM){ if(birdPlayerBG==1){ birdPlayerBG=2; } else if(birdPlayerBG==2){ birdPlayerBG=3; } else{ birdPlayerBG=1; } birdStatus(); } if(!IsStart){//初始化动画 if(birdPlayerFX==0){ birdPlayer.style.top=NowBirdPlayerLoc+"px"; NowBirdPlayerLoc=NowBirdPlayerLoc-1; if(NowBirdPlayerLoc<235){ birdPlayerFX=1; } } else if(birdPlayerFX==1){ birdPlayer.style.top=NowBirdPlayerLoc+"px"; NowBirdPlayerLoc=NowBirdPlayerLoc+1; if(NowBirdPlayerLoc>244){ birdPlayerFX=0; } } } } } //改变小鸟状态 function birdStatus(){ birdPlayer.style.backgroundImage='url(image/bird'+birdtype+'_'+birdPlayerBG+'_'+birdPlayerMotionState+'.png)'; } //重置障碍物 function resettingObs(){ obstacle1.style.top='0px'; obstacle2.style.bottom='0px'; obstacle3.style.top='0px'; obstacle4.style.bottom='0px'; obstacle5.style.top='0px'; obstacle6.style.bottom='0px'; resettingObs1(obstacle1); resettingObs1(obstacle2); resettingObs1(obstacle3); resettingObs1(obstacle4); resettingObs1(obstacle5); resettingObs1(obstacle6); } function resettingObs1(Obs){ Obs.style.left='294px'; } //刷新障碍物 function refreshObstacle(){ resettingObs(); } //发送障碍物 var ObsJudgeValue=294; function sendObs(){ if(IsStart&&!IsPause&&!IsEndGM){ var A=randomNext(80,240);//可见高度 var AObsH=320-A;//获取A障碍物的不可见高度 var BObsH=doBObsTop(95,A);//通过A获取B障碍物的不可见高度 if(obstacle1.offsetLeft>=ObsJudgeValue&&obstacle2.offsetLeft>=ObsJudgeValue){ sendObs2(AObsH,BObsH,obstacle1,obstacle2); } else if(obstacle3.offsetLeft>=ObsJudgeValue&&obstacle4.offsetLeft>=ObsJudgeValue){ sendObs2(AObsH,BObsH,obstacle3,obstacle4); } else if(obstacle5.offsetLeft>=ObsJudgeValue&&obstacle6.offsetLeft>=ObsJudgeValue){ sendObs2(AObsH,BObsH,obstacle5,obstacle6); } } } function sendObs2(AObsH,BObsH,obstacle1,obstacle2){ obstacle1.style.top='-'+AObsH+'px'; obstacle2.style.bottom='-'+BObsH+'px'; whileObs(obstacle1,false,0); whileObs(obstacle2,false,1); } //循环障碍物 function whileObs(Obs,oldPassObs,ObsFX){ if(Obs.offsetLeft<-58){ Obs.style.left='294px'; } else{ if(IsStart&&!IsEndGM){ if(!IsPause){ Obs.style.left=(Obs.offsetLeft-WallSpeed)+'px'; checkObsCollide(Obs,birdPlayer,ObsFX); if(birdPlayer.offsetLeft>=(Obs.offsetLeft+52)&&!oldPassObs){ oldPassObs=true;//通过当前关卡 setIntegral(); } } window.setTimeout(function(){ whileObs(Obs,oldPassObs,ObsFX);},synchroFloorObs); } } } //写入积分 function setIntegral(){ integral=integral+0.5; if(Math.floor(integral)==integral){ setGateNO(integral,gNO1,gNO2,gNO3,gNO4); playSound3(); } } //写入最大的积分 function setMaxIntegral(){ if(maxIntegral<integral){ maxIntegral=integral; IsNewShow=true; } else{ IsNewShow=false; } } //检测是否与障碍物碰撞 function checkObsCollide(Obs,birdPlayer,ObsFX){//误差校正 birdPlayer-W28 Obs-W48 if(Obs.offsetLeft<=(birdPlayer.offsetLeft+28)){//是否进入障碍物左范围内 x轴检测 if((Obs.offsetLeft+48)>=birdPlayer.offsetLeft){//是否进入障碍物右范围内 x轴检测 if(ObsFX==0){//上障碍物 if(birdPlayer.offsetTop<=(320+Obs.offsetTop)){//是否碰到障碍物部分 y轴检测 birdAndObsCollide(); birdPlayerMotionState=3; birdStatus(); verticalDescend(); } } else{//下障碍物 if((birdPlayer.offsetTop+28)>=Obs.offsetTop){//是否碰到障碍物部分 y轴检测 birdAndObsCollide(); birdPlayerMotionState=3; birdStatus(); verticalDescend(); } } } } } //获取随机数 function randomNext(under,over){ switch(arguments.length){//检测传入几个参数 case 1: return parseInt(Math.random()*under+1); case 2: return parseInt(Math.random()*(over-under+1) + under); default: return 0; } } //计算B障碍物长度 预留长度 可见高度 function doBObsTop(step,AHight){ return 320-(402-AHight-step);//A B 320px } //将数据写到积分处 v 分数 c是数字对应的控件框 function setGateNO(v,c1,c2,c3,c4){ var a,b,c,d; if(v<10){ c1.style.backgroundImage='url(image/no.png)'; c2.style.backgroundImage=NOToImg(v); c3.style.backgroundImage='url(image/no.png)'; c4.style.backgroundImage='url(image/no.png)'; } else if(v/10<10){ c=Math.floor(v/10); c1.style.backgroundImage='url(image/no.png)'; c2.style.backgroundImage=NOToImg(c); c3.style.backgroundImage=NOToImg(v-c*10); c4.style.backgroundImage='url(image/no.png)'; } else if(v/100<10){ b=Math.floor(v/100); c=Math.floor((v-b*100)/10); c1.style.backgroundImage='url(image/no.png)'; c2.style.backgroundImage=NOToImg(b); c3.style.backgroundImage=NOToImg(c); c4.style.backgroundImage=NOToImg(v-b*100-c*10); } else if(v/1000<10){ a=Math.floor(v/1000); b=Math.floor((v-a*1000)/100) c=Math.floor((v-a*1000-b*100)/10) c1.style.backgroundImage=NOToImg(a); c2.style.backgroundImage=NOToImg(b); c3.style.backgroundImage=NOToImg(c); c4.style.backgroundImage=NOToImg(v-a*1000-b*100-c*10); } else{ c1.style.backgroundImage=NOToImg(8); c2.style.backgroundImage=NOToImg(8); c3.style.backgroundImage=NOToImg(8); c4.style.backgroundImage=NOToImg(8); } } //将数据写到积分处 v 分数 c是数字对应的控件框 function setGateNO2(v,c1,c2,c3,c4){ var a,b,c,d; if(v<10){ c1.src='image/no.png'; c2.src='image/no.png'; c3.src='image/no.png'; c4.src=NOToImg2(v); } else if(v/10<10){ c=Math.floor(v/10); c1.src='image/no.png'; c2.src='image/no.png'; c3.src=NOToImg2(c); c4.src=NOToImg2(v-c*10); } else if(v/100<10){ b=Math.floor(v/100); c=Math.floor((v-b*100)/10); c1.src='image/no.png'; c2.src=NOToImg2(b); c3.src=NOToImg2(c); c4.src=NOToImg2(v-b*100-c*10); } else if(v/1000<10){ a=Math.floor(v/1000); b=Math.floor((v-a*1000)/100) c=Math.floor((v-a*1000-b*100)/10) c1.src=NOToImg2(a); c2.src=NOToImg2(b); c3.src=NOToImg2(c); c4.src=NOToImg2(v-a*1000-b*100-c*10); } else{ c1.src=NOToImg2(8); c2.src=NOToImg2(8); c3.src=NOToImg2(8); c4.src=NOToImg2(8); } } //默认显示 function emptyGateNO(){ gNO1.style.backgroundImage='url(image/no.png)'; gNO2.style.backgroundImage='url(image/no0.png)'; gNO3.style.backgroundImage='url(image/no.png)'; gNO4.style.backgroundImage='url(image/no.png)'; } //将数字转化为图片 function NOToImg(v){ return 'url(image/no'+v+'.png)'; } //将数字转化为图片 function NOToImg2(v){ return 'image/no'+v+'.png'; } //开始按键玩耍 function btnPlaying(){ if(IsStart&&!IsPause&&!IsEndGM){ if(isUpstrokeFinsh){ isDescendFinish=true;//关闭下降 playSound4(); startUpstroke(); } } } function keyPlaying(){ if(event.keyCode==13||event.keyCode==32) { btnPlaying(); } } //上升方法 var isUpstrokeFinsh=true;//是否完成上升 var upstrokeFinshOfs=10;//当前上升值 function btnUpstroke(){ if(!isUpstrokeFinsh){ if(upstrokeFinshOfs<=0||birdPlayer.offsetTop<=0){ isUpstrokeFinsh=true; startBirdDescend(); } else{ upstrokeFinshOfs=upstrokeFinshOfs-1; birdPlayer.style.top=(birdPlayer.offsetTop-upstrokeFinshOfs)+'px'; window.setTimeout(btnUpstroke,38); } } } //启动上升 function startUpstroke(){ upstrokeFinshOfs=10; isUpstrokeFinsh=false;//开启上升 birdPlayerMotionState=1;//启动上升动画 btnUpstroke();//启动上升 } //下降方法 全局循环检测 var isDescendFinish=true; var descendFinishOfs=0;//当前下降值 function btnDescend(){ if(IsStart){ if(birdPlayer.offsetTop>=371&&!IsEndGM){//撞上地面 descendFinishOfs=0;//初速度 birdAndObsCollide(); verticalDescend(); } else{ if(!isDescendFinish&&!IsPause&&!IsEndGM){ descendFinishOfs=descendFinishOfs+1; birdPlayer.style.top=(birdPlayer.offsetTop+descendFinishOfs)+'px'; if(descendFinishOfs==10){//改变小鸟方向 birdPlayerMotionState=2; } else if(descendFinishOfs==15){//改变小鸟方向 birdPlayerMotionState=3; } } } } } //小鸟碰撞 function birdAndObsCollide(){//方法的位置不能乱放 bluring();//全局暂停 isDescendFinish=true;//降到了地面 IsEndGM=true;//结束游戏 IsStart=false; playSound2();//播放声音 gameover_ss();//闪屏 setMaxIntegral();//写入最大值 11->22 set_Config_MaxIntegral(maxIntegral);//写入持久化最大值 11->22 setHistoryScore();//1->2 set_Config_Integrallist();//1->2 } //写入历史分数 function setHistoryScore(){ if(integral>oldintegral1){ var a1=oldintegral1; var a2=oldintegral2; var a3=oldintegral3; oldintegral1=integral; oldintegral2=a1; oldintegral3=a2; oldintegral4=a3; } else if(integral>oldintegral2){ var a2=oldintegral2; var a3=oldintegral3; oldintegral2=integral; oldintegral3=a2; oldintegral4=a3; } else if(integral>oldintegral3){ var a3=oldintegral3; oldintegral3=integral; oldintegral4=a3; } else if(integral>oldintegral4){ oldintegral4=integral; } } //启动下降 function startBirdDescend(){ descendFinishOfs=0; birdPlayerMotionState=0; isDescendFinish=false; } //启动垂直下落 var verticalDescendFinshOfs=0; function verticalDescend(){ if(IsEndGM){ if(birdPlayer.offsetTop>=371){ verticalDescendFinshOfs=0;//初速度 birdPlayerMotionState=3; birdStatus(); openMedalwall(); } else{ verticalDescendFinshOfs=verticalDescendFinshOfs+1; birdPlayer.style.top=(birdPlayer.offsetTop+verticalDescendFinshOfs)+'px'; if(verticalDescendFinshOfs==15){ playSound1(); } window.setTimeout(verticalDescend,38); } } } //游戏结束时的闪屏 function gameover_ss(){ gameover2.style.display='block'; openQuitOpc(gameover2); } //播放音频 var openSound=true; function playSound1(){ if(openSound){ bgs2.innerHTML=playSound("sound/sfx_die.mp3"); } } function playSound2(){ if(openSound){ bgs.innerHTML=playSound("sound/sfx_hit.mp3"); } } function playSound3(){ if(openSound){ bgs2.innerHTML=playSound("sound/sfx_point.mp3"); } } function playSound4(){ if(openSound){ bgs.innerHTML=playSound("sound/sfx_wing.mp3"); } } function playSound5(){ if(openSound){ bgs.innerHTML=playSound("sound/sfx_swooshing.mp3"); } } function playSound(url){ return "<embed src=\""+url+"\" autoplay=\"true\" hidden=\"true\" loop=\"false\" width=\"0px\" height=\"0px\" type=\"audio/mpeg\"></embed>"; } var NowLogoBirdLoc=18;//默认位置 var NowLogoBirdFX=0;//logo中小鸟的方向 var NowLogoBirdBG=1;//小鸟的序号 var birdtype=1;//小鸟类型 //随机修改封面小鸟颜色 function randomBirdType(){ var type=0; while(true){ type=Math.round(Math.random()*10); if(type<4&&type>0){ birdtype=type; break; } } } //logoBird的运动效果 function logoSportsBird(){ if(!IsPause){ if(NowLogoBirdFX==0){ logoBird.style.marginTop=NowLogoBirdLoc+"px"; NowLogoBirdLoc=NowLogoBirdLoc-1; if(NowLogoBirdLoc<18){ NowLogoBirdFX=1; } } else if(NowLogoBirdFX==1){ logoBird.style.marginTop=NowLogoBirdLoc+"px"; NowLogoBirdLoc=NowLogoBirdLoc+1; if(NowLogoBirdLoc>25){ NowLogoBirdFX=0; } } if(NowLogoBirdBG==1){ logoBird.style.backgroundImage='url(image/bird'+birdtype+'_'+NowLogoBirdBG+'_0.png)'; NowLogoBirdBG=2; } else if(NowLogoBirdBG==2){ logoBird.style.backgroundImage='url(image/bird'+birdtype+'_'+NowLogoBirdBG+'_0.png)'; NowLogoBirdBG=3; } else{ logoBird.style.backgroundImage='url(image/bird'+birdtype+'_'+NowLogoBirdBG+'_0.png)'; NowLogoBirdBG=1; } } } //开启全局循环 var w_Wall,w_CloseWelcomePage,w_LogoSportsBird,w_ChangeBirdPlayer,w_SendObs,w_BtnDescend; function startWhile(){ if(w_Wall==null){ w_Wall=window.setInterval(whileWall,synchroFloorObs); } if(w_LogoSportsBird==null){ w_LogoSportsBird=window.setInterval(logoSportsBird,150); } if(w_ChangeBirdPlayer==null){ w_ChangeBirdPlayer=window.setInterval(changeBirdPlayer,150); } if(w_SendObs==null){ w_SendObs=window.setInterval(sendObs,sendObsTime); } if(w_BtnDescend==null){ w_BtnDescend=window.setInterval(btnDescend,60); } } //关闭全局循环 function stopWhile(){ } //开关声音 function openOrCloseSoundUP(ev){ var oEvent=ev||event; if(oEvent.button==1||oEvent.button==0){//谷歌、IE//单左键 } else if(oEvent.button==2){//谷歌、IE右键 openOrCloseSound(); } else if(oEvent.button==3){//IE兼容按下左右键 } } function openOrCloseSound(){ if(openSound){ openSound=false; soundclass.style.backgroundImage='url(image/soundopen.png)'; set_Config_SoundS("1"); } else{ openSound=true; soundclass.style.backgroundImage='url(image/soundoff.png)'; set_Config_SoundS("0"); } } //停止循环发送障碍物,防堆积 function stop_w_SendObs(){ if(w_SendObs!=null){ window.clearInterval(w_SendObs); w_SendObs=null; } } //开启循环发送障碍物,防堆积 var sendObsTime=2700; function start_w_SendObs(){ if(w_SendObs==null){ w_SendObs=window.setInterval(sendObs,sendObsTime); } } //隐藏进度条 function hideScroll(){ document.body.style.overflow="hidden"; } //屏蔽右键菜单 function OffRightMenu(){ event.returnValue=false; return false; } //循环墙运动动画 var synchroFloorObs=50; var WallSpeed=3;//背景移动速度 var WallLoc=0;//背景的默认位置 var MaxLoc=99999999;//背景的最大位置 function whileWall(){ if(!IsPause&&!IsEndGM){ if(WallLoc<MaxLoc){ wall.style.backgroundPosition="-"+WallLoc+"px 0px"; WallLoc=WallLoc+WallSpeed; } else{ WallLoc=0; } } } //转到我的社区 function openRoom(){ window.open("http://my.oschina.net/BruceWan/blog","开发者的空间",""); } //是否是白天 function isDaytime(){ var dateTime=new Date(); var hours=dateTime.getHours(); if(hours>6&&hours<19){ return true; } else{ return false; } } --> </script>
以上是游戏的节选逻辑。
代码下载地址:
ElvishBird(快速版)
ElvishBird(慢速版)
联系方式:
QQ:1101587171 或 975738768
备注:
1.点击空格键、鼠标左键、回车键实现小鸟跳跃。
2.代码的具体内容,你可以下载并查看。