完整文件及代码可以在网盘下载,下载链接为:https://pan.baidu.com/s/1hs7sBUs 密码: d83x

飞机大战css定义:

  1. <style>
  2. #container{  
  3. width:320px;
  4. height:568px;
  5. background: url(images/start.png);
  6. margin:20px auto;
  7. position: relative;
  8. overflow: hidden;  
  9. }
  10. #container input{
  11. width:120px;
  12. height: 38px;
  13. background: #ff6600;
  14. border:;
  15. color:#fff;
  16. font-size:14px;
  17. font-family: 微软雅黑;
  18. position: absolute;
  19. left: 100px;
  20. bottom:50px;
  21. }
  22. #enddiv{
  23. position: absolute;
  24. top: 210px;
  25. left: 75px;
  26. border: 1px solid gray;
  27. border-radius: 5px;
  28. text-align: center;
  29. background-color:#d7ddde;
  30. display: none;
  31. z-index:;
  32. }
  33. .plantext{
  34. width: 160px;
  35. height: 30px;
  36. line-height: 30px;
  37. font-size: 16px;
  38. font-weight: bold;
  39. }
  40. #enddiv div{
  41. width: 160px;
  42. height: 50px;
  43. }
  44. #enddiv div button{
  45. margin-top:10px ;
  46. width: 90px;
  47. height: 30px;
  48. border: 1px solid gray;
  49. border-radius: 30px;
  50. }
  51. #scoretext{
  52. margin:;
  53. font-family: arial;
  54. font-size:12px;
  55. font-weight: bold;
  56. color:#ff6600;
  57. position: absolute;
  58. left: 4px;
  59. top: 4px;
  60. z-index:;
  61. }
  62. </style>

飞机大战: HTML代码如下:

  1.   <div id="container">
  2. <p id="scoretext"></p>
  3. <div id="enddiv">
  4. <p class="plantext">游戏结束</p>
  5. <div><button onclick="restartGame()" id="restart">继续</button></div>
  6. </div>
  7. <input type="button" value="开始游戏" id="startBtn">
  8. </div>

飞机大战 : 调用js

  1. //中大型飞机射击次数未实现,gameover未实现
  2. function startGame(container){
  3.  
  4. var startBtn = document.getElementById("startBtn");
  5. startBtn.onclick = function(){
  6. container.style.background = "url(images/background_1.png)";
  7. this.style.display = "none";
  8. bgMove(container);
  9. var score = 0;
  10. var myplan = new MyPlan(120,480,container);
  11. var middleEnemy = new MiddleEnemy(container,myplan.bullets,myplan,score); //中型飞机对象实例
  12. var maxEnemy = new MaxEnemy(container,myplan.bullets,myplan,score);//大型飞机对象实例
  13. var enemy = new Enemy(container,myplan.bullets,myplan,middleEnemy,maxEnemy,score);
  14. enemy.init();
  15. }
  16. }
  17. var speed = 0;
  18. function bgMove(container){
  19. setInterval(function(){
  20. speed++;
  21. container.style.backgroundPosition = "0 " + speed + "px";
  22. if(speed > 568){
  23. speed = 0;
  24. }
  25. },15);
  26.  
  27. }
  28. function gameOver(){
  29. var enddiv = document.getElementById("enddiv");
  30. var restart = document.getElementById("restart");
  31.  
  32. enddiv.style.display = "block";
  33. restart.onclick = function(){
  34. location.reload();
  35. }
  36.  
  37. }
  38. var score = 0;
  39. function getScore(num){
  40. var scoretext = document.getElementById("scoretext");
  41. score += num;
  42. scoretext.innerHTML = score + "分";
  43.  
  44. }
  45. onload = function(){
  46. var container = document.getElementById("container");
  47.  
  48. startGame(container);
  49.  
  50. }

