jQuery实现简单飞机大战

 更新时间:2020年7月5日 17:36  点击:1884

本文实例为大家分享了jQuery实现飞机大战的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>飞机大战</title>
 <style>
 * {
  margin: 0px
 }

 .container {
  height: 700px;
  width: 500px;
  background-color: black;
  margin: 5px auto; /*上下外边距5px,左右自动*/
  position: relative;
 }

 .plane {
  height: 80px;
  width: 80px;
  /*background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial|inherit;
  */
  background: url(images/plane.png) no-repeat center / 100% 100%;
  position: absolute;
  bottom: 10px;
  left: calc(50% - 40px);
 }

 .bullet {
  height: 10px;
  width: 5px;
  /*border-radius
  *每个半径的四个值的顺序是:左上角,右上角,右下角,左下角。
  *如果省略左下角,右上角是相同的。如果省略右下角,左上角是相同的。
  *如果省略右上角,左上角是相同的。
  */
  border-radius: 45% 45% 0 0;
  /*box-shadow: h-shadow v-shadow blur spread color inset;
  *h-shadow: 必需,水平阴影的位置
  *v-shadow: 必需,垂直阴影的位置
  *blur: 可选,模糊距离
  *spread: 可选,阴影的大小
  *color: 可选,阴影的颜色
  *inset: 可选,从外层的阴影(开始时)改变阴影内侧阴影
  */
  box-shadow: 0px 2px 10px orange;
  background: gold;
  position: absolute;
 }

 .enemy {
  height: 34.4px;
  width: 32.5px;
  /*background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial|inherit;
  */
  background: url(images/enemy.png) no-repeat center / 100% 100%;
  transform: rotate(180deg);
  position: absolute;
  overflow: hidden;
  top: 0px;
 }

 h2 {
  height: 40px;
  display: table;
  border-color: deepskyblue;
  border-radius: 5px;
  background-color: deepskyblue;
  text-align: center;
  padding: 5px;
  position: relative;
  float: right;
  right: 300px;
 }
 </style>
 <!--引入jQuery-->
 <script src="jquery-3.4.1.js"></script>
</head>
<body>
<h2>得分:
 <span id="score" style="color: white;">0</span>
</h2>
<div class="container">
 <div class="plane">

 </div>
