电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

js實現飛機大戰游戲

瀏覽:118日期:2024-04-23 16:11:16

本文實例為大家分享了js實現飛機大戰游戲的具體代碼,供大家參考,具體內容如下

CSS部分的代碼:

<style> * { margin: 0px; padding: 0px; } canvas{ border: 1px solid #000; display: block; margin: auto; }</style>

JavaScript代碼:

<!-- 先創建一個畫布 --><canvas height='640'></canvas><script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); // 0游戲初始化 // 0.1定義游戲開始的五個階段 var START = 0; var STARTING = 1; var RUNNING = 2; var PAUSE = 3; var GAMEOVER = 4; // 0.2 定義一個自己的狀態,時刻去和上面的五個狀態作比較 // 0.3 頁面加載時 var state = START; // 0.4 背景圖片的寬和高 var WIDTH = 480; var HEIGHT = 640; // 0.5 定義分數 var score = 0; // 0.6 定義生命條數 var life = 3; // 1 游戲開始前 // 1.1 加載背景圖片 // 1.1.1 背景圖片的對象 var bg = new Image();// 創建一個背景圖片 bg.src = 'http://www.hdgsjgj.cn/bcjs/images/background.png'; // 1.1.2 背景圖片的數據 var BG = { imgs: bg, width: 480, height: 852 } // 1.1.3 背景圖片的構造函數 function Bg(config) { this.imgs = config.imgs; this.width = config.width; this.height = config.height; // 繪制圖片的坐標(兩張背景圖片進行輪流滑動) this.x1 = 0; this.y1 = 0; this.x2 = 0; this.y2 = -this.height; // 背景圖片繪制 this.paint = function () { context.drawImage(this.imgs, this.x1, this.y1); context.drawImage(this.imgs, this.x2, this.y2); } // 圖片的運動 this.step = function () { this.y1++; this.y2++; // 判斷圖片的臨界點 if (this.y1 == this.height) {this.y1 = -this.height; } if (this.y2 == this.height) {this.y2 = -this.height; } } } // 1.1.4創建對象 var sky = new Bg(BG); // console.log(sky); // 1.2 創建頁面加載時的飛機大戰圖片 var logo = new Image(); logo.src = 'http://www.hdgsjgj.cn/bcjs/images/start.png'; // 2.游戲開始前 // 2.1 開始前動畫的對象 var loadings = []; loadings[0] = new Image(); loadings[0].src = 'http://www.hdgsjgj.cn/bcjs/images/game_loading1.png'; loadings[1] = new Image(); loadings[1].src = 'http://www.hdgsjgj.cn/bcjs/images/game_loading2.png'; loadings[2] = new Image(); loadings[2].src = 'http://www.hdgsjgj.cn/bcjs/images/game_loading3.png'; loadings[3] = new Image(); loadings[3].src = 'http://www.hdgsjgj.cn/bcjs/images/game_loading4.png'; // 2.2 開始前加載的動畫圖片的數據 var LOADINGS = { imgs: loadings, length: loadings.length, width: 186, height: 38 } // 2.3 開始前動畫的構造函數 function Loading(config) { this.imgs = config.imgs; this.length = config.length; this.width = config.width; this.height = config.height; // 定義一個索引 this.startIndex = 0; // 繪制 this.paint = function () { context.drawImage(this.imgs[this.startIndex], 0, HEIGHT - this.height); } // 定義一個速度 this.time = 0; this.step = function () { this.time++; if (this.time % 3 == 0) { // 頁面加載時下面小飛機運行的速度this.startIndex++; } // 當動畫運行完成進入下一個階段 if (this.startIndex == this.length) {state = RUNNING; } } } // 2.4創建對象 var loading = new Loading(LOADINGS); // 2.5綁定時間 canvas.onclick = function () { if (state == START) { state = STARTING; } } // 3.1.1游戲開始時的圖片 var heros = []; heros[0] = new Image(); heros[0].src = 'http://www.hdgsjgj.cn/bcjs/images/hero1.png'; heros[1] = new Image(); heros[1].src = 'http://www.hdgsjgj.cn/bcjs/images/hero2.png'; heros[2] = new Image(); heros[2].src = 'http://www.hdgsjgj.cn/bcjs/images/hero_blowup_n1.png'; heros[3] = new Image(); heros[3].src = 'http://www.hdgsjgj.cn/bcjs/images/hero_blowup_n2.png'; heros[4] = new Image(); heros[4].src = 'http://www.hdgsjgj.cn/bcjs/images/hero_blowup_n3.png'; heros[5] = new Image(); heros[5].src = 'http://www.hdgsjgj.cn/bcjs/images/hero_blowup_n4.png'; // 3.1.2游戲開始時加載數據 var HEROS = { imgs: heros, length: heros.length, width: 99, height: 124, // 我方飛機有兩種狀態,增加標識 frame: 2 } // 3.1.3我方飛機的構造器 function Hero(config) { this.imgs = config.imgs; this.length = config.length; this.width = config.width; this.height = config.height; this.frame = config.frame; // 定義索引 this.startIndex = 0; // 繪制坐標 this.x = WIDTH / 2 - this.width / 2; this.y = HEIGHT - 150; // 增加標識符 this.down = false; //表示一直沒有撞擊 // 增加標識符 this.candel = false; //表示撞擊以后的動畫是否運行完成,完成以后的恢復運行的狀態 // 定義繪制方法 this.paint = function () { context.drawImage(this.imgs[this.startIndex], this.x, this.y) } // 定義運動的方法 this.step = function () { // 兩個狀態 // 1.正常運動狀態 // 2.碰撞以后的狀態 if (!this.down) {//正常運動狀態// 沒有發生撞擊的時候 一直在0和1之間切換this.startIndex++;this.startIndex = this.startIndex % 2; } else {//撞擊以后的狀態// 腳標就要不停的加1,模擬出從碰撞到爆炸完成的動畫this.startIndex++;// 判斷是否完成撞擊if (this.startIndex == this.length) { life--//爆炸一次生命值減1; if (life == 0) { state = GAMEOVER; // 如果死了,動畫保存最后一張爆破的照片 this.startIndex = this.length - 1; } else { hero = new Hero(HEROS); }} } } // 我方飛機增加射擊方法 this.time = 0; this.shoot = function () { this.time++; if (this.time % 2 == 0) {bullets.push(new Bullet(BULLET)); } } // 撞擊以后觸發 this.bang = function () { this.down = true; } } // 3.1.4我方飛機的對象 var hero = new Hero(HEROS); // 3.1.5綁定鼠標移動事件 canvas.onmousemove = function (e) { if (state == RUNNING) { var x = e.offsetX; var y = e.offsetY; hero.x = x - hero.width / 2; hero.y = y - hero.height / 2; } } // 3.2 繪制子彈 // 3.2.1 圖片 var bullet = new Image(); bullet.src = 'http://www.hdgsjgj.cn/bcjs/images/bullet1.png'; // 3.2.2數據 var BULLET = { imgs: bullet, width: 9, height: 21 } // 3.2.3 子彈的構造函數 function Bullet(config) { this.imgs = config.imgs; this.width = config.width; this.height = config.height; // 子彈坐標 this.x = hero.x + hero.width / 2 - this.width / 2; this.y = hero.y - this.height; // 繪制 this.paint = function () { context.drawImage(this.imgs, this.x, this.y); } // 運動 往上運動 this.step = function () { this.y -= 10; } this.candel = false;//表示撞擊以后的動畫是否運行完成,完成以后的恢復運行的狀態 this.bang = function () { this.candel = true; } } // 3.2.4 存放所有子彈 var bullets = []; // 3.2.5 繪制所有子彈 function bulletsPaint() { for (var i = 0; i < bullets.length; i++) { bullets[i].paint(); } } // 3.2.6 繪制所有子彈的運動 function bulletsStep() { for (var i = 0; i < bullets.length; i++) { bullets[i].step(); } } // 3.2.7 刪除子彈 function bulletsDel(){ for(var i = 0; i < bullets.length;i++){ if(bullets[i].y < -bullets[i].height || bullets[i].candel){ bullets.splice(i,1) } } // console.log(bullets) } // 3.3 敵方飛機 // 3.3.1 敵方飛機的圖片(3種) // 小號 var enemy1 = []; enemy1[0] = new Image(); enemy1[0].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy1.png'; enemy1[1] = new Image(); enemy1[1].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy1_down1.png'; enemy1[2] = new Image(); enemy1[2].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy1_down2.png'; enemy1[3] = new Image(); enemy1[3].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy1_down3.png'; enemy1[4] = new Image(); enemy1[4].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy1_down4.png'; // 中號 var enemy2 = []; enemy2[0] = new Image(); enemy2[0].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy2.png'; enemy2[1] = new Image(); enemy2[1].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy2_down1.png'; enemy2[2] = new Image(); enemy2[2].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy2_down2.png'; enemy2[3] = new Image(); enemy2[3].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy2_down3.png'; enemy2[4] = new Image(); enemy2[4].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy2_down4.png'; // 大號 var enemy3 = []; enemy3[0] = new Image(); enemy3[0].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_n1.png'; enemy3[1] = new Image(); enemy3[1].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_n2.png'; enemy3[2] = new Image(); enemy3[2].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_down1.png'; enemy3[3] = new Image(); enemy3[3].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_down2.png'; enemy3[4] = new Image(); enemy3[4].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_down3.png'; enemy3[5] = new Image(); enemy3[5].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_down4.png'; enemy3[6] = new Image(); enemy3[6].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_down5.png'; enemy3[7] = new Image(); enemy3[7].src = 'http://www.hdgsjgj.cn/bcjs/images/enemy3_down6.png'; // 3.2.2 數據 var ENEMY1 = { imgs: enemy1, length: enemy1.length, width: 57, height: 51, type: 1, //增加標識符,區分飛機的種類。小號的設置成1 frame: 1, //增加標識符,1種狀態就為1,2種狀態就為2 life: 1, //增加標識符,被子彈打擊的次數 score: 1 //打倒一只的得分 } var ENEMY2 = { imgs: enemy2, length: enemy2.length, width: 69, height: 95, type: 2, //增加標識符,區分飛機的種類。中號的設置成2 frame: 1, //增加標識符,1種狀態就為1,2種狀態就為2 life: 3, //增加標識符,被子彈打擊的次數 score: 5 //打倒一只的得分 } var ENEMY3 = { imgs: enemy3, length: enemy3.length, width: 169, height: 258, type: 3, //增加標識符,區分飛機的種類。大號的設置成3 frame: 2, //增加標識符,,1種狀態就為1,2種狀態就為2 life: 10, //增加標識符,被子彈打擊的次數 score: 15 //打倒一只的得分 } // 3.3.3 構造函數 function Enemy(config) { this.imgs = config.imgs; this.length = config.length; this.width = config.width; this.height = config.height; this.type = config.type; this.frame = config.frame; this.life = config.life; this.score = config.score; // 圖片的索引 this.startIndex = 0 this.down = false; //表示一直沒有撞擊 this.candel = false; //表示撞擊以后的動畫是否運行完成,完成以后的恢復運行的狀態 // 繪制坐標 this.x = Math.random() * (WIDTH - this.width); this.y = -this.height; // 繪制的方法 this.paint = function () { context.drawImage(this.imgs[this.startIndex], this.x, this.y); } // 運動方法 this.step = function(){ if(!this.down){ //正常 // 小號的,中號 角標始終是0 // 大號的是在0和1之間切換 this.startIndex ++; this.startIndex = this.startIndex % this.frame; this.y += 2; }else { //爆炸 this.startIndex ++; if(this.startIndex == this.length){ this.candel = true; this.startIndex = this.length - 1; } } } // 爆炸的方法 this.bang = function(){ this.life -- ; if(this.life == 0){ this.down = true; score += this.score; } } // 檢測是否撞擊 this.checkHit = function(wo){ // 1.撞擊到子彈 // 2.撞擊到我方飛機 return wo.y + wo.height > this.y && wo.x + wo.width > this.x && wo.y < this.y + this.height && wo.x < this.x + this.width; } } // 3.3.4 創建數組 存儲敵方飛機 var enemies = []; // 3.3.5 創建飛機 function enterEnemies() { var num = Math.random(); if (num < 0.1) { enemies.push(new Enemy(ENEMY1)) } else if (num < 0.15) { enemies.push(new Enemy(ENEMY2)) } else if (num < 0.16) { enemies.push(new Enemy(ENEMY3)) } } // 3.3.6 繪制 function paintEnemies() { for (var i = 0; i < enemies.length; i++) { enemies[i].paint(); } } // 3.3.7 運動 function stepEnemies() { for (var i = 0; i < enemies.length; i++) { enemies[i].step(); } } // 3.3.8 刪除 function delEnemies(){ for(var i = 0;i < enemies.length;i++){ if(enemies[i].y > HEIGHT || enemies[i].candel){ enemies.splice(i,1) } } } // 3.4 檢測撞擊 function hitEnemies() { for (var i = 0; i < enemies.length; i++) { if (enemies[i].checkHit(hero)) {enemies[i].bang();hero.bang(); } for (var j = 0; j < bullets.length; j++) {if (enemies[i].checkHit(bullets[j])) { enemies[i].bang(); bullets[j].bang();} } } } // 3.5 我方飛機的生命和得分 function paintText(){ context.font = 'bold 30px 微軟雅黑'; context.fillText('SCORE:' + score,10,30); context.fillText('LIFE:' + life,380,30) } // 4.暫停階段 canvas.onmouseover=function(){ if(state==PAUSE){ state=RUNNING; } } canvas.onmouseout=function(){ if(state==RUNNING){ state=PAUSE; } } var pause=new Image(); pause.src='http://www.hdgsjgj.cn/bcjs/images/game_pause_nor.png' function paintdown(){ context.drawImage(pause,220,300) } // 5.gameover階段 function paintOver(){ context.font='bold 50px 微軟雅黑'; context.fillText('GAME OVER',110,300); } // 定時器加載,使圖片緩慢往下面移動 setInterval(function () { sky.paint(); sky.step(); if (state == START) { context.drawImage(logo, 40, 0);//繪制在正中間 } else if (state == STARTING) { loading.paint(); loading.step(); } else if (state == RUNNING) { hero.paint(); hero.step(); hero.shoot(); bulletsPaint(); bulletsStep(); bulletsDel(); enterEnemies(); paintEnemies(); stepEnemies(); delEnemies(); hitEnemies(); paintText(); }else if(state==PAUSE){ hero.paint(); bulletsPaint(); paintEnemies(); paintText(); paintdown(); }else if(state==GAMEOVER){ hero.paint(); bulletsPaint(); paintEnemies(); paintText(); paintdown(); paintOver(); } }, 100)</script>

