完整文件及代码可以在网盘下载,下载链接为: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. 本地如何将svn和git管理的代码做关联

    svn和git都是广为流传的代码版本管理工具,实际项目中往往会将两者结合使用,那么如何将本地的一份代码和两者做有机的关联呢! 前提假设:项目已经在开发阶段中,此时变更了svn代码库的地址:或者是组里来 ...

  2. css常用左右布局方案整理

     实际项目开发过程中我们经常会遇到页面div左右布局的需求:左侧 div 固定宽度,右侧 div 自适应宽度,填充满剩余页面,下面整理几种常用的方案  1 左侧 div 设置 float 属性为 le ...

  3. [android] 练习viewpagerindicator的使用(一)

    主要是学习一下使用这个库 activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...

  4. 解决javac无效的目标发行版1.8问题

    之前遇到了几次这个问题,解决了又忘记了,所以特别记录一下这个问题. 遇到这个问题,改pom文件不行,改project的sdk也不行,后面看到网上说真正的原因是maven的runner的jre的环境依然 ...

  5. flask之flask-sqlalchemy(一)

    一 安装flask-sqlalchemy pip install flask-sqlalchemy 二 导入相关模块和对象 from flask_sqlalchemy import SQLAlchem ...

  6. VS code 自定义快捷输入

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 位置 ctrl+shift+p 搜索: snippets 输入类型: 比如 ...

  7. vue中全局引入bootstrap.css

    1.首先在官网上下载bootstrap的压缩包(必须是官网上下载的) 将压缩包解压后引入在项目文件夹下面.如下图所示: 2.在main.js中引入 import './assets/bootstrap ...

  8. 【Python】安装配置Anaconda

    优点:解决Python 库依赖问题 清华安装镜像 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

  9. 13.git别名

    虽然别名不是很重要,但是你大概应该知道如何使用它们. Git 并不会在你输入部分命令时自动推断出你想要的命令. 如果不想每次都输入完整的 Git 命令,可以通过 git config 文件来轻松地为每 ...

  10. react-native-mapbox-gl

    mapbox是基于谷歌地图集成的地图插件,可以在很多平台使用,具体可以看mapbox官网.这里具体讲解“react-native-mapbox-gl”插件,是mapbox结合react native封 ...