</div>
<script type="text/javascript">
 // 入口函数
 $(function () {
 // 变量定义
 var bulletCreateInterval = 300; // 子弹发射时间间隔
 var bulletMoveInterval = 100; // 子弹飞行时间间隔
 var bulletSpeed = 10;  // 子弹飞行速度
 var enemyCreateInterval = 2000; // 敌机出生时间间隔
 var enemyMoveInterval = 100; // 敌机下降时间间隔
 var enemySpeed = 5;   // 敌机下降速度
 var bGamePlaying = false;  // 游戏状态
 var score = 0;
 var endTime = new Date();
 // 计算飞机位置
 var calcPosition = (left, top, maxLeft, maxTop, minLeft = 0, minTop = 0) => {
  left = left < minLeft &#63; minLeft : left > maxLeft &#63; maxLeft : left;
  top = top < minTop &#63; minTop : top > maxTop &#63; maxTop : top;
  return {left, top};
 }
 // 获取DOM对象的四个边界位置
 var getDomTRBL = (dom) => {
  var bounds = {};
  bounds.left = dom.offsetLeft;
  bounds.right = dom.offsetLeft + dom.offsetWidth;
  bounds.top = dom.offsetTop;
  bounds.bottom = dom.offsetTop + dom.offsetHeight;
  return bounds;
 }
 // 计算两个div是否相撞
 var calcHit = (div1, div2) => {
  var bounds1 = getDomTRBL(div1);
  var bounds2 = getDomTRBL(div2);
  if (bounds1.left >= bounds2.left && bounds1.left <= bounds2.right) {
  if (bounds1.top >= bounds2.top && bounds1.top <= bounds2.bottom) {
   return true;
  }
  else if (bounds1.bottom >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
   return true;
  }
  }
  else if (bounds1.right >= bounds2.left && bounds1.right <= bounds2.right) {
  if (bounds1.top >= bounds2.top && bounds1.top <= bounds2.bottom) {
   return true;
  }
  else if (bounds1.bottom >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
   return true;
  }
  }
  return false;
 }
 // 发射子弹
 var shoot = () => {
  // 控制发射时间间隔
  if (new Date() - endTime < bulletCreateInterval) {
  return false;
  }
  /*addClass() 方法向被选元素添加一个或多个类名。
  *该方法不会移除已存在的 class 属性,仅仅添加一个或多个类名到 class 属性。
  *提示:如需添加多个类,请使用空格分隔类名。
  */
  var planeLF = $(".plane").position().left;
  var planeTP = $(".plane").position().top;
  var bullet = $("<div></div>").addClass("bullet");
  $(".container").append(bullet);
  var bulletLF = planeLF + $(".plane").innerWidth() / 2 - bullet.innerWidth() / 2;
  var bulletTP = planeTP - $(".plane").innerHeight() / 2 + 20;
  bullet.css("left", bulletLF).css("top", bulletTP);

  endTime = new Date();
  return true;
 }
 // 键盘按下事件
 $(window).keydown(function (e) {
  if (e.keyCode == 13) { //enter 开始游戏
  bGamePlaying = true;
  console.log("game start!")
  }
  if (!bGamePlaying) return;
  var tp = $(".plane").position().top;
  var lf = $(".plane").position().left;
  switch (e.keyCode) {
  case 87:// w
   tp -= 10;
   break;
  case 83:// s
   tp += 10;
   break;
  case 65:// a
   lf -= 10;
   break;
  case 68:// d
   lf += 10;
   break;
  case 74:// j
   shoot(); // 发射子弹
   break;
  }
  var maxLeft = $(".container").innerWidth() - $(".plane").innerWidth();
  var maxTop = $(".container").innerHeight() - $(".plane").innerHeight();
  var position = calcPosition(lf, tp, maxLeft, maxTop);
  $(".plane").css("left", position.left).css("top", position.top);
 });
 // 鼠标移动事件
 var containerBounds = getDomTRBL($(".container")[0]);
 $(document).mousemove(function (e) {
  if (!bGamePlaying) return;
  var tp = e.pageY;
  var lf = e.pageX;
  if (tp >= containerBounds.top && tp <= containerBounds.bottom && lf >= containerBounds.left && lf <= containerBounds.right) {
  tp -= containerBounds.top;
  lf -= containerBounds.left;
  }
  else return;
  tp -= $(".plane").innerHeight() / 2;
  lf -= $(".plane").innerWidth() / 2;
  var maxLeft = $(".container").innerWidth() - $(".plane").innerWidth();
  var maxTop = $(".container").innerHeight() - $(".plane").innerHeight();
  var position = calcPosition(lf, tp, maxLeft, maxTop);
  $(".plane").css("left", position.left).css("top", position.top);
 });
 // 鼠标点击事件
 $(window).click(() => {
  if (!bGamePlaying) {
  bGamePlaying = true;
  }
  shoot();
 });
 // 为了便于对计时器进行操作,选择用一个计时器对选取到的所有元素进行操作
 // 这样可以大幅减少计时器的数目
 // 生成敌方战机计时器
 var enemyCreateTimer = setInterval(() => {
  var enemy = $("<div></div>").addClass("enemy").css("top", 0);
  $(".container").append(enemy);
  // round()方法可把一个数字舍入为最接近的整数(四舍五入)
  var left = Math.round(Math.random() * ($(".container").innerWidth() - $(".enemy").innerWidth()));
  enemy.css("left", left);
 }, enemyCreateInterval);
 // 让子弹飞计时器
 var bulletTimer = setInterval(() => {
  $(".bullet").each((index, element) => {
  var bullet = $(element);
  bullet.css("top", bullet.position().top - bulletSpeed);
  if (bullet.position().top < 0) {
   bullet.remove();
  }
  });
 }, bulletMoveInterval);
 // 敌机下落计时器
 var enemyTimer = setInterval(() => {
  $(".enemy").each((index, element) => {
  var enemy = $(element);
  enemy.css("top", enemy.position().top + enemySpeed);
  if (enemy.position().top > $(".container").innerHeight()) {
   enemy.remove();
  }
  });
 }, enemyMoveInterval);
 // 游戏主计时器
 var mainTimer = setInterval(() => {
  var plane = $(".plane").get(0);
  $(".enemy").each(function (index, enemy) {
  //判断玩家是否撞上了敌机
  if (calcHit(plane, enemy) || calcHit(enemy, plane)) {
   stopGame();
   return;
  }
  // 判断子弹是否撞上敌机
  $(".bullet").each((index, bullet) => {
   if (calcHit(enemy, bullet) || calcHit(bullet, enemy)) {
   enemy.remove();
   bullet.remove();
   score += 10;
   $("#score").text(score);
   }
  });
  });
 }, 50);
 // 停止游戏
 var stopGame = () => {
  bGamePlaying = false;
  clearInterval(enemyCreateTimer);
  clearInterval(enemyTimer);
  clearInterval(bulletTimer);
  clearInterval(mainTimer);
  alert("游戏结束!你的积分为" + score);
 }
 });
