jQuery實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
游戲規(guī)則:
WSAD鍵控制大飛機(jī)移動方向,按下J鍵可發(fā)射子彈消滅敵機(jī),每殲滅一架敵機(jī)可得10分;
與敵機(jī)相撞或者有遺漏敵機(jī)沒有殲滅,則游戲結(jié)束
游戲顯示如下圖:
css部分:
<style> .container { width: 800px; height: 500px; margin: 10vh auto; background: #000; position: relative; overflow: hidden; } .plane { color: #fff; width: 70px; height: 70px; position: absolute; bottom: 10px; left: calc(50% - 30px); background: url(img/戰(zhàn)斗機(jī).png) no-repeat; background-size: 70px 70px; } .bullet { width: 25px; height: 30px; background: url(img/子彈.png) no-repeat; background-size: 100% 100%; position: absolute; } .enemy { width: 40px; height: 40px; background: url(img/戰(zhàn)斗機(jī).png) no-repeat; transform: rotate(180deg); background-size: 100% 100%; position: absolute; top: 0; } .again { width: 220px; height: 160px; border: 1px solid #ccc; box-shadow: 5px 5px #ccc; background: rgb(252, 187, 187); text-align: center; line-height: 80px; color: #fff; font-size: 20px; position: absolute; top: calc(50% - 80px); left: calc(50% - 110px); margin: 0 auto; cursor: pointer; } i { font-style: normal; } .sorce { height: 30px; font-size: 20px; text-align: center; font-weight: bold; margin: 0 auto; position: absolute; top: 65px; left: calc(50% - 100px); color: #fff; z-index: 100; background: linear-gradient(to right, rgb(255, 163, 220), rgb(243, 176, 213)); -webkit-background-clip: text; color: transparent; } </style>
html部分:
<div class="sorce"> 擊?。?lt;i class="ok">0</i> 得分:<i class="get">0</i> </div> <div class="container"> <div class="plane"> </div> </div>
js部分:
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(function () { let maxtop = $(".container").innerHeight() - $(".plane").innerHeight() let maxleft = $(".container").innerWidth() - $(".plane").innerWidth() let ok = 0, get = 0; $(window).keydown(function ({ keyCode }) { let { top: t, left: l } = $(".plane").position() switch (keyCode) { case 87: t -= 10 break; case 83: t += 10 break; case 65: l -= 10 break; case 68: l += 10 break; case 74: shoot(); break; default: break; } if (t < 0) t = 0 if (t > maxtop) t = maxtop if (l < 0) l = 0 if (l > maxleft) l = maxleft $(".plane").css("top", t).css("left", l) }) let endTime = new Date() function shoot() { if (new Date() - endTime < 500) return let bullet = $('<div/>').addClass("bullet") $('.container').append(bullet) bullet.css('top', $('.plane').position().top - 30) bullet.css('left', $('.plane').position().left + $('.plane').innerWidth() / 2 - bullet .innerWidth() / 2) endTime = new Date() } let timer1 = setInterval(() => { $('.bullet').each(function () { let bullet = $(this) let { top } = bullet.position() if (top < 0) bullet.remove() else bullet.css('top', bullet.position().top - 10) }) }, 100); let timer2 = setInterval(() => { $('.enemy').each(function () { let enemy = this if (calc(enemy, $('.plane').get(0)) || calc($('.plane').get(0), enemy)) { let again = $('<div/>').html(`GAME OVER<br/>點(diǎn)擊重新開始`).addClass( 'again') $('.container').append(again) clearInterval(timer1) clearInterval(timer2) clearInterval(timer3) clearInterval(timer4) } $('.again').click(function () { location.reload() }) $('.bullet').each(function () { let bullet = this if (calc(enemy, bullet) || calc(bullet, enemy)) { bullet.remove() enemy.remove() get += 10 ok++ $('.ok').html(ok) $('.get').html(get) } }) }) }, 50) let timer3 = setInterval(() => { let enemy = $('<div/>').addClass("enemy") $('.container').append(enemy) enemy.css('left', Math.round(Math.random() * ($('.container').innerWidth() - enemy .innerWidth()))) }, 2000) let timer4 = setInterval(() => { $('.enemy').each(function () { let enemy = $(this) let { top } = enemy.position() if (top > $('.container').innerHeight() - enemy.innerHeight()) { clearInterval(timer1) clearInterval(timer2) clearInterval(timer3) clearInterval(timer4) let again = $('<div/>').html(`GAME OVER<br/>點(diǎn)擊重新開戰(zhàn)`).addClass( 'again') $('.container').append(again) $('.again').click(function () { location.reload() }) } else enemy.css('top', enemy.position().top + 10) }) }, 200); function getLTRB(node) { return { l: node.offsetLeft, t: node.offsetTop, r: node.offsetLeft + node.offsetWidth, b: node.offsetTop + node.offsetHeight } } //獲取上下左右位置 function calc(a, b) { a = getLTRB(a) b = getLTRB(b) //判斷飛機(jī)與敵機(jī)是否相撞 if (a.l > a.l && b.l < a.r && b.t > a.t && b.t < a.b) return true else if (b.l > a.l && b.l < a.r && b.b > a.t && b.b < a.b) return true else if (b.r > a.l && b.r < a.r && b.b > a.t && b.b < a.b) return true else if (b.r > a.l && b.r < a.r && b.t > a.t && b.t < a.b) return true else return false } }) </script>
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery用get實(shí)現(xiàn)ajax在ie里面刷新不進(jìn)入后臺解決方法
jquery用get實(shí)現(xiàn)ajax在ie里面刷新不進(jìn)入后臺的情況想必大家都有遇到過吧,下面與大家分享下具體的解決方法,希望對大家解決問題有所幫助2013-08-08jQuery實(shí)現(xiàn)html table行Tr的復(fù)制、刪除、計(jì)算功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)html table行Tr的復(fù)制、刪除、計(jì)算功能,涉及jQuery針對table表格的常見復(fù)制、添加、刪除與計(jì)算行數(shù)等簡單操作技巧,需要的朋友可以參考下2017-07-07jQuery實(shí)現(xiàn)表格的數(shù)據(jù)拖拽
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)表格的數(shù)據(jù)拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02jQuery中[attribute=value]選擇器用法實(shí)例
這篇文章主要介紹了jQuery中[attribute=value]選擇器用法,實(shí)例分析了[attribute=value]選擇器的功能、定義及給定屬性及屬性值元素的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2014-12-12淺談MVC+EF easyui dataGrid 動態(tài)加載分頁表格
下面小編就為大家?guī)硪黄獪\談MVC+EF easyui dataGrid 動態(tài)加載分頁表格。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11