更多有趣的經典小游戲實現專題,分享給大家:

C++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

java經典小游戲匯總

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 缠膜机|缠绕包装机|无纺布包装机-济南达伦特机械设备有限公司 | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | 比亚迪叉车-比亚迪电动叉车堆垛车托盘车仓储叉车价格多少钱报价 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 专业甜品培训学校_广东糖水培训_奶茶培训_特色小吃培训_广州烘趣甜品培训机构 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 潍坊青州古城旅游景点攻略_青州酒店美食推荐-青州旅游网 | 武汉不干胶印刷_标签设计印刷_不干胶标签印刷厂 - 武汉不干胶标签印刷厂家 | 都江堰招聘网-都江堰人才网 都江堰人事人才网 都江堰人才招聘网 邢台人才网_邢台招聘网_邢台123招聘【智达人才网】 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 北京软件开发_软件开发公司_北京软件公司-北京宜天信达软件开发公司 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 高清视频编码器,4K音视频编解码器,直播编码器,流媒体服务器,深圳海威视讯技术有限公司 | 警方提醒:赣州约炮论坛真的安全吗?2025年新手必看的网络交友防坑指南 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 阿里巴巴诚信通温州、台州、宁波、嘉兴授权渠道商-浙江联欣科技提供阿里会员办理 | 微动开关厂家-东莞市德沃电子科技有限公司 | 鼓风干燥箱_真空烘箱_高温干燥箱_恒温培养箱-上海笃特科学仪器 | 滚珠丝杆升降机_螺旋升降机_丝杠升降机-德迈传动 | 水上浮桥-游艇码头-浮动码头-游船码头-码瑞纳游艇码头工程 | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 自清洗过滤器_全自动过滤器_全自动反冲洗过滤器_量子过滤器-滑漮滴 | 开云(中国)Kaiyun·官方网站 - 登录入口 | 安徽控制器-合肥船用空调控制器-合肥家电控制器-合肥迅驰电子厂 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 利浦顿蒸汽发生器厂家-电蒸汽发生器/燃气蒸汽发生器_湖北利浦顿热能科技有限公司官网 | 真空包装机-诸城市坤泰食品机械有限公司 |