飞机大战: 我方飞机创建:

  1. Array.prototype.remove = function(value){
  2. for(var i = 0; i < this.length; i++){
  3. if(this[i] == value){
  4. this.splice(i,1);
  5. }
  6. }
  7. }
  8. function MyPlan(x , y , container){
  9. this.x = x;
  10. this.y = y;
  11. this.img = "images/my.gif";
  12. this.container = container;
  13. this.bullets = [];
  14. this.createTimer;
  15. this.init();
  16. }
  17. MyPlan.prototype = {
  18. init:function(){
  19. this.create();
  20. this.planMove();
  21. this.bullets.push(this.plan);
  22. var that = this;
  23. this.createTimer = setInterval(function(){
  24. that.createBullets();
  25. },200);
  26. this.createBullets();
  27. },
  28. planMove:function(){
  29. var that = this;
  30. this.container.onmousemove = function(e){
  31. e = e || event;
  32. var maxLeft = that.container.offsetWidth - that.plan.offsetWidth;
  33. var maxTop = that.container.offsetHeight - that.plan.offsetHeight;
  34. var planX = Math.max(Math.min(e.clientX - that.container.offsetLeft - that.plan.offsetWidth / 2,maxLeft),0);
  35. var planY = Math.max(Math.min(e.clientY - that.container.offsetTop - that.plan.offsetHeight / 2,maxTop),0);
  36. that.plan.style.left = planX + "px";
  37. that.plan.style.top = planY + "px";
  38. }
  39. },
  40. create:function(){
  41. this.plan = document.createElement("img");
  42. this.plan.src = this.img;
  43. this.plan.style.cssText = "position:absolute; top:" + this.y + "px; left:" + this.x + "px;";
  44. this.container.appendChild(this.plan);
  45. },
  46. createBullets:function(){
  47. this.bull = document.createElement("img");
  48. this.bull.src = "images/bullet1.png";
  49. var bullX = this.plan.offsetLeft + this.plan.offsetWidth / 2 - 6 / 2;
  50. var bullY = this.plan.offsetTop - 14;
  51.  
  52. this.bull.style.cssText = "position:absolute; top:" + bullY + "px; left:" + bullX + "px;";
  53. this.container.appendChild(this.bull);
  54. this.bullets.push(this.bull);
  55. var bull = this.bull; //不能用that = this 对象冒充 闭包问题
  56. var container = this.container;
  57. var bullets = this.bullets;
  58. this.bull.timer = setInterval(function(){
  59. bull.style.top = bull.offsetTop - 3 + "px";
  60. if(bull.offsetTop <= -14){
  61. bullets.remove(bull);
  62. container.removeChild(bull);
  63. clearInterval(bull.timer);
  64. }
  65. },8);
  66. }
  67. }

