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

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

用JS實現飛機大戰小游戲

瀏覽:76日期:2024-03-23 10:08:03

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

小的時候玩的飛機大戰感覺還蠻神奇,今天自己就學著做了一個

先制作好要做好的幾步以及背景樣式

var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var start = 0; // 開始階段 var starting = 1; // 開始的加載階段 var running = 2; // 游戲階段 var pause = 3; // 暫停階段 var gameover = 4; // 結束階段 var state = start; // 目前狀態 var width = canvas.width; // 獲取畫布的寬度 var height = canvas.height; // 獲取畫布的高度 var score = 0; // 分數 var life = 3; // 我放飛機生命值 var bg = new Image(); // 創建背景圖片 bg.src = 'http://www.hdgsjgj.cn/bcjs/img/background.png'; var BG = {imgs: bg,width: 480,height: 852, }; // 創建生成背景圖片的構造函數 function Bg(config) { // 參數為BG對象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() { //分別繪制了兩張背景圖 ctx.drawImage(this.imgs, this.x1, this.y1); ctx.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; }} }; // 創建背景圖片對象 var sky = new Bg(BG); // 生成游戲名文字 var logo = new Image(); logo.src = 'http://www.hdgsjgj.cn/bcjs/img/start.png'; // 游戲加載過程的4張圖片存入一個數組中 var loadings = []; loadings[0] = new Image(); loadings[0].src = 'http://www.hdgsjgj.cn/bcjs/img/game_loading1.png'; loadings[1] = new Image(); loadings[1].src = 'http://www.hdgsjgj.cn/bcjs/img/game_loading2.png'; loadings[2] = new Image(); loadings[2].src = 'http://www.hdgsjgj.cn/bcjs/img/game_loading3.png'; loadings[3] = new Image(); loadings[3].src = 'http://www.hdgsjgj.cn/bcjs/img/game_loading4.png'; var LOADING = {imges: loadings,length: loadings.length,width: 186,height: 38, }; // 構造函數 function Loading(config) {this.imges = config.imges;this.length = config.length;this.width = config.width;this.height = config.height;this.startIndex = 0; // 用于判斷需要顯示的圖片是哪個// 繪制方法this.paint = function() { ctx.drawImage(this.imges[this.startIndex], 0, height - this.height)};this.time = 0; // 加載時圖片切換速度// 圖片切換方法this.step = function() { this.time++; if (this.time % 4 === 0) {this.startIndex++; } if (this.startIndex === this.length) {// 加載階段結束,進入游戲階段state = running; }} }; // 創建加載階段的對象var loading = new Loading(LOADING);

在制作我方飛機

// 我方飛機 var heros = []; heros[0] = new Image(); heros[0].src = 'http://www.hdgsjgj.cn/bcjs/img/hero1.png'; heros[1] = new Image(); heros[1].src = 'http://www.hdgsjgj.cn/bcjs/img/hero2.png'; heros[2] = new Image(); heros[2].src = 'http://www.hdgsjgj.cn/bcjs/img/hero_blowup_n1.png'; heros[3] = new Image(); heros[3].src = 'http://www.hdgsjgj.cn/bcjs/img/hero_blowup_n2.png'; heros[4] = new Image(); heros[4].src = 'http://www.hdgsjgj.cn/bcjs/img/hero_blowup_n3.png'; heros[5] = new Image(); heros[5].src = 'http://www.hdgsjgj.cn/bcjs/img/hero_blowup_n4.png'; var HEROS = {imgs: heros,length: heros.length,width: 99,height: 124,frame: 2 }; // 我方飛機的構造函數 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 - this.height;// 定義飛機撞擊的標志,表示飛機沒有被撞擊this.down = false;// 定義飛機是否爆破完成,表示飛機還沒有完全爆炸this.candel = false;// 繪制方法this.paint = function() { ctx.drawImage(this.imgs[this.startIndex], this.x, this.y)};// 我方飛機運動方法this.step = function() { if (!this.down) { // 飛機正常狀態if (this.startIndex === 0) { this.startIndex = 1;} else { this.startIndex = 0} } else { // 爆炸狀態this.startIndex++;if (this.startIndex === this.length) { // 判斷是否炸完了 // 炸完了,命-1 life--; if (life === 0) { // 判斷是否掛了state = gameover;this.startIndex = this.length - 1; } else { // 重新開始新生命hero = new Hero(HEROS) }} }};// 我方飛機碰撞this.bang = function() { this.down = true;};