</script>
</body>
</html>


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • jquery实现加载更多"转圈圈"效果(示例代码)

    这篇文章主要介绍了jquery实现加载更多"转圈圈"效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-10
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • jQuery+jRange实现滑动选取数值范围特效

    有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
  • jQuery实现非常实用漂亮的select下拉菜单选择效果

    本文实例讲述了jQuery实现非常实用漂亮的select下拉菜单选择效果。分享给大家供大家参考,具体如下:先来看如下运行效果截图:在线演示地址如下:http://demo.jb51.net/js/2015/js-select-chose-style-menu-codes/具体代码如...2015-11-08
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • jQuery 2.0.3 源码分析之core(一)整体架构

    拜读一个开源框架,最想学到的就是设计的思想和实现的技巧。废话不多说,jquery这么多年了分析都写烂了,老早以前就拜读过,不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍我也不会照本宣科的翻译...2014-05-31
  • Jquery Ajax Error 调试错误的技巧

    JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息。在没给大家介绍正文之前先给分享Jquery中AJAX参...2015-11-24
  • jQuery页面加载初始化常用的三种方法

    当页面打开时我们需要执行一些操作,这个时候如果我们选择使用jquery的话,需要重写他的3中方法,自我感觉没什么区 别,看个人喜好了,第二种感觉比较简单明了: 第一种: 复制代码 代码如下: <script type="text/javas...2014-06-07
  • jquery中常用的SET和GET$(”#msg”).html循环介绍

    复制代码 代码如下: $(”#msg”).html(); //返回id为msg的元素节点的html内容。 $(”#msg”).html(”new content“); //将“new content” 作为html串写入id为msg的元素节点内容中,页面显示粗体的new content $(”...2013-10-13
  • jQuery实现广告显示和隐藏动画

    这篇文章主要为大家详细介绍了jQuery实现广告显示和隐藏动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-05
  • jquery获取div距离窗口和父级dv的距离示例

    jquery中jquery.offset().top / left用于获取div距离窗口的距离,jquery.position().top / left 用于获取距离父级div的距离(必须是绝对定位的div)。 (1)先介绍jquery.offset().top / left css: 复制代码 代码如下: *{ mar...2013-10-13
  • 使用JQuery实现Ctrl+Enter提交表单的方法

    有时候我们为了省事就操作键盘组合键去代替使用鼠标,我们今天就使用JQuery实现Ctrl+Enter提交表单。我们发帖时,在内容输入框中输入完内容后,可以点击“提交”按钮来发表内容。可是,如果你够“懒”,你可以不用动鼠标,只需按...2015-10-23
  • jQuery Mobile开发中日期插件Mobiscroll使用说明

    这篇文章主要介绍了jQuery Mobile开发中日期插件Mobiscroll使用说明,需要的朋友可以参考下...2016-03-03
  • jQuery实现切换页面过渡动画效果

    直接为大家介绍制作过程,希望大家可以喜欢。HTML结构该页面切换特效的HTML结构使用一个<main>元素来作为页面的包裹元素,div.cd-cover-layer用于制作页面切换时的遮罩层,div.cd-loading-bar是进行ajax加载时的loading进...2015-10-30
  • jQuery 1.9使用$.support替代$.browser的使用方法

    jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31
  • jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果

    本文实例讲述了jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果。分享给大家供大家参考,具体如下:这里演示jQuery实现鼠标移动到链接上,滑动展开/隐藏图片效果,鼠标放在“上一页”“下一页”上,立即浮现出所对应的图...2015-10-30
  • jQuery+PHP发布的内容进行无刷新分页(Fckeditor)

    这篇文章将使用jQuery,并结合PHP,将Fckeditor发布的内容进行分页,并且实现无刷新切换页面。 本文假设你是WEB开发人员,掌握了jQuery和PHP相关知识,并且熟知Fckeditor的配置和使用。...2015-10-23
  • jQuery 中的 DOM 操作

    在DOM操作中,常常需要动态创建HTML内容,使文档在浏览器里的呈现效果发生变化,并且达到各种各样的人机交互目的....2016-04-27
  • jQuery实现下拉菜单滑动效果

    这篇文章主要为大家详细介绍了jQuery实现下拉菜单滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
  • jQuery实现带玻璃流光质感的手风琴特效

    jQuery实现带玻璃流光质感的手风琴特效是一款基于jQuery+CSS3实现的带玻璃流光质感的手风琴特效,分享给大家,具体如下效果图:具体代码如下:html代码: <section class="strips"> <article class="strips__strip"> <di...2015-11-24