共 3 条评论关于"js打飞机游戏"
最新评论
说到打飞机,大家一定能想起来微信的打飞机小游戏,我们今天介绍的打飞机游戏和微信那款有异曲同工之妙,接下来我们就来看看是如何实现的吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | <!DOCTYPE html> <html onselectstart="return false"> <head> <meta charset="UTF-8"> <meta name="Author" content="FengYu"> <title>爱前端博客</title> <style> *{margin:0;padding:0;font-family: Microsoft YaHei,serif;} li{list-style: none;} body{ background: #000; } #wrap{ width: 300px; height: 500px; border:10px solid #fff; margin:50px auto; } #box{ position: relative; width:100%; height:100%; } #box h1{ padding:30px; text-align: center; color: #fff; } #box .pattern{ width: 150px; height: 30px; font-size:14px; text-align: center; line-height:30px; background: #fff; margin:55px auto; cursor:pointer; } #box .pattern.red{ font-size:16px; font-weight: bold; color:red; background: skyblue; } #box .plane,#box .biu,#box .enemy,#box .boom{ position: absolute; } #box .GG{ position:absolute; top:0; right:0; bottom:0; left:0; margin:auto; width:200px; height: 400px; background: #fff; } #box .GGh1{ text-align: center; padding-top:30px; color:#000; } #box .score{ position: absolute; top: 0; left: 0; color:#fff; font-weight: bold; } #box p{ text-align: center; } #box .continue{ position: absolute; bottom:20px; left: 50%; width:100px; height: 30px; margin-left:-50px; background: #000; color:red; text-align: center; line-height:30px; font-weight: bold; cursor: pointer; } </style> </head> <body> <div id="wrap"> <div id="box"></div> </div> <script> /* * * 本次案例不考虑兼容问题 * * */ (function () { var box = document.getElementById("box"); init(); //初始化界面函数 function init() { box.innerHTML = ""; var H1 = document.createElement("h1"); H1.innerHTML = "飞机大战v1.0"; box.appendChild(H1); //var Div = document.createElement("div"); //把div的内容用数组存起来 var arrInit = ["简单","一般","困难","风的力量"]; //循环生成4个模式的盒子 for(var i=0;i<4;i++){ var Div = document.createElement("div"); Div.innerHTML = arrInit[i]; Div.className = i===3?"pattern red":"pattern"; Div.i = i; //每个模式的盒子都有一个点击事件 Div.onclick=function (e) { //开始函数执行 start(this.i,e); }; box.appendChild(Div); } } //开始函数 function start(index,e){ //清空box box.innerHTML = ""; //生成我军和子弹的函数执行 //var feiji = myPlane(index,e);//执行生成我方战机函数 enemy(index,myPlane(index,e));//执行生成敌军函数 score();//计分板 } //生成我军和子弹 function myPlane(index,e) { //用new操作符生成一个img标签 var plane = new Image(); plane.src = "img/plane.png"; plane.width = 60; plane.height = 36; plane.className = "plane"; plane.style.top = e.pageY - getoffset(box).top - plane.height/2 + "px"; plane.style.left = e.pageX - getoffset(box).left - plane.width/2 + "px"; box.appendChild(plane); //设置我军活动范围的最大和最小值,由于topmin是0 就不写了; var leftMin,leftMax,topMin,topMax; leftMin = -plane.width/2; leftMax = box.offsetWidth-plane.width/2; topMax = box.offsetHeight-plane.height; //移动事件控制飞机移动 document.onmousemove=function (e) { var left = e.pageX -getoffset(box).left - plane.width/2, top = e.pageY -getoffset(box).top - plane.height/2; //限定我军的活动范围(边界) left = Math.min(leftMax,left); left = Math.max(leftMin,left); top = Math.min(topMax,top); top = Math.max(0,top); plane.style.left = left + "px"; plane.style.top = top + "px"; }; //生成子弹 //把不同模式的子弹的速度存起来 var biuSpeed = ["100","200","300","20"]; //用定时器来批量生成子弹(因为需要很多的子弹) plane.timer = setInterval(function () { //用new操作符生成img标签,在对应的设置 var Biu = new Image(); Biu.src = "img/bullet.png"; Biu.width = 6; Biu.height = 22; Biu.className = "biu"; Biu.style.left = plane.offsetLeft +plane.width/2 - Biu.width/2+ "px"; Biu.style.top = plane.offsetTop + "px"; box.appendChild(Biu); //用requestAnimationFrame来循环调用函数mq,来达到改变top,从而让子弹向上运动 mq(); function mq() { //每次执行就改变top 向上运动 12px Biu.style.top = Biu.offsetTop - 12 + "px"; //判断子弹达到边界就删除子弹,避免页面有很多的子弹,切很多定时器,导致页面卡顿 if(Biu.offsetTop<=-Biu.height/2){ box.removeChild(Biu); }else{ Biu.parentNode&&requestAnimationFrame(mq); } } },biuSpeed[index]) return plane; } //生成敌军 function enemy(index,my_plane) { var arrEnemy = [300,200,100,50];//敌军生成的速度 box.timer = setInterval(function () { var enemy = new Image(); enemy.src = "img/enemy.png"; enemy.className = "enemy"; enemy.width = 23; enemy.height = 30; enemy.style.top = 0; enemy.style.left = Math.random()*box.offsetWidth - enemy.width/2 + "px"; box.appendChild(enemy); //控制敌机下落 var speed = Math.random()*5+2; eSpeed(); function eSpeed() { var aBiu = document.getElementsByClassName("biu"); enemy.style.top = enemy.offsetTop + speed + "px"; if(enemy.offsetTop >= box.offsetHeight-enemy.height){ box.removeChild(enemy); }else{ //敌机跟子弹检测 for(var i=0;i<aBiu.length;i++){ if(collide(enemy,aBiu[i])){ box.removeChild(aBiu[i]); createBoom(enemy,""); document.getElementsByClassName("score")[0].innerHTML = box.score+=10; box.removeChild(enemy); return; } } //敌机跟我方飞机检测 var my_plane1 = document.getElementsByClassName("plane")[0]; if(my_plane.parentNode&&collide(enemy,my_plane1)){ //飞机都炸了,还打个毛的子弹 clearInterval(my_plane1.timer); //飞机都没了,敌机也不用出来,任务完成 clearInterval(box.timer); //控制飞机移动的事件也不需要了 document.onmousemove=null; //生成碰撞敌机的boom图 createBoom(enemy,""); //生成我方战机boom图 createBoom(my_plane1,"2"); //移除敌机 box.removeChild(enemy); box.removeChild(my_plane1); setTimeout(GG,1000); //移除我军 return; } enemy.parentNode&&requestAnimationFrame(eSpeed); } } },arrEnemy[index]) } //碰撞函数 //obj1 是敌人 obj2 是我们的子弹 function collide(obj1,obj2){ //预存敌机和子弹的四个方向的值 var L1 = obj1.offsetLeft, T1 = obj1.offsetTop, R1 = L1 + obj1.offsetWidth, B1 = T1+obj1.offsetHeight; var L2 = obj2.offsetLeft, T2 = obj2.offsetTop, R2 = L2 + obj2.offsetWidth, B2 = T2+obj2.offsetHeight; return !(L1>R2 || T1>B2||R1<L2||B1<T2); /*if(L1>R2 || T1>B2||R1<L2||B1<T2){ //真的时候就是没有碰到 return true; }else{ //假 就是碰到了 return false; }*/ } //生成爆炸图片 function createBoom(obj,num) { var boom = new Image; boom.className = "boom"; boom.src = "img/boom"+num+".png"; boom.width = obj.width; boom.height = obj.height; boom.style.top =obj.offsetTop+"px"; boom.style.left = obj.offsetLeft+"px"; box.appendChild(boom); setTimeout(function () { box.removeChild(boom) },num*300+200) } //计分板 function score() { box.score = 0; var span = document.createElement("span"); span.className = "score"; span.innerHTML = box.score; box.appendChild(span); } //GG 游戏结束 function GG(){ box.innerHTML = ""; var Div = document.createElement("div"); Div.className = "GG"; var h1 = document.createElement("h1"); h1.innerHTML = "GG"; h1.className = "GGh1"; var Div1 = document.createElement("div"); Div1.className = "continue"; Div1.innerHTML = "不服,再战"; Div1.onclick=init; Div.appendChild(h1); Div.innerHTML += "<p>最终得分:"+box.score+"</p>"; Div.appendChild(Div1); box.appendChild(Div); } //获取对象到body的距离 function getoffset(obj) { var json = { left:0, top:0 }; while(obj!==document.body){ json.left += obj.offsetLeft; json.top += obj.offsetTop; obj = obj.offsetParent; } return json; } })() </script> </body> </html> |
「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」
上一篇:抽奖转盘代码怎么做
下一篇:js控制div的拖拽和拉伸
最新评论
免费领取学习资料和视频
收到了,你这个网址无法访问呀
评:react中调用 setState 之后发生了什么大佬,都联系不到你人, 那个友链能帮忙换一下不 原 共享博客 http:/...
评:react中调用 setState 之后发生了什么没咋研究,emmm
评:web前端绘制八卦图_超简单教程这个是太极阴阳鱼,不是八卦图。
评:web前端绘制八卦图_超简单教程文章不错
评:宝塔系统面板无法正常显示我采用该方法解决了才发布的教程
评:iview-admin点击路由报错vue-router.esm.js?8c4f:2007 Uncaught (in promise) NavigationDuplicated {_name: "Navi
11111
@12122
@超级管理员3