繪制子彈狀態

var bullet = new Image(); bullet.src = 'http://www.hdgsjgj.cn/bcjs/img/bullet1.png'; // 初始化 var BULLETS = {imgs: bullet,width: 9,height: 21, }; // 創建子彈的構造函數 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() { ctx.drawImage(this.imgs, this.x, this.y)};// 運動方法this.step = function() { this.y -= 10;};this.candel = false; // 用于判斷子彈是否碰撞// 子彈碰撞方法this.bang = function() { this.candel = true;} }; // 所有new的子彈對象放到一個數組 var bullets = []; // 遍歷繪制子彈 function bulletdPaint() {for (var i = 0; i < bullets.length; i++) { bullets[i].paint();} }; // 遍歷調用子彈的運動; function bulletdStep() {for (var i = 0; i < bullets.length; i++) { bullets[i].step();} }; // 子彈的刪除函數 function bulletDel() {// 碰撞的時候刪除子彈// 超出畫布的高度,即負的子彈的高度for (var i = 0; i < bullets.length; i++) { if (bullets[i].candel || bullets[i].y < -bullets[i].height) {bullets.splice(i, 1) }} };

子彈跟隨飛機的移動而移動

// 子彈發射this.time = 0; // 設計速度初始為0this.shoot = function() { this.time++; if (this.time % 2 === 0) { // 每2步移動射擊一次bullets.push(new Bullet(BULLETS)) }}; }; // 創建我方飛機的對象實例 var hero = new Hero(HEROS); // 鼠標移動事件 canvas.onmousemove = function(event) {// console.log('onmousemove');var event = event || window.event;if (state == running) { //判斷當前游戲狀態 //把獲取到的頁面中的鼠標橫坐標的值賦給飛機的橫坐標(位置) hero.x = event.offsetX - hero.width / 2; //把獲取到的頁面中的鼠標縱坐標的值賦給飛機的縱坐標(位置) hero.y = event.offsetY - hero.height / 2;} };

繪制敵方飛機

