jQuery实现打飞机游戏
玩法介绍:不同样式的飞机出来其它飞机会暂停飞行且处于无敌状态,子弹对它无效,你操纵的飞机不能碰到任何飞机,发出的子弹可以攻击正在飞行的飞机,每击落一架飞机会记录分数,你操纵的飞机碰到其它飞机即为游戏结束
效果图演示(gif图看着可能会有些卡顿,可以复制下面代码在浏览器运行查看):
完整代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> * { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; overflow: hidden; background: url(./img/bj.jpg) repeat 0 0px; } .mine { width: 110px; height: 90px; background: url(img/P.png) no-repeat 0 -102px; position: absolute; cursor: pointer; } .zi { width: 10px; height: 10px; background-color: red; position: absolute; border-radius: 50%; } .lan { width: 118px; height: 100px; position: absolute; background: url(img/P.png) no-repeat -115px -1px; } .hong{ width: 107px; height: 80px; position: absolute; background: url(img/P.png) no-repeat -1px -1px; } .cheng{ width: 122px; height: 87px; position: absolute; background: url(img/P.png) no-repeat -242px -1px; } .chen{ width: 122px; height: 80px; position: absolute; background: url(img/P.png) no-repeat -387px -1px; } p{ position: absolute; color: orange; font-size: 30px; z-index: 999; left:20; top:40; } </style> </head> <body> <ul></ul> <p>击落飞机:0</p> </body> <script src="//cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> let a = null, //创建mine定时器 b = null, //创建琪它飞机定时器 qi = null, //其它飞机移动定时器 tim = null, //子弹移动定时器 n = 'lan', //存取随机飞机名称 op = 0 //记录分数 const feiji = ['lan','hong','cheng','chen'] $('<div>').addClass('mine').css({ left: ($(document).innerWidth() - 110) / 2, top: ($(document).innerHeight() - 90) }).appendTo('ul') //添加mine飞机 .on('mousedown', function(e) { //实现拖拽 $(document).on('mousemove', e => { //鼠标移动 n = feiji[Math.floor(Math.random()*feiji.length)] let left = e.clientX - $(this).width() / 2 + 2 let top = e.clientY - $(this).height() / 2 - 10 left = loob(left, 0, $(document).innerWidth() - $(this).width()) top = loob(top, 0, $(document).innerHeight() - $(this).height()) $(this).css({ left, top }) e.preventDefault() //清楚默认事件 }) $(document).on('mouseup', () => { //鼠标抬起 $(document).off('mousemove') }) }) function loob(dir, min, max) { //限制出界 dir < min ? dir = min : dir > max ? dir = max : '' return dir } const $mine = $('.mine') let num = 0 let kkk = 0 a = setInterval(() => { //创建子弹 clearInterval(tim) $('<div>').addClass('zi').css({ left: $mine.offset().left + $mine.width() / 2 - 5, top: $mine.offset().top - 5 }).appendTo('ul') tim = setInterval(() => { //子弹移动 num += 10 //背景移动速度 $('.zi').css({ top: '-=10' }).each(function() { let k = $(this) $(`.${n}`).each(function(){ if(k.offset().left+10>$(this).offset().left&&k.offset().left<$(this).offset().left+10+$(this).width()&&k.offset().top+10>$(this).offset().top&&k.offset().top<$(this).offset().top+$(this).height()){//判断子弹是否击中其它飞机 kkk++ if(kkk==10){ $(this).remove() kkk=0 op++ $('p').html(`击落飞机:${op}`) } k.remove() } }) if ($(this).offset().top <= -10) { //超出屏幕删除子弹 $(this).remove() } }) $('body').css({ background: `url(./img/bj.jpg) repeat 0 ${num}px` }) }, 1000 / 60) }, 100) b = setInterval(() => { //随机创建飞机 clearInterval(qi) xuan(n)//随机出现飞机 }, 600) function xuan(obj){ $('<div>').addClass(obj).css({ left: Math.floor(Math.random() * ($(document).innerWidth() - 300)) }).appendTo('ul') qi = setInterval(() => {//其它飞机移动 $(`.${n}`).each(function(){ if($('.mine').offset().left+$('.mine').width()>$(this).offset().left&&$('.mine').offset().left<$(this).offset().left+$(this).width()&&$('.mine').offset().top+$('.mine').height()>$(this).offset().top&&$('.mine').offset().top<$(this).offset().top+$(this).height()){//判断mine飞机是否触碰其它飞机 clearInterval(a) clearInterval(b) clearInterval(tim) clearInterval(qi) $('body').css({ background:'white' }).html(`游戏结束!你一共击落了${op}架飞机`) return } }) $(`.${obj}`).css({ top: '+=4' }).each(function() { $(this)[0].num = 0 if($(this).offset().top>$(window).innerHeight()){ $(this).remove() } }) }, 1000 / 60) } </script> </html>
如果要复制代码运行的话,可以私聊我,我把用到的图片分享给你们