经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
js+canvas实现飞机大战
来源:jb51  时间:2022/5/9 9:44:04  对本文有异议

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

首先我们绘制一个canvas区域,确实其宽高为480px*852px;水平居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. ?
  4. <head>
  5. ? ? <meta charset="UTF-8">
  6. ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. ? ? <title>Document</title>
  9. ? ? <style>
  10. ? ? ? ? canvas {
  11. ? ? ? ? ? ? position: absolute;
  12. ? ? ? ? ? ? left: 0;
  13. ? ? ? ? ? ? right: 0;
  14. ? ? ? ? ? ? margin: auto;
  15. ? ? ? ? ? ? border: #000 solid 1px;
  16. ? ? ? ? }
  17. ? ? </style>
  18. </head>
  19. ?
  20. <body>
  21. ? ? <canvas width="480" height="852"></canvas>
  22. </body>
  23. ?
  24. </html>

然后我们再用js查询相应的canvas,再确定画笔cex;然后定一个全局变量state代表游戏状态。

其中state=0表示游戏初始化,state=1代表我方飞机入场,state=2代表战斗过程,state=3代表暂停过程,state=4代表游戏结束过程。

  1. var canvas = document.getElementsByTagName("canvas")[0];
  2. var cex = canvas.getContext('2d');
  3. var state = 0; //状态

再确实背景图片,根据图片大小确实背景的的宽高等数据,再编写相应的函数,最终使用函数声明出一个背景图片对象出来。

  1. //背景图片
  2. var bg = new Image()
  3. ? ? ? ? bg.src = 'img/background.png'
  4. ? ? ? ? // 背景数据对象
  5. ? ? ? ? var bakgobj = {
  6. ? ? ? ? ? ? img: bg,
  7. ? ? ? ? ? ? width: 480,
  8. ? ? ? ? ? ? height: 852,
  9. ? ? ? ? }
  10. ? ? ? ? // 背景函数
  11. ? ? ? ? function By(params) {
  12. ? ? ? ? ? ? this.width = params.width;
  13. ? ? ? ? ? ? this.height = params.height;
  14. ? ? ? ? ? ? this.img = params.img;
  15. ? ? ? ? ? ? this.x = 0;
  16. ? ? ? ? ? ? this.y = 0;
  17. ? ? ? ? ? ? this.y1 = -this.height;
  18. ? ? ? ? ? ? // 背景绘制
  19. ? ? ? ? ? ? this.paint = function () {
  20. ? ? ? ? ? ? ? ? cex.drawImage(this.img, this.x, this.y);
  21. ? ? ? ? ? ? ? ? cex.drawImage(this.img, this.x, this.y1)
  22. ? ? ? ? ? ? }
  23. ? ? ? ? ? ? // 背景运动
  24. ? ? ? ? ? ? this.sprot = function () {
  25. ? ? ? ? ? ? ? ? this.y += 3;
  26. ? ? ? ? ? ? ? ? this.y1 += 3;
  27. ? ? ? ? ? ? ? ? if (this.y >= this.height) {
  28. ? ? ? ? ? ? ? ? ? ? this.y = -this.height;
  29. ? ? ? ? ? ? ? ? }
  30. ? ? ? ? ? ? ? ? if (this.y1 >= this.height) {
  31. ? ? ? ? ? ? ? ? ? ? this.y1 = -this.height;
  32. ? ? ? ? ? ? ? ? }
  33. ? ? ? ? ? ? }
  34. ? ? ? ? }
  35. // 背景对象
  36. var bakg = new By(bakgobj);

声明出相应的logo图片与暂停图片

  1. // logo图片
  2. var logo = new Image();
  3. ? ? ? ? logo.src = 'img/start.png'
  4. // 暂停图片
  5. var pause = new Image();
  6. ? ? ? ? pause.src = 'img/game_pause_nor.png';

