jQuery实现简单飞机大战


Posted in jQuery onJuly 05, 2020

本文实例为大家分享了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 ? minLeft : left > maxLeft ? maxLeft : left;
  top = top < minTop ? minTop : top > maxTop ? 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>

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jQuery实现选中行变色效果(实例讲解)
Jul 06 jQuery
基于jQuery实现手风琴菜单、层级菜单、置顶菜单、无缝滚动效果
Jul 20 jQuery
jquery如何实现点击空白处隐藏元素
Dec 05 jQuery
JQuery实现table中tr上移下移的示例(超简单)
Jan 08 jQuery
jquery动态添加带有样式的HTML标签元素方法
Feb 24 jQuery
vue+jquery+lodash实现滑动时顶部悬浮固定效果
Apr 28 jQuery
jQuery动态移除与增加onclick属性的方法详解
Jun 07 jQuery
jQuery实现合并表格单元格中相同行操作示例
Jan 28 jQuery
JavaScript自动生成 年月范围 选择功能完整示例【基于jQuery插件】
Sep 03 jQuery
如何解决jQuery 和其他JS库的冲突
Jun 22 jQuery
jQuery实现雪花飘落效果
Aug 02 jQuery
jQuery实现简单日历效果
Jul 05 #jQuery
jQuery实现飞机大战小游戏
Jul 05 #jQuery
jquery实现上传图片功能
Jun 29 #jQuery
jQuery实时统计输入框字数及限制
Jun 24 #jQuery
jQuery实现移动端下拉展现新的内容回弹动画
Jun 24 #jQuery
如何解决jQuery 和其他JS库的冲突
Jun 22 #jQuery
jQuery 移除事件的方法
Jun 20 #jQuery
You might like
经常用到的JavasScript事件的翻译
2007/04/09 Javascript
JAVASCRIPT IE 与 FF中兼容问题小结
2009/02/18 Javascript
简单实例处理url特殊符号&amp;处理(2种方法)
2013/04/02 Javascript
使用jquery插件qrcode生成二维码
2015/10/22 Javascript
js window对象属性和方法相关资料整理
2015/11/11 Javascript
javaScript 事件绑定、事件冒泡、事件捕获和事件执行顺序整理总结
2016/10/10 Javascript
微信小程序 require机制详解及实例代码
2016/12/14 Javascript
angularjs 实现带查找筛选功能的select下拉框实例
2017/01/11 Javascript
JS仿QQ好友列表展开、收缩功能(第二篇)
2017/07/07 Javascript
使用vue-cli3 创建vue项目并配置VS Code 自动代码格式化 vue语法高亮问题
2019/05/14 Javascript
nodejs语言实现验证码生成功能的示例代码
2019/10/13 NodeJs
Vue跨域请求问题解决方案过程解析
2020/08/07 Javascript
[06:07]刀塔密之二:攻之吾命受之吾幸
2014/07/03 DOTA
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
[02:10]DOTA2 TI10勇士令状玩法及不朽Ⅰ展示:焕新世界,如你所期
2020/05/29 DOTA
python中__call__方法示例分析
2014/10/11 Python
python发送邮件功能实现代码
2016/07/15 Python
python实现生命游戏的示例代码(Game of Life)
2018/01/24 Python
对python3 一组数值的归一化处理方法详解
2018/07/11 Python
Python3实现的回文数判断及罗马数字转整数算法示例
2019/03/27 Python
pycharm内无法import已安装的模块问题解决
2020/02/12 Python
草莓网化妆品加拿大网站:Strawberrynet Canada
2016/09/20 全球购物
澳大利亚二手奢侈品网站:Modsie
2019/09/23 全球购物
农救科工作职责
2013/11/27 职场文书
民族团结先进集体事迹材料
2014/05/22 职场文书
幼儿园保育员责任书
2014/07/22 职场文书
领导班子“四风问题”“整改方案
2014/10/02 职场文书
运动会搞笑广播稿
2014/10/14 职场文书
毕业设计答辩开场白
2015/05/29 职场文书
爱国主题班会教案
2015/08/14 职场文书
Python破解极验滑动验证码详细步骤
2021/05/21 Python
python批量创建变量并赋值操作
2021/06/03 Python
mysql联合索引的使用规则
2021/06/23 MySQL
纯CSS如何禁止用户复制网页的内容
2021/11/01 HTML / CSS
Nginx动静分离配置实现与说明
2022/04/07 Servers
Java对文件的读写操作方法
2022/04/29 Java/Android