H5 canvas实现简单的飞机大战小游戏

r囧r小猫 2023-06-24 06:26 91阅读 0赞

整体分析飞机大战游戏

游戏存在就会有准备阶段(控制游戏全局属性):

1. 定义游戏状态

  1. 第一阶段:游戏欢迎状态 0 START
  2. 第二阶段:游戏加载状态 1 LOADING
  3. 第三阶段:游戏运行状态 2 RUNNING
  4. 第四阶段:游戏暂停阶段 3 PAUSE
  5. 第五阶段:游戏结束阶段 4 GAMEOVER

2. 定义游戏得分 score = 0

3. 定义我方飞机的生命值 life = 3

4. 定义游戏开关:State=0

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>飞机</title>
  6. <style type="text/css">
  7. body{
  8. text-align: center;
  9. }
  10. canvas{
  11. background-color: #ccc;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <canvas id="canvas" width="480" height="850"></canvas>
  17. <script type="text/javascript">
  18. //1. 定义游戏状态 第一阶段:游戏欢迎状态 0 START 第二阶段:游戏加载状态 1 LOADING 第三阶段:游戏运行状态 2 RUNNING 第四阶段:游戏暂停阶段 3 PAUSE 第五阶段:游戏结束阶段 4 GAMEOVER
  19. const START = 0,
  20. LOADING = 1,
  21. RUNNING = 2,
  22. PAUSE = 3,
  23. GAMEOVER = 4;
  24. //2. 定义游戏得分 score = 0
  25. var score = 0,
  26. //3. 定义我方飞机的生命值
  27. life = 3,
  28. //4. 定义游戏开关
  29. State=0 ,
  30. //当前画布的宽高
  31. WIDTH = 480,
  32. HEIGHT = 850;
  33. //1.第一阶段开始:游戏欢迎阶段
  34. var canvas = document.getElementById('canvas');
  35. var cxt = canvas.getContext('2d');
  36. var myimg = new Image();
  37. myimg.src ="images/background.png";
  38. //定义背景的数据
  39. var obj = {
  40. imgs : myimg,
  41. width:480,
  42. height:850
  43. }
  44. //定义背景对象的构造器
  45. function BG(obj){
  46. //定义绘制背景的坐标
  47. this.x1 = 0;
  48. this.y1 = 0;
  49. this.x2 = 0;
  50. this.y2 = -obj.height;
  51. this.width = obj.width;
  52. this.height = obj.height;
  53. //定义绘制背景图片的方法
  54. this.draw = function(){
  55. cxt.drawImage(obj.imgs,this.x1,this.y1);
  56. cxt.drawImage(obj.imgs,this.x2,this.y2);
  57. }
  58. //判断两张图片的临界值
  59. this.bgrun =function(){
  60. this.y1++;
  61. this.y2++;
  62. if(this.y1 == this.height){
  63. this.y1 = -this.height;
  64. }else if(this.y2 == this.height){
  65. this.y2 = -this.height;
  66. }
  67. }
  68. }
  69. var bg = new BG(obj);
  70. var logo = new Image();
  71. logo.src ="images/start.png";
  72. //控制游戏状态从一个变为第二个
  73. canvas.onclick = function(){
  74. if(State == START){
  75. State = LOADING;
  76. }
  77. }
  78. //2.第二阶段开始
  79. var loadings = [];
  80. loadings[0] = new Image();
  81. loadings[0].src="images/game_loading1.png";
  82. loadings[1] = new Image();
  83. loadings[1].src="images/game_loading2.png";
  84. loadings[2] = new Image();
  85. loadings[2].src="images/game_loading3.png";
  86. loadings[3] = new Image();
  87. loadings[3].src="images/game_loading4.png";
  88. //初始化动态效果的数据
  89. var loading={
  90. imgs:loadings,
  91. width : 186,
  92. height : 38,
  93. length : loadings.length
  94. }
  95. function Loading(obj){
  96. this.startIndex = 0;
  97. //定义绘制加载效果图片的方法
  98. this.endloading = function(){
  99. cxt.drawImage(obj.imgs[this.startIndex],(WIDTH-obj.width)/2,HEIGHT-obj.height)
  100. }
  101. this.time = 0;
  102. //定义动画方法
  103. this.speed = function(){
  104. this.time++;
  105. if(this.time % 5 == 0){
  106. this.startIndex++;
  107. }
  108. if(this.startIndex == obj.length){
  109. State = RUNNING;
  110. }
  111. }
  112. }
  113. var load = new Loading(loading);
  114. // 游戏运行阶段
  115. // 绘制我方灰机
  116. var heros = [];
  117. heros[0] = new Image();
  118. heros[0].src = "images/hero1.png";
  119. heros[1] = new Image();
  120. heros[1].src = "images/hero2.png";
  121. heros[2] = new Image();
  122. heros[2].src = "images/hero_blowup_n1.png";
  123. heros[3] = new Image();
  124. heros[3].src = "images/hero_blowup_n2.png";
  125. heros[4] = new Image();
  126. heros[4].src = "images/hero_blowup_n3.png";
  127. heros[5] = new Image();
  128. heros[5].src = "images/hero_blowup_n4.png";
  129. //初始化我方飞机的数据
  130. var HERO = {
  131. imgs:heros,
  132. length:heros.length,
  133. width:99,
  134. height :124,
  135. frame : 2
  136. }
  137. //我方飞机
  138. function Hero(obj){
  139. this.imgs = obj.imgs;
  140. this.length = obj.length;
  141. this.height=obj.height;
  142. this.width = obj.width;
  143. this.x =(WIDTH-this.width)/2;
  144. this.y = HEIGHT-this.height;
  145. this.frame = obj.frame;
  146. this.startIndex = 0;
  147. this.candel = false;
  148. //新增一个标识--表示是否被撞击,当前没有被撞击
  149. this.down = false;
  150. //绘制图片
  151. this.draw = function(){
  152. cxt.drawImage(this.imgs[this.startIndex],this.x,this.y)
  153. }
  154. this.speed = function(){
  155. if(!this.down){
  156. //没有被撞击:startIndex在0和1进行切换
  157. if(this.startIndex == 0){
  158. this.startIndex = 1
  159. }else{
  160. this.startIndex = 0;
  161. }
  162. }else{
  163. //被撞击
  164. this.startIndex++;
  165. if(this.startIndex == obj.length){
  166. //生命减少1
  167. life--;
  168. if(life == 0){
  169. State = GAMEOVER;
  170. this.startIndex = this.length-1;
  171. }else{
  172. hero = new Hero(HERO)
  173. }
  174. }
  175. }
  176. }
  177. this.time = 0;
  178. //增加子弹射击的方法
  179. //物体运动思想控制敌机和子弹的速度
  180. this.shoot = function(){
  181. this.time++;
  182. if(this.time% 5 == 0){
  183. bullets.push(new Bullet(BULLET));
  184. }
  185. if(this.time% 13== 0){
  186. shootenemys.push(new Enemy(ENEMY));
  187. }
  188. if(this.time% 120== 0){
  189. shootenemys.push(new Enemy(ENEMYM));
  190. }
  191. if(this.time% 310== 0){
  192. shootenemys.push(new Enemy(ENEMYL));
  193. }
  194. }
  195. this.bang = function(){
  196. this.down = true;
  197. }
  198. }
  199. var hero = new Hero(HERO);
  200. canvas.onmousemove = function(event){
  201. // var event = event || e || arguments[0];
  202. if(State == RUNNING){
  203. var x = event.offsetX;
  204. var y = event.offsetY;
  205. hero.x = x-hero.width/2;
  206. hero.y = y-hero.height/2;
  207. }
  208. }
  209. //制造子弹
  210. var bullet = new Image();
  211. bullet.src = "images/bullet1.png";
  212. //初始化子弹的构造器
  213. var BULLET ={
  214. imgs : bullet,
  215. width : 9,
  216. height : 21
  217. }
  218. function Bullet(obj){
  219. this.imgs = obj.imgs;
  220. this.width = obj.width;
  221. this.height = obj.height;
  222. //子弹绘制的坐标
  223. this.x = hero.x + hero.width/2-this.width/2;
  224. this.y = hero.y-this.height-10;
  225. this.draw = function(){
  226. cxt.drawImage(this.imgs,this.x,this.y);
  227. }
  228. this.speed = function(){
  229. this.y -=4;
  230. }
  231. this.candel = false;
  232. this.bang = function(){
  233. this.candel = true;
  234. }
  235. }
  236. //创建存储子弹的数组
  237. var bullets = [];
  238. //创建函数,用于绘制所有的子弹
  239. // function bulletsPanit(){
  240. // for(var i=0;i<bullets.length;i++){
  241. // bullets[i].draw();
  242. // bullets[i].speed();
  243. // if(bullets[i].y < -bullets[i].height || bullets[i].candel){
  244. // bullets.splice(i,1);
  245. // }
  246. // }
  247. // }
  248. // 绘制子弹
  249. function drawBullets(){
  250. for(var i=0;i<bullets.length;i++){
  251. bullets[i].draw();
  252. }
  253. }
  254. //子弹运动
  255. function speedBullets(){
  256. for(var i=0;i<bullets.length;i++){
  257. bullets[i].speed();
  258. }// 绘制子弹
  259. //子弹运动
  260. //删除击中和越界的子弹
  261. }
  262. //删除击中和越界的子弹
  263. function delBullets(){
  264. for(var i=0;i<bullets.length;i++){
  265. if(bullets[i].y < -bullets[i].height || bullets[i].candel){
  266. bullets.splice(i,1);
  267. }
  268. }
  269. }
  270. //敌机小号
  271. var enemys = [];
  272. enemys[0] = new Image();
  273. enemys[0].src="images/enemy1.png";
  274. enemys[1] = new Image();
  275. enemys[1].src="images/enemy1_down1.png";
  276. enemys[2] = new Image();
  277. enemys[2].src="images/enemy1_down2.png";
  278. enemys[3] = new Image();
  279. enemys[3].src="images/enemy1_down3.png";
  280. enemys[4] = new Image();
  281. enemys[4].src="images/enemy1_down4.png";
  282. var ENEMY = {
  283. length : enemys.length,
  284. imgs : enemys,
  285. width : 57,
  286. height : 51,
  287. type : 1,
  288. life : 1,
  289. score : 1 ,
  290. frame : 1
  291. }
  292. //敌机构造
  293. function Enemy(obj){
  294. this.type = obj.type;
  295. this.imgs = obj.imgs;
  296. this.life = obj.life;
  297. this.width = obj.width;
  298. this.height = obj.height;
  299. this.length = obj.length;
  300. this.x = Math.floor(Math.random()*(WIDTH-this.width));
  301. this.y = -this.height;
  302. //当前没被撞击
  303. this.down = false;
  304. this.startIndex = 0;
  305. //确定动画是否播完
  306. this.cancel = false;
  307. //分数
  308. this.score = obj.score;
  309. //
  310. this.frame = obj.frame;
  311. this.draw = function(){
  312. cxt.drawImage(this.imgs[this.startIndex],this.x,this.y);
  313. }
  314. this.speed =function(){
  315. if(!this.down){
  316. this.y+=5;
  317. this.startIndex++;
  318. this.startIndex = this.startIndex%this.frame;
  319. }else{
  320. this.startIndex++;
  321. if(this.startIndex == this.length){
  322. this.cancel = true;
  323. this.startindex = this.length-1;
  324. }
  325. }
  326. }
  327. //碰撞
  328. this.collision = function(obj){
  329. return obj.y + obj.height > this.y
  330. && obj.x + obj.width > this.x
  331. && obj.y < this.y + this.height
  332. && obj.x < this.x + this.width;
  333. }
  334. this.bang = function(){
  335. this.life--;
  336. if(this.life == 0){
  337. this.down = true;
  338. score += this.score;
  339. }
  340. }
  341. }
  342. var shootenemys = [];
  343. // function shootenemy(){
  344. // for(var i=0;i<shootenemys.length;i++){
  345. // shootenemys[i].draw();
  346. // shootenemys[i].speed();
  347. // if( shootenemys[i].cancel||shootenemys[i].y > (HEIGHT+shootenemys[i].height) ){
  348. // shootenemys.splice(i,1);
  349. // }
  350. // }
  351. // }
  352. // 绘制敌机
  353. function drawEnemies(){
  354. for(var i=0;i<shootenemys.length;i++){
  355. shootenemys[i].draw();
  356. }
  357. }
  358. //敌机运动
  359. function speedEnemies(){
  360. for(var i=0;i<shootenemys.length;i++){
  361. shootenemys[i].speed();
  362. }
  363. }
  364. //删除击中和越界的敌机
  365. function delEnemies(){
  366. for(var i=0;i<shootenemys.length;i++){
  367. if( shootenemys[i].cancel||shootenemys[i].y > (HEIGHT+shootenemys[i].height) ){
  368. shootenemys.splice(i,1);
  369. }
  370. }
  371. }
  372. function hit(){
  373. for(var i=0;i<shootenemys.length;i++){
  374. // 我方飞机撞击敌方飞机
  375. if(shootenemys[i].collision(hero)){
  376. // 处理敌方飞机撞击后的逻辑
  377. shootenemys[i].bang();
  378. // 处理我方飞机撞击后的逻辑
  379. hero.bang();
  380. }
  381. // 子弹撞击敌方飞机bullets
  382. for(var j=0;j<bullets.length;j++){
  383. if(shootenemys[i].collision(bullets[j])){
  384. // 处理敌方飞机撞击后的逻辑
  385. shootenemys[i].bang();
  386. // 处理子弹撞击后的逻辑
  387. bullets[j].bang();
  388. }
  389. }
  390. }
  391. }
  392. //敌机中号
  393. var enemym = [];
  394. enemym[0] = new Image();
  395. enemym[0].src="images/enemy2.png";
  396. enemym[1] = new Image();
  397. enemym[1].src="images/enemy2_down1.png";
  398. enemym[2] = new Image();
  399. enemym[2].src="images/enemy2_down2.png";
  400. enemym[3] = new Image();
  401. enemym[3].src="images/enemy2_down3.png";
  402. enemym[4] = new Image();
  403. enemym[4].src="images/enemy2_down4.png";
  404. var ENEMYM = {
  405. length : enemym.length,
  406. imgs : enemym,
  407. width : 69,
  408. height : 95,
  409. type : 1,
  410. life : 3,
  411. score : 4 ,
  412. frame : 1
  413. }
  414. // 敌机大号
  415. var enemyl = [];
  416. enemyl[0] = new Image();
  417. enemyl[0].src="images/enemy3_n2.png";
  418. enemyl[1] = new Image();
  419. enemyl[1].src="images/enemy3_n1.png";
  420. enemyl[2] = new Image();
  421. enemyl[2].src="images/enemy3_down1.png";
  422. enemyl[3] = new Image();
  423. enemyl[3].src="images/enemy3_down2.png";
  424. enemyl[4] = new Image();
  425. enemyl[4].src="images/enemy3_down3.png";
  426. enemyl[5] = new Image();
  427. enemyl[5].src="images/enemy3_down4.png";
  428. enemyl[6] = new Image();
  429. enemyl[6].src="images/enemy3_down5.png";
  430. enemyl[7] = new Image();
  431. enemyl[7].src="images/enemy3_down6.png";
  432. var ENEMYL = {
  433. length : enemyl.length,
  434. imgs : enemyl,
  435. width : 165,
  436. height : 261,
  437. type : 1,
  438. life : 20,
  439. score : 10 ,
  440. frame : 2
  441. }
  442. //绘制分数和生命
  443. function text(){
  444. cxt.font = "bold 26px 宋体"
  445. cxt.fillText(`score:${ score}`,10,30)
  446. cxt.fillText("LIFE"+life,WIDTH-80,30);
  447. }
  448. //鼠标移入
  449. canvas.onmouseover =function(){
  450. if(State == PAUSE){
  451. State = RUNNING;
  452. }
  453. }
  454. //鼠标移出
  455. canvas.onmouseout =function(){
  456. if(State == RUNNING){
  457. State = PAUSE;
  458. }
  459. }
  460. //暂停
  461. var pause = new Image();
  462. pause.src = "images/game_pause_nor.png";
  463. function over(){
  464. cxt.font = "bold 48px 宋体";
  465. cxt.fillText("GAME OVER",150,400);
  466. }
  467. //游戏核心控制器
  468. setInterval(function(){
  469. bg.draw();
  470. //控制背景运动
  471. bg.bgrun();
  472. if(State == START){
  473. cxt.drawImage(logo,40,20);
  474. }else if(State == LOADING){
  475. load.endloading();
  476. load.speed();
  477. }else if(State == RUNNING){
  478. //我方飞机
  479. hero.draw();
  480. //我方飞机运动
  481. hero.speed();
  482. //绘制分数和生命
  483. text();
  484. //我方飞机射击
  485. hero.shoot();
  486. // bulletsPanit();
  487. // 检查碰撞
  488. hit()
  489. //绘制敌机
  490. drawEnemies()
  491. //绘制敌机运动
  492. speedEnemies()
  493. //判断删除敌机
  494. delEnemies()
  495. //绘制子弹
  496. drawBullets()
  497. //绘制子弹运动
  498. speedBullets()
  499. //判断删除子弹
  500. delBullets()
  501. }else if(State == PAUSE){
  502. //暂停状态时保存画面除去运动
  503. text()
  504. //绘制暂停图标
  505. cxt.drawImage(pause,200,400);
  506. drawEnemies()
  507. drawBullets()
  508. hero.draw();
  509. }else if(State == GAMEOVER){
  510. //游戏结束
  511. drawEnemies()
  512. drawBullets()
  513. delEnemies()
  514. text()
  515. over()
  516. hero.draw();
  517. }
  518. },100);
  519. </script>
  520. </body>
  521. </html>

发表评论

表情:
评论列表 (有 0 条评论,91人围观)

还没有评论,来说两句吧...

相关阅读

    相关 飞机大战(微信游戏

    依旧是熟悉的期末作业,是微信小游戏官网的Demo来着,然后我边百度边对它做了一些改动 说起来好久没更新博客了......最近在用unity做游戏准备参加计算机设计大赛,花了好