使用相同的思路声明入场时的飞机对象

  1. // 入场阶段
  2. var gamearr = ['img/game_loading1.png', 'img/game_loading2.png', 'img/game_loading3.png',
  3. ? ? ? ? ? ? 'img/game_loading4.png'
  4. ? ? ? ? ];
  5. ? ? ? ? // 入场图片对象
  6. ? ? ? ? var gameArr = [];
  7. ? ? ? ? for (var i = 0; i < gamearr.length; i++) {
  8. ? ? ? ? ? ? gameArr[i] = new Image();
  9. ? ? ? ? ? ? gameArr[i].src = gamearr[i];
  10. ? ? ? ? }
  11. ? ? ? ? // 入场飞机数据
  12. ? ? ? ? var gameobj = {
  13. ? ? ? ? ? ? img: gameArr,
  14. ? ? ? ? ? ? width: 186,
  15. ? ? ? ? ? ? height: 38,
  16. ? ? ? ? ? ? length: gameArr.length
  17. ? ? ? ? }
  18. ? ? ? ? // 入场飞机函数
  19. ? ? ? ? function Game(params) {
  20. ? ? ? ? ? ? this.imgs = params.img;
  21. ? ? ? ? ? ? this.width = params.width;
  22. ? ? ? ? ? ? this.height = params.height;
  23. ? ? ? ? ? ? this.length = params.length;
  24. ? ? ? ? ? ? this.index = 0; //入场顺序图片下标
  25. ? ? ? ? ? ? this.thim = 0;
  26. ? ? ? ? ? ? this.paint = function () {
  27. ? ? ? ? ? ? ? ? cex.drawImage(this.imgs[this.index], 0, bakg.height - this.height);
  28. ? ? ? ? ? ? }
  29. ? ? ? ? ? ? this.sprot = function () {
  30. ? ? ? ? ? ? ? ? this.thim++;
  31. ? ? ? ? ? ? ? ? if (this.thim % 3 == 0) {
  32. ? ? ? ? ? ? ? ? ? ? this.index++;
  33. ? ? ? ? ? ? ? ? }
  34. ? ? ? ? ? ? ? ? if (this.index == this.length) {
  35. ? ? ? ? ? ? ? ? ? ? state = 2;
  36. ? ? ? ? ? ? ? ? }
  37. ?
  38. ? ? ? ? ? ? }
  39. ? ? ? ? }
  40. ? ? ? ? // 入场飞机对象
  41. ? ? ? ? var game = new Game(gameobj);