// 敵方飛機的繪制 var enemy1 = []; //小飛機 enemy1[0] = new Image(); enemy1[0].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy1.png'; enemy1[1] = new Image(); enemy1[1].src = ’img/enemy1_down1.png’; enemy1[2] = new Image(); enemy1[2].src = ’img/enemy1_down2.png’; enemy1[3] = new Image(); enemy1[3].src = ’img/enemy1_down3.png’; enemy1[4] = new Image(); enemy1[4].src = ’img/enemy1_down4.png’; var enemy2 = []; //中飛機 enemy2[0] = new Image(); enemy2[0].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy2.png'; enemy2[1] = new Image(); enemy2[1].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy2_down1.png'; enemy2[2] = new Image(); enemy2[2].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy2_down2.png'; enemy2[3] = new Image(); enemy2[3].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy2_down3.png'; enemy2[4] = new Image(); enemy2[4].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy2_down4.png'; var enemy3 = []; //大飛機 enemy3[0] = new Image(); enemy3[0].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_n1.png'; enemy3[1] = new Image(); enemy3[1].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_n2.png'; enemy3[2] = new Image(); enemy3[2].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_down1.png'; enemy3[3] = new Image(); enemy3[3].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_down2.png'; enemy3[4] = new Image(); enemy3[4].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_down3.png'; enemy3[5] = new Image(); enemy3[5].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_down4.png'; enemy3[6] = new Image(); enemy3[6].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_down5.png'; enemy3[7] = new Image(); enemy3[7].src = 'http://www.hdgsjgj.cn/bcjs/img/enemy3_down6.png'; // 初始化數據 var ENEMY1 = {imgs: enemy1,length: enemy1.length,width: 57,height: 51,type: 1,frame: 2,life: 1,score: 1, }; var ENEMY2 = {imgs: enemy2,length: enemy2.length,width: 69,height: 95,type: 2,frame: 2,life: 5,score: 5, }; var ENEMY3 = {imgs: enemy3,length: enemy3.length,width: 165,height: 261,type: 3,frame: 2,life: 15,score: 20, }; // 敵方飛機的構造函數 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.x = Math.random() * (width - this.width);this.y = -this.height;this.startIndex = 0; // 用于判斷的下標this.down = false; // 用于判斷是否碰撞this.candel = false; // 用于判斷是否爆炸完成//繪制方法this.paint = function() { ctx.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.checkHit = function(wo) { //判斷四個邊 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;};//敵方飛機碰撞后this.bang = function() { this.life--; if (this.life === 0) {this.down = true;score += this.score; }} }; // 數組存放敵方飛機 var enemise = []; // 往敵方飛機數組中添加數據 function enterEnemise() {var rand = Math.floor(Math.random() * 100)if (rand < 10) { // 添加小飛機 enemise.push(new Enemy(ENEMY1));} else if (rand < 55 && rand > 50) { // 添加中飛機 enemise.push(new Enemy(ENEMY2));} else if (rand === 88) { // 添加大飛機 if (enemise[0].type !== 3 && enemise.length > 0) {enemise.splice(0, 0, new Enemy(ENEMY3)); }} }; // 繪制敵方飛機函數 function enemyPaint() {for (var i = 0; i < enemise.length; i++) { enemise[i].paint();} }; // 敵方飛機的運動函數 function enemyStep() {for (var i = 0; i < enemise.length; i++) { enemise[i].step();} }; // 刪除敵方飛機函數 function delenemy() {for (var i = 0; i < enemise.length; i++) { // console.log(enemise[i].candel) if (enemise[i].y > height || enemise[i].candel) {enemise.splice(i, 1) }} }; // 碰撞以后的函數 function hitEnemise() {for (var i = 0; i < enemise.length; i++) { // 如果我放飛機撞到了敵方飛機以后 if (enemise[i].checkHit(hero)) {// 敵方飛機碰撞后,碰撞狀態改變enemise[i].bang();// 我方飛機碰撞后,碰撞狀態改變hero.bang(); }; // 子彈碰到敵方飛機 for (var j = 0; j < bullets.length; j++) {if (enemise[i].checkHit(bullets[j])) { enemise[i].bang(); // 子彈的碰撞后,碰撞狀態改變 bullets[j].bang();} }} };

最后的收尾階段

// 繪制分數和生命值 function scoreText() {ctx.font = '30px bold'ctx.fillText('score:' + score, 10, 30);ctx.fillText('life:' + life, 300, 30); }; // 游戲暫停的階段 canvas.onmouseout = function() {if (state === running) { state = pause;} }; // 調用畫布的鼠標移入事件 canvas.onmouseover = function() {if (state === pause) { state = running;} }; // 暫停圖片 var pause = new Image() pause.src = 'http://www.hdgsjgj.cn/bcjs/img/game_pause_nor.png'; // 游戲結束 function gameoverfn() {ctx.font = '50px bold'ctx.fillText('GAME OVER !!!', 80, 300);ctx.fillText('ONCE MORE !!!', 80, 400); }; // 畫布點擊事件 canvas.addEventListener('click', function(e) {p = getEventPosition(e);// 點擊畫布時,判斷游戲是否開始if (state === start) { state = starting;}console.log(123);// 重新開始游戲有問題???if (state === gameover) { if (p.y >= 350 && p.y < 450) {console.log(’你點擊了ONCE MORE !!!’);state = running; }} }); function getEventPosition(e) {var x, y;if (e.layerX || ev.layerX === 0) { x = e.layerX; y = e.layerY;} else if (e.offsetX || ev.offsetX === 0) { x = e.offsetX; y = e.offsetY;}return { x: x, y: y}; };