飞机大战: 敌方基本飞机创建:

  1. function Enemy(container,bullets,myplan,middleEnemy,maxEnemy,score){
  2. this.container = container;
  3. this.img = "images/enemy1_fly_1.png";
  4. this.createTime = 600; //创建敌机的间隔时间
  5. this.bullets = bullets;
  6. this.dieImg = "images/enemy1_fly_3.gif";
  7. this.width = 34; //敌机的宽度
  8. this.height = 24; //敌机的高度
  9. this.myplan = myplan;
  10. this.count = 1; //小型敌机创建个数
  11. this.dieCount = 1; //敌机消灭需子弹打击次数
  12. this.middleEnemy = middleEnemy;
  13. this.maxEnemy = maxEnemy;
  14. this.score = score;
  15.  
  16. }
  17. Enemy.prototype = {
  18. init:function(){
  19. var that = this;
  20. var middleEnemy = this.middleEnemy;
  21. var maxEnemy = this.maxEnemy;
  22. var count = 0;
  23. setInterval(function(){
  24. that.create();
  25. count++;
  26. if(count % 5 == 0){
  27. middleEnemy.create();
  28. }
  29. if(count % 30 == 0){
  30. maxEnemy.create();
  31. count = 1;
  32. }
  33. },this.createTime);
  34.  
  35. },
  36. create:function(){
  37. this.enemy = document.createElement("img");
  38. this.enemy.src = this.img;
  39. var enemyX = Math.random() * (this.container.offsetWidth - this.width);
  40. var enemyY = -1 * this.height;
  41. this.enemy.style.cssText = "position:absolute; left:" + enemyX + "px; top:" + enemyY + "px;";
  42. this.container.appendChild(this.enemy);
  43. var enemy = this.enemy;
  44. this.data_hitCount = 0;
  45. var container = this.container;
  46. var bullets = this.bullets;
  47. var dieImg = this.dieImg;
  48. var myplan = this.myplan;
  49. var plan = this.myplan.plan;
  50. var createBullets = this.myplan.createBullets;
  51. var dieCount = this.dieCount;
  52. var isremove = true; //是否可以移除敌机
  53. var score = this.score;
  54. var that = this;
  55. this.enemy.timer = setInterval(function(){
  56. enemy.style.top = enemy.offsetTop + 3 + "px";
  57.  
  58. for(var i = 0; i < bullets.length; i++){
  59.  
  60. if(bullets[i].offsetLeft + bullets[i].offsetWidth > enemy.offsetLeft && bullets[i].offsetLeft < enemy.offsetLeft + enemy.offsetWidth){
  61. if(bullets[i].offsetTop + bullets[i].offsetHeight > enemy.offsetTop && bullets[i].offsetTop < enemy.offsetTop + enemy.offsetHeight && isremove){
  62. if(i == 0){
  63. plan.src = "images/myover.gif";
  64. container.onmousemove = null;
  65. clearInterval(myplan.createTimer);
  66. gameOver();
  67. }
  68. else{
  69. container.removeChild(bullets[i]);
  70. bullets.remove(bullets[i]);
  71. that.data_hitCount++;
  72. if(that.data_hitCount == dieCount){
  73. isremove = false;
  74. enemy.src = dieImg;
  75. getScore(dieCount);
  76. setTimeout(function(){
  77. container.removeChild(enemy);
  78. },300);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. if(enemy.offsetTop >= container.offsetHeight){
  85. container.removeChild(enemy);
  86. clearInterval(enemy.timer);
  87. }
  88. },30);
  89. }
  90. }

飞机大战: 其他敌机创建:

  1. function MiddleEnemy(container,bullets,myplan,score){
  2.  
  3. Enemy.call(this,container,bullets,myplan,score);
  4. this.img = "images/enemy2_fly_1.png";
  5. this.dieImg = "images/enemy2_fly_3.gif";
  6. this.width = 46; //敌机的宽度
  7. this.height = 60; //敌机的高度
  8. this.dieCount = 5;
  9. }
  10.  
  11. MiddleEnemy.prototype = Enemy.prototype;
  12.  
  13. function MaxEnemy(container,bullets,myplan,score){
  14.  
  15. Enemy.call(this,container,bullets,myplan,score);
  16. this.img = "images/enemy3_fly_1.png";
  17. this.dieImg = "images/enemy3_fly_3.gif";
  18. this.width = 110; //敌机的宽度
  19. this.height = 164; //敌机的高度
  20. this.dieCount = 10;
  21. }
  22. MaxEnemy.prototype = Enemy.prototype;

效果图如图所示:

  

js 飞机大战的更多相关文章

  1. JS+html实现简单的飞机大战

    摘要:通过原生的js+html实现简单的飞机大战小游戏效果,如图所示: 实现代码如下: 1.自己的飞机实现 飞机html: <!DOCTYPE html> <html lang=&q ...

  2. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  3. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  4. 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)

    微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  5. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  6. JS 实现飞机大战

    这是JS版本的飞机大战,和C#版本的思路相同,就是语言上有差别,用来巩固知识.可以将代码直接引入到HTML中就可以看到效果 //编写背景对象 function Background(width,hei ...

  7. js实例--飞机大战

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  8. [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第1讲(实现思路与游戏界面的实现)

    整体效果展示: 一.实现思路 如图,这是我完成该项目的一个逻辑图,也是一个功能模块完成的顺序图. 游戏界面的完成 英雄飞机对象实现,在实现发射子弹方法过程中,又引出了子弹对象并实现.在此时,英雄飞机能 ...

  9. 用Javascript模拟微信飞机大战游戏

    最近微信的飞机大战非常流行,下载量非常高. 利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化] [此游戏中没有使用HTML5 任何浏览器都可以运行]. 效果图: 原理:利用 ...

随机推荐

  1. JS闭包、作用域链、垃圾回收、内存泄露相关知识小结

    补充: 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 闭包的三个特性: 1.函数嵌套函数 2.函数内部可以引用外部的参数和变量 3.参数和变 ...

  2. 一:ActiveMQ知识整理

    一:JMS概念 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消 ...

  3. Java API 之 动态代理

    一.代理模式 如图:由于某些原因我们希望对该实现类进行访问控制.功能增强等,那么加入一层代理层,用代理层来调用实现类是一个很好的方式来解决这个问题,我们可以在调用实现类功能前后进行校验或者加入一些功能 ...

  4. Linux下剪切拷贝命令

    Linux下剪切拷贝命令   命令格式: mv   source    dest   mv: 命令字   source: 源文件   dest: 目的地址   Linux下拷贝命令   命令格式:cp ...

  5. 第10章 布局样式相关-伸缩布局(Flexible Box)

    伸缩布局(一) CSS3引入了一种新的布局模式--Flexbox布局,即伸缩布局盒模型(Flexible Box),用来提供一个更加有效的方式制定.调整和分布一个容器里项目布局,即使它们的大小是未知或 ...

  6. python 2.7支持中文

    在代码的第一行加上#coding=utf-8 return render_template('index.html',message=u"小明小明")print u'你要打印的字符 ...

  7. class 命名规范

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 注: 文章摘自 penggelies07- 简书, super晴天 - C ...

  8. How to use Log4cplus

    Introduction Log4cplus is derived by the popular Log4j written in java.<br>This tutorial show ...

  9. C语言数组指针(指向数组的指针)

    注意:数组指针的定义,与指针数组的区别 转载:http://c.biancheng.net/cpp/biancheng/view/162.html 指向多维数组元素的指针变量 ① 指向数组元素的指针变 ...

  10. Linux-vi/vim编辑器常用命令与用法

    vi/vim是什么? Linux世界几乎所有的配置文件都是以纯文本形式存在的,而在所有的Linux发行版系统上都有vi编辑器,因此利用简单的文字编辑软件就能够轻松地修改系统的各种配置了,非常方便.vi ...