再声明飞机对象

  1. // 飞机图片
  2. var heroarr = ['img/hero1.png', 'img/hero2.png']
  3. // 飞机图片对象
  4. var heroArr = [];
  5. ? ? ? ? for (var i = 0; i < heroarr.length; i++) {
  6. ? ? ? ? ? ? heroArr[i] = new Image();
  7. ? ? ? ? ? ? heroArr[i].src = heroarr[i];
  8. ? ? ? ? }
  9. ? ? ? ? // 飞机数据
  10. ? ? ? ? var heroobj = {
  11. ? ? ? ? ? ? img: heroArr,
  12. ? ? ? ? ? ? width: 99,
  13. ? ? ? ? ? ? height: 124,
  14. ? ? ? ? ? ? length: heroArr.length,
  15. ? ? ? ? ? ? full:4, //生命
  16. ? ? ? ? ? ? invinc_:50, ? ? //无敌时间
  17. ? ? ? ? ? ? maga:500, ? //子弹数量
  18. ? ? ? ? }
  19. ? ? ? ? // 飞机函数
  20. ? ? ? ? function Hero(params) {
  21. ? ? ? ? ? ? this.imgs = params.img;
  22. ? ? ? ? ? ? this.width = params.width;
  23. ? ? ? ? ? ? this.height = params.height;
  24. ? ? ? ? ? ? this.length = params.length;
  25. ? ? ? ? ? ? this.x = (bakgobj.width - this.width) / 2;
  26. ? ? ? ? ? ? this.y = bakgobj.height - this.height;
  27. ? ? ? ? ? ? this.index = 0;
  28. ? ? ? ? ? ? this.maga=params.maga;
  29. ? ? ? ? ? ? this.full=params.full; ? //飞机生命
  30. ? ? ? ? ? ? this.invinc=0; ?//初始无敌时间
  31. ? ? ? ? ? ? this.invinc_=params.invinc_;
  32. ? ? ? ? ? ? this.frac=0; ? ?//飞机分数;
  33. ? ? ? ? ? ? this.cou = 0; //控制子弹发射速度
  34. ? ? ? ? ? ? this.ene = 0; //控制敌机出现频率
  35. ? ? ? ? ? ? this.paint = function () {
  36. ? ? ? ? ? ? ? ? if((this.invinc>0 && this.invinc%2==0)||this.invinc<=0){
  37. ? ? ? ? ? ? ? ? ? ? cex.drawImage(this.imgs[this.index], this.x, this.y)
  38. ? ? ? ? ? ? ? ? }
  39. ? ? ? ? ? ? ? ??
  40. ? ? ? ? ? ? }
  41. ? ? ? ? ? ? this.sprot = function () {
  42. ? ? ? ? ? ? ? ? this.index++;
  43. ? ? ? ? ? ? ? ? if (this.index == 2) {
  44. ? ? ? ? ? ? ? ? ? ? this.index = 0;
  45. ? ? ? ? ? ? ? ? }
  46. ? ? ? ? ? ? }
  47. ? ? ? ? ? ? // 增加射出子弹
  48. ? ? ? ? ? ? this.bullk = function () {
  49. ? ? ? ? ? ? ? ? this.cou++;
  50. ? ? ? ? ? ? ? ? // 子弹发射速度
  51. ? ? ? ? ? ? ? ? // if (this.cou % 5 == 0) {
  52. ? ? ? ? ? ? ? ? ? ? bullsec.push(new Bull(bullobj))
  53. ? ? ? ? ? ? ? ? // }
  54. ? ? ? ? ? ? }
  55. ? ? ? ? ? ? // 增加敌机
  56. ? ? ? ? ? ? this.enemysos = function () {
  57. ? ? ? ? ? ? ? ? if (this.ene % 20 == 0) {
  58. ? ? ? ? ? ? ? ? ? ? var rand = Math.random();
  59. ? ? ? ? ? ? ? ? ? ? if (rand < 0.5) {
  60. ? ? ? ? ? ? ? ? ? ? ? ? enemy.push(new Enemy(enemy1obj))
  61. ? ? ? ? ? ? ? ? ? ? } else if (rand < 0.8) {
  62. ? ? ? ? ? ? ? ? ? ? ? ? enemy.push(new Enemy(enemy2obj))
  63. ? ? ? ? ? ? ? ? ? ? } else {
  64. ? ? ? ? ? ? ? ? ? ? ? ? enemy.push(new Enemy3(enemy3obj))
  65. ? ? ? ? ? ? ? ? ? ? }
  66. ? ? ? ? ? ? ? ? }
  67. ? ? ? ? ? ? ? ? this.ene++;
  68. ? ? ? ? ? ? }
  69. ? ? ? ? }
  70. var hero = new Hero(heroobj);

子弹对象与数组

  1. // 子弹图像
  2. var bullimg = new Image();
  3. bullimg.src = 'img/bullet1.png';
  4. // 子弹数据
  5. var bullobj = {
  6. ? ? ? ? ? ? img: bullimg,
  7. ? ? ? ? ? ? width: 9,
  8. ? ? ? ? ? ? height: 21,
  9. ? ? ? ? }
  10. ? ? ? ? // 子弹函数
  11. ? ? ? ? function Bull(params) {
  12. ? ? ? ? ? ? this.img = params.img;
  13. ? ? ? ? ? ? this.width = params.width;
  14. ? ? ? ? ? ? this.height = params.height;
  15. ? ? ? ? ? ? this.x = hero.x + hero.width / 2 - this.width / 2;
  16. ? ? ? ? ? ? this.y = hero.y - this.height;
  17. ? ? ? ? ? ? this.paint = function () {
  18. ? ? ? ? ? ? ? ? cex.drawImage(this.img, this.x, this.y)
  19. ? ? ? ? ? ? }
  20. ? ? ? ? ? ? this.sprot = function () {
  21. ? ? ? ? ? ? ? ? this.y -= 20; //子弹飞行速度
  22. ? ? ? ? ? ? }
  23. ? ? ? ? }
  24. ? ? ? ? var bull = new Bull(bullobj);
  25. ? ? ? ? // 界面上的子弹数组;
  26. ? ? ? ? var bullsec = [];
  27. ?
  28. ? ? ? ? function bull_paint() {
  29. ? ? ? ? ? ? for (var i = 0; i < bullsec.length; i++) {
  30. ? ? ? ? ? ? ? ? bullsec[i].paint();
  31. ? ? ? ? ? ? }
  32. ? ? ? ? }
  33. ?
  34. ? ? ? ? function bull_sprot() {
  35. ? ? ? ? ? ? for (var i = 0; i < bullsec.length; i++) {
  36. ? ? ? ? ? ? ? ? bullsec[i].sprot();
  37. ? ? ? ? ? ? }
  38. ? ? ? ? }