后面就是基本的每個階段的調用問題叻

setInterval(function() {//背景圖片無論在哪個狀態都有背景圖片以及它的動態效果sky.paint(); // 繪制背景sky.step(); // 背景動畫if (state === start) { // 第一階段 ctx.drawImage(logo, 35, 0)} else if (state === starting) { // 第二階段 loading.paint(); // 繪制背景 loading.step(); // 背景動畫} else if (state === running) { // 第三狀態 // 繪制我放飛機 hero.paint(); // 我方飛機的運動 hero.step(); // 我方飛機的射擊方法 hero.shoot(); // 子彈的繪制 bulletdPaint(); // 子彈的運動 bulletdStep(); // 子彈的刪除 bulletDel(); // 創建敵方飛機 enterEnemise(); // 繪制敵方飛機 enemyPaint(); // 繪制敵方飛機的運動 enemyStep(); // 刪除敵方飛機 delenemy(); // 判斷是否撞擊 hitEnemise(); // 繪制分數和生命值 scoreText()} else if (state === pause) { // 第四狀態 sky.paint(); // 繪制背景 sky.step(); // 背景動畫 // 繪制我放飛機 hero.paint(); // 子彈的繪制 bulletdPaint(); // 繪制敵方飛機 enemyPaint(); // 繪制分數和生命值 scoreText(); ctx.drawImage(pause, 220, 300)} else if (state === gameover) { // 第五狀態 sky.paint(); // 繪制背景 sky.step(); // 背景動畫 hero.paint(); // 子彈的繪制 bulletdPaint(); // 繪制敵方飛機 enemyPaint(); // 繪制分數和生命值 scoreText(); // 游戲結束 gameoverfn();} }, 10)})()

這個也就是飛機大戰的全部源碼了,僅供參考。

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

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | 管家婆-管家婆软件-管家婆辉煌-管家婆进销存-管家婆工贸ERP | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 黑龙江京科脑康医院-哈尔滨精神病医院哪家好_哈尔滨精神科医院排名_黑龙江精神心理病专科医院 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 福建自考_福建自学考试网 | 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | 环球电气之家-中国专业电气电子产品行业服务网站! | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 美名宝起名网-在线宝宝、公司、起名平台 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 物联网卡_物联网卡购买平台_移动物联网卡办理_移动联通电信流量卡通信模组采购平台? | 上海噪音治理公司-专业隔音降噪公司-中广通环保 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 高温高压釜(氢化反应釜)百科 | 华溶溶出仪-Memmert稳定箱-上海协烁仪器科技有限公司 | 高速龙门架厂家_监控杆_多功能灯杆_信号灯杆_锂电池太阳能路灯-鑫世源照明 | 广东佛电电器有限公司|防雷开关|故障电弧断路器|智能量测断路器 广东西屋电气有限公司-广东西屋电气有限公司 | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 | Q361F全焊接球阀,200X减压稳压阀,ZJHP气动单座调节阀-上海戎钛 | 开云(中国)Kaiyun·官方网站 - 登录入口 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 合金耐磨锤头_破碎机锤头_郑州市德勤建材有限公司 | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 英语词典_成语词典_日语词典_法语词典_在线词典网 | 智慧旅游_智慧景区_微景通-智慧旅游景区解决方案提供商 | 衬氟旋塞阀-卡套旋塞阀-中升阀门首页 | 排烟防火阀-消防排烟风机-正压送风口-厂家-价格-哪家好-德州鑫港旺通风设备有限公司 | 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 全钢实验台,实验室工作台厂家-无锡市辰之航装饰材料有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 浙江自考_浙江自学考试网 | 作文导航网_作文之家_满分作文_优秀作文_作文大全_作文素材_最新作文分享发布平台 | HDPE储罐_厂家-山东九州阿丽贝防腐设备 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 |