敌机小、中、大对象与数组、方法

  1. // 敌机--小
  2. var enemy1arr = ['img/enemy1.png', 'img/enemy1_down1.png', 'img/enemy1_down2.png', 'img/enemy1_down3.png',
  3. ? ? ? ? ? ? 'img/enemy1_down4.png'
  4. ? ? ? ? ]
  5. ? ? ? ? var enemy1Arr = [];
  6. ? ? ? ? for (var i = 0; i < enemy1arr.length; i++) {
  7. ? ? ? ? ? ? enemy1Arr[i] = new Image();
  8. ? ? ? ? ? ? enemy1Arr[i].src = enemy1arr[i];
  9. ? ? ? ? }
  10. ? ? ? ? //敌机—-小 ? 数据
  11. ? ? ? ? var enemy1obj = {
  12. ? ? ? ? ? ? img: enemy1Arr,
  13. ? ? ? ? ? ? width: 57,
  14. ? ? ? ? ? ? height: 51,
  15. ? ? ? ? ? ? length: enemy1Arr.length,
  16. ? ? ? ? ? ? frac:3,
  17. ? ? ? ? ? ? full:1,
  18. ? ? ? ? }
  19. ?
  20. ? ? ? ? // 敌机--中
  21. ? ? ? ? var enemy2arr = ['img/enemy2.png', 'img/enemy2_down1.png', 'img/enemy2_down2.png', 'img/enemy2_down3.png',
  22. ? ? ? ? ? ? 'img/enemy2_down4.png'
  23. ? ? ? ? ]
  24. ? ? ? ? var enemy2Arr = [];
  25. ? ? ? ? for (var i = 0; i < enemy2arr.length; i++) {
  26. ? ? ? ? ? ? enemy2Arr[i] = new Image();
  27. ? ? ? ? ? ? enemy2Arr[i].src = enemy2arr[i];
  28. ? ? ? ? }
  29. ? ? ? ? //敌机--中 ? 数据
  30. ? ? ? ? var enemy2obj = {
  31. ? ? ? ? ? ? img: enemy2Arr,
  32. ? ? ? ? ? ? width: 69,
  33. ? ? ? ? ? ? height: 95,
  34. ? ? ? ? ? ? length: enemy2Arr.length,
  35. ? ? ? ? ? ? frac:5,
  36. ? ? ? ? ? ? full:2,
  37. ? ? ? ? }
  38. ?
  39. ? ? ? ? ?// 敌机--小、中 函数
  40. ? ? ? ? ?function Enemy(params) {
  41. ? ? ? ? ? ? this.imgs = params.img;
  42. ? ? ? ? ? ? this.width = params.width;
  43. ? ? ? ? ? ? this.height = params.height;
  44. ? ? ? ? ? ? this.length = params.length;
  45. ? ? ? ? ? ? this.frac=params.frac;
  46. ? ? ? ? ? ? this.index = 0;
  47. ? ? ? ? ? ? this.buff=Math.random<0.05?true:false; ? //随机带buff
  48. ? ? ? ? ? ? this.ext=false;//敌机是否被击落
  49. ? ? ? ? ? ? this.full = params.full; //敌机生命值
  50. ? ? ? ? ? ? this.x = Math.random() * (bakg.width - this.width);
  51. ? ? ? ? ? ? this.y = -this.height;
  52. ? ? ? ? ? ? this.paint = function () {
  53. ? ? ? ? ? ? ? ? cex.drawImage(this.imgs[this.index], this.x, this.y);
  54. ? ? ? ? ? ? }
  55. ? ? ? ? ? ? this.sprot = function () {
  56. ? ? ? ? ? ? ? ? this.y += 5;
  57. ? ? ? ? ? ? ? ? if (this.full <= 0) {
  58. ? ? ? ? ? ? ? ? ? ? this.index++;
  59. ? ? ? ? ? ? ? ? }
  60. ? ? ? ? ? ? }
  61. ? ? ? ? }
  62. ?
  63. ? ? ? ? // 敌机--大 ?
  64. ? ? ? ? var enemy3arr = ['img/enemy3_n1.png', 'img/enemy3_n2.png', 'img/enemy3_hit.png', 'img/enemy3_down1.png',
  65. ? ? ? ? ? ? 'img/enemy3_down2.png', 'img/enemy3_down3.png', 'img/enemy3_down4.png', 'img/enemy3_down5.png',
  66. ? ? ? ? ? ? 'img/enemy3_down6.png'
  67. ? ? ? ? ]
  68. ? ? ? ? var enemy3Arr = [];
  69. ? ? ? ? for (var i = 0; i < enemy3arr.length; i++) {
  70. ? ? ? ? ? ? enemy3Arr[i] = new Image();
  71. ? ? ? ? ? ? enemy3Arr[i].src = enemy3arr[i];
  72. ? ? ? ? }
  73. ? ? ? ? //敌机--大 ? 数据
  74. ? ? ? ? var enemy3obj = {
  75. ? ? ? ? ? ? img: enemy3Arr,
  76. ? ? ? ? ? ? width: 169,
  77. ? ? ? ? ? ? height: 258,
  78. ? ? ? ? ? ? length: enemy3Arr.length,
  79. ? ? ? ? ? ? frac:10,
  80. ? ? ? ? ? ? full:4,
  81. ? ? ? ? }
  82. ? ? ? ? // 敌机--大 ? 函数
  83. ? ? ? ? function Enemy3(params) {
  84. ? ? ? ? ? ? this.imgs = params.img;
  85. ? ? ? ? ? ? this.width = params.width;
  86. ? ? ? ? ? ? this.height = params.height;
  87. ? ? ? ? ? ? this.length = params.length;
  88. ? ? ? ? ? ? this.frac=params.frac;
  89. ? ? ? ? ? ? this.index = 0;
  90. ? ? ? ? ? ? this.thim = 0;
  91. ? ? ? ? ? ? this.buff=Math.random<0.2?true:false; ? //随机带buff
  92. ? ? ? ? ? ? this.ext=false;//敌机是否被击落
  93. ? ? ? ? ? ? this.full = params.full;
  94. ? ? ? ? ? ? this.full_=Math.floor(this.full/2);//战损
  95. ? ? ? ? ? ? this.x = Math.random() * (bakg.width - this.width);
  96. ? ? ? ? ? ? this.y = -this.height;
  97. ? ? ? ? ? ? this.paint = function () {
  98. ? ? ? ? ? ? ? ? cex.drawImage(this.imgs[this.index], this.x, this.y);
  99. ? ? ? ? ? ? }
  100. ? ? ? ? ? ? this.sprot = function () {
  101. ? ? ? ? ? ? ? ? this.y += 5;
  102. ? ? ? ? ? ? ? ? if (this.full <= 0) {
  103. ? ? ? ? ? ? ? ? ? ? this.index++;
  104. ? ? ? ? ? ? ? ? }else if(this.full>0&&this.full<=this.full_){
  105. ? ? ? ? ? ? ? ? ? ? this.index=2;
  106. ? ? ? ? ? ? ? ? }else if (this.thim % 5 == 0) {
  107. ? ? ? ? ? ? ? ? ? ? this.index++;
  108. ? ? ? ? ? ? ? ? ? ? if (this.index == 2) {
  109. ? ? ? ? ? ? ? ? ? ? ? ? this.index = 0;
  110. ? ? ? ? ? ? ? ? ? ? }
  111. ? ? ? ? ? ? ? ? }
  112. ? ? ? ? ? ? ? ? this.thim++;
  113. ? ? ? ? ? ? }
  114. ? ? ? ? }
  115. ? ? ? ? //敌机数组
  116. ? ? ? ? var enemy = [];
  117. ? ? ? ? // 敌机绘制
  118. ? ? ? ? function enemy_paint() {
  119. ? ? ? ? ? ? for (var i = 0; i < enemy.length; i++) {
  120. ? ? ? ? ? ? ? ? enemy[i].paint();
  121. ? ? ? ? ? ? }
  122. ? ? ? ? }
  123. ? ? ? ? // 敌机移动
  124. ? ? ? ? function enemy_sprot() {
  125. ? ? ? ? ? ? for (var i = 0; i < enemy.length; i++) {
  126. ? ? ? ? ? ? ? ? enemy[i].sprot();
  127. ? ? ? ? ? ? }
  128. ? ? ? ? }
  129. ? ? ? ? // 敌机爆炸后删除
  130. ? ? ? ? function enemy_del(){
  131. ? ? ? ? ? ? for(var i=0;i<enemy.length;i++){
  132. ? ? ? ? ? ? ? ? if(enemy[i].index==enemy[i].length){
  133. ? ? ? ? ? ? ? ? ? ? hero.frac+=enemy[i].frac;
  134. ? ? ? ? ? ? ? ? ? ? enemy.splice(i,1);
  135. ? ? ? ? ? ? ? ? ? ? i--;
  136. ? ? ? ? ? ? ? ? }
  137. ? ? ? ? ? ? }
  138. ? ? ? ? }

再创建一个函数判断碰撞

  1. // 检测敌机是否产生碰撞
  2. function enemy_bull_hero() {
  3. ? ? ? ? ? ? hero.invinc--;
  4. ? ? ? ? ? ? for (var i = 0; i < enemy.length; i++) {
  5. ? ? ? ? ? ? ? ? if (hero.invinc<=0&&hero.y <= enemy[i].y + enemy[i].height&&hero.y>enemy[i].y-hero.height) {
  6. ? ? ? ? ? ? ? ? ? ? if (hero.x > enemy[i].x - hero.width && hero.x < enemy[i].x + enemy[i].width) {
  7. ? ? ? ? ? ? ? ? ? ? ?// 飞机与敌机碰撞;
  8. ? ? ? ? ? ? ? ? ? ? ? ? hero.full--;
  9. ? ? ? ? ? ? ? ? ? ? ? ? hero.invinc=hero.invinc_;
  10. ? ? ? ? ? ? ? ? ? ? ? ? if(hero.full==0){
  11. ? ? ? ? ? ? ? ? ? ? ? ? ? ? state = 4;
  12. ? ? ? ? ? ? ? ? ? ? ? ? }
  13. ? ? ? ? ? ? ? ? ? ? }
  14. ? ? ? ? ? ? ? ? }
  15. ? ? ? ? ? ? ? ? for (var n = 0; n < bullsec.length; n++) {
  16. ? ? ? ? ? ? ? ? ? ? if (!enemy[i].ext&&bullsec[n].y <= enemy[i].y + enemy[i].height&&bullsec[n].y>enemy[i].y-bullsec[n].height) {
  17. ? ? ? ? ? ? ? ? ? ? ? ? if (bullsec[n].x > enemy[i].x - bullsec[n].width && bullsec[n].x < enemy[i].x + enemy[i]
  18. ? ? ? ? ? ? ? ? ? ? ? ? ? ? .width) {
  19. ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 敌机与子弹碰撞;
  20. ? ? ? ? ? ? ? ? ? ? ? ? ? ? bullsec.splice(n, 1);
  21. ? ? ? ? ? ? ? ? ? ? ? ? ? ? n--;
  22. ? ? ? ? ? ? ? ? ? ? ? ? ? ? enemy[i].full--;
  23. ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(enemy[i].full<=0){
  24. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? enemy[i].ext=true;
  25. ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
  26. ? ? ? ? ? ? ? ? ? ? ? ? }
  27. ? ? ? ? ? ? ? ? ? ? }
  28. ? ? ? ? ? ? ? ? }
  29. ? ? ? ? ? ? }
  30. ? ? ? ? }

再分别绑定相应的事件

  1. //点击画布从状态0切换到状态1;
  2. canvas.onclick = function () {
  3. ? ? ? ? ? ? if (state == 0) {
  4. ? ? ? ? ? ? ? ? state = 1;
  5. ? ? ? ? ? ? }
  6. ? ? ? ? }
  7. ? ? ? ? // 飞机跟随鼠标移动
  8. ? ? ? ? canvas.onmousemove = function (e) {
  9. ? ? ? ? ? ? if (state == 3) {
  10. ? ? ? ? ? ? ? ? state = 2;
  11. ? ? ? ? ? ? }
  12. ? ? ? ? ? ? if (state == 2) {
  13. ? ? ? ? ? ? ? ? var x = e.offsetX;
  14. ? ? ? ? ? ? ? ? var y = e.offsetY;
  15. ? ? ? ? ? ? ? ? hero.x = x - hero.width / 2;
  16. ? ? ? ? ? ? ? ? hero.y = y - hero.height / 2;
  17. ? ? ? ? ? ? }
  18. ? ? ? ? }
  19. ? ? ? ? // 鼠标移出暂停
  20. ? ? ? ? canvas.onmouseout = function () {
  21. ? ? ? ? ? ? if (state == 2) {
  22. ? ? ? ? ? ? ? ? state = 3;
  23. ? ? ? ? ? ? }
  24. ? ? ? ? }
  25. ? ? ? ? // 弹夹子弹发射
  26. ? ? ? ? document.onkeydown =function(event){
  27. ? ? ? ? ? ? if(state==2){
  28. ? ? ? ? ? ? ? ? if(event.keyCode==32&&hero.maga>0){
  29. ? ? ? ? ? ? ? ? ? ? hero.bullk() //增加界面射出子弹
  30. ? ? ? ? ? ? ? ? ? ? hero.maga--;
  31. ? ? ? ? ? ? ? ? }
  32. ? ? ? ? ? ? }
  33. ? ? ? ? ? ??
  34. ? ? ? ? };

最终在定时器中实时更新相应的画面

  1. ?setInterval(function () {
  2. ? ? ? ? ? ? bakg.paint()
  3. ? ? ? ? ? ? bakg.sprot()
  4. ? ? ? ? ? ? cex.font='40px 微软雅黑';
  5. ? ? ? ? ? ? cex.fillText('生命:'+hero.full,330,40);
  6. ? ? ? ? ? ? cex.fillText('分数:'+hero.frac,0,40);
  7. ? ? ? ? ? ? cex.fillText('子弹:'+hero.maga,0,80);
  8. ? ? ? ? ? ? if (state == 0) { //初始化
  9. ? ? ? ? ? ? ? ? cex.drawImage(logo, 40, 0);
  10. ? ? ? ? ? ? }
  11. ? ? ? ? ? ? if (state == 1) { //出场动画
  12. ? ? ? ? ? ? ? ? game.paint();
  13. ? ? ? ? ? ? ? ? game.sprot();
  14. ? ? ? ? ? ? }
  15. ? ? ? ? ? ? if (state == 2) { //战斗状态
  16. ? ? ? ? ? ? ? ? hero.paint()
  17. ? ? ? ? ? ? ? ? hero.sprot()
  18. ? ? ? ? ? ? ? ? bull_paint()
  19. ? ? ? ? ? ? ? ? bull_sprot()
  20. ? ? ? ? ? ? ? ? hero.enemysos() //增加敌机数量
  21. ? ? ? ? ? ? ? ? enemy_paint()
  22. ? ? ? ? ? ? ? ? enemy_sprot()
  23. ? ? ? ? ? ? ? ? enemy_bull_hero() //碰撞检测
  24. ? ? ? ? ? ? ? ? enemy_del();
  25. ? ? ? ? ? ? }
  26. ? ? ? ? ? ? if (state == 3) { //暂停状态
  27. ? ? ? ? ? ? ? ? cex.drawImage(pause, 210, 375)
  28. ? ? ? ? ? ? ? ? hero.paint()
  29. ? ? ? ? ? ? ? ? bull_paint()
  30. ? ? ? ? ? ? ? ? enemy_paint()
  31. ? ? ? ? ? ? }
  32. ? ? ? ? ? ? if (state == 4) { //游戏结束状态
  33. ? ? ? ? ? ? ? ? cex.fillText('菜',bakg.width/2-30,bakg.height/2-90);
  34. ? ? ? ? ? ? }
  35. ? ? ? ? }, 30)

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号