- (function(W){
- "use strict"
- var planWidth = 24,
- planHeight = 24,
- missleWidth = 70,
- missleHeight = 70,
- boomWidth = 60;
- //精灵类
- W.Sprite = function(name , painter , behaviors , args){
- if(name !== undefined) this.name = name;
- if(painter !== undefined) this.painter = painter;
- this.top = 0;
- this.left = 0;
- this.width = 0;
- this.height = 0;
- this.velocityX = 3;
- this.velocityY = 2;
- this.visible = true;
- this.animating = false;
- this.behaviors = behaviors;
- this.rotateAngle = 0;
- this.blood = 50;
- this.fullBlood = 50;
- if(name==="plan"){
- this.rotateSpeed = 0.05;
- this.rotateLeft = false;
- this.rotateRight = false;
- this.fire = false;
- this.firePerFrame = 10;
- this.fireLevel = 1;
- }else if(name==="star"){
- this.width = Math.random()*2;
- this.speed = 1*this.width/2;
- this.lightLength = 5;
- this.cacheCanvas = document.createElement("canvas");
- this.cacheCtx = this.cacheCanvas.getContext('2d');
- this.cacheCanvas.width = this.width+this.lightLength*2;
- this.cacheCanvas.height = this.width+this.lightLength*2;
- this.painter.cache(this);
- }else if(name==="badPlan"){
- this.badKind = 1;
- this.speed = 2;
- this.rotateAngle = Math.PI;
- }else if(name==="missle"){
- this.width = missleWidth;
- }else if(name==="boom"){
- this.width = boomWidth;
- }else if(name==="food"){
- this.width = 40;
- this.speed = 3;
- this.kind = "LevelUP"
- }
- this.toLeft = false;
- this.toTop = false;
- this.toRight = false;
- this.toBottom = false;
- this.outArcRadius = Math.sqrt((this.width/2*this.width/2)*2);
- if(args){
- for(var arg in args){
- this[arg] = args[arg];
- }
- }
- }
- Sprite.prototype = {
- constructor:Sprite,
- paint:function(){
- if(this.name==="badPlan"){this.update();}
- if(this.painter !== undefined && this.visible){
- if(this.name!=="badPlan") {
- this.update();
- }
- if(this.name==="plan"||this.name==="missle"||this.name==="badPlan"){
- ctx.save();
- ctx.translate(this.left , this.top);
- ctx.rotate(this.rotateAngle);
- this.painter.paint(this);
- ctx.restore();
- }else {
- this.painter.paint(this);
- }
- }
- },
- update:function(time){
- if(this.behaviors){
- for(var i=0;i<this.behaviors.length;i++){
- this.behaviors[i].execute(this,time);
- }
- }
- }
- }
- // 精灵表绘制器
- W.SpriteSheetPainter = function(cells , isloop , endCallback , spritesheet){
- this.cells = cells || [];
- this.cellIndex = 0;
- this.dateCount = null;
- this.isloop = isloop;
- this.endCallback = endCallback;
- this.spritesheet = spritesheet;
- }
- SpriteSheetPainter.prototype = {
- advance:function(){
- this.cellIndex = this.isloop?(this.cellIndex===this.cells.length-1?0:this.cellIndex+1):(this.cellIndex+1);
- },
- paint:function(sprite){
- if(this.dateCount===null){
- this.dateCount = new Date();
- }else {
- var newd = new Date();
- var tc = newd-this.dateCount;
- if(tc>40){
- this.advance();
- this.dateCount = newd;
- }
- }
- if(this.cellIndex<this.cells.length || this.isloop){
- var cell = this.cells[this.cellIndex];
- ctx.drawImage(this.spritesheet , cell.x , cell.y , cell.w , cell.h , sprite.left-sprite.width/2 , sprite.top-sprite.width/2 , cell.w , cell.h);
- } else if(this.endCallback){
- this.endCallback.call(sprite);
- this.cellIndex = 0;
- }
- }
- }
- //特制飞机精灵表绘制器
- W.controllSpriteSheetPainter = function(cells , spritesheet){
- this.cells = cells || [];
- this.cellIndex = 0;
- this.dateCount = null;
- this.isActive = false;
- this.derection = true;
- this.spritesheet = spritesheet;
- }
- controllSpriteSheetPainter.prototype = {
- advance:function(){
- if(this.isActive){
- this.cellIndex++;
- if(this.cellIndex === this.cells.length){
- this.cellIndex = 0;
- this.isActive = false;
- }
- }
- },
- paint:function(sprite){
- if(this.dateCount===null){
- this.dateCount = new Date();
- }else {
- var newd = new Date();
- var tc = newd-this.dateCount;
- if(tc>sprite.firePerFrame){
- this.advance();
- this.dateCount = newd;
- }
- }
- var cell = this.cells[this.cellIndex];
- ctx.drawImage(this.spritesheet , cell.x , cell.y , cell.w , cell.h , -planWidth/2 , -planHeight/2 , cell.w , cell.h);
- }
- }
- W.planBehavior = [
- {execute:function(sprite,time){
- if(sprite.toTop){
- sprite.top = sprite.top
- }
- if(sprite.toLeft){
- sprite.left = sprite.left
- }
- if(sprite.toRight){
- sprite.left = sprite.left>canvas.width-planWidth/2? sprite.left : sprite.left+sprite.velocityX;
- }
- if(sprite.toBottom){
- sprite.top = sprite.top>canvas.height-planHeight/2? sprite.top : sprite.top+sprite.velocityY;
- }
- if(sprite.rotateLeft){
- sprite.rotateAngle -= sprite.rotateSpeed;
- }
- if(sprite.rotateRight){
- sprite.rotateAngle += sprite.rotateSpeed;
- }
- if(sprite.fire&&!sprite.painter.isActive){
- sprite.painter.isActive = true;
- this.shot(sprite);
- }
- },
- shot:function(sprite){
- this.addMissle(sprite , sprite.rotateAngle);
- var missleAngle = 0.1
- for(var i=1;i
- this.addMissle(sprite , sprite.rotateAngle-i*missleAngle);
- this.addMissle(sprite , sprite.rotateAngle+i*missleAngle);
- }
- var audio = document.getElementsByTagName("audio");
- for(var i=0;i
- console.log(audio[i].paused)
- if(audio[i].src.indexOf("shot")>=0&&audio[i].paused){
- audio[i].play();
- break;
- }
- }
- },
- addMissle:function(sprite , angle){
- for(var j=0;j
- if(!missles[j].visible){
- missles[j].left = sprite.left;
- missles[j].top = sprite.top;
- missles[j].rotateAngle = angle;
- var missleSpeed = 20;
- missles[j].velocityX = missleSpeed*Math.sin(-missles[j].rotateAngle);
- missles[j].velocityY = missleSpeed*Math.cos(-missles[j].rotateAngle);
- missles[j].visible = true;
- break;
- }
- }
- }
- }
- ]
- W.starBehavior = [
- {execute:function(sprite,time){
- if(sprite.top > canvas.height){
- sprite.left = Math.random()*canvas.width;
- sprite.top = Math.random()*canvas.height - canvas.height;
- }
- sprite.top += sprite.speed;
- }}
- ]
- W.starPainter = {
- paint:function(sprite){
- ctx.drawImage(sprite.cacheCanvas , sprite.left-sprite.width/2-sprite.lightLength , sprite.top-sprite.width/2-sprite.lightLength)
- },
- cache:function(sprite){
- sprite.cacheCtx.save();
- var opacity = 0.5,addopa = 1/sprite.lightLength;
- sprite.cacheCtx.fillStyle = "rgba(255,255,255,0.8)";
- sprite.cacheCtx.beginPath();
- sprite.cacheCtx.arc(sprite.width/2+sprite.lightLength , sprite.width/2+sprite.lightLength , sprite.width/2 , 0 , 2*Math.PI);
- sprite.cacheCtx.fill();
- for(var i=1;i<=sprite.lightLength;i+=2){
- opacity-=addopa;
- sprite.cacheCtx.fillStyle = "rgba(255,255,255,"+opacity+")";
- sprite.cacheCtx.beginPath();
- sprite.cacheCtx.arc(sprite.width/2+sprite.lightLength , sprite.width/2+sprite.lightLength , sprite.width/2+i , 0 , 2*Math.PI);
- sprite.cacheCtx.fill();
- }
- }
- }
- W.foodBehavior = [
- {execute:function(sprite,time){
- sprite.top += sprite.speed;
- if(sprite.top > canvas.height+sprite.width){
- sprite.visible = false;
- }
- }}
- ]
- W.foodPainter = {
- paint:function(sprite){
- ctx.fillStyle = "rgba("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+",1)"
- ctx.f/span>"15px 微软雅黑"
- ctx.textAlign = "center";
- ctx.textBaseline = "middle";
- ctx.fillText(sprite.kind , sprite.left , sprite.top);
- }
- }
- W.missleBehavior = [{
- execute:function(sprite,time){
- sprite.left -= sprite.velocityX;
- sprite.top -= sprite.velocityY;
- if(sprite.left<-missleWidth/2||sprite.top<-missleHeight/2||sprite.left>canvas.width+missleWidth/2||sprite.top<-missleHeight/2){
- sprite.visible = false;
- }
- }
- }];
- W.misslePainter = {
- paint:function(sprite){
- var img = new Image();
- img.src="../planGame/image/plasma.png"
- ctx.drawImage(img , -missleWidth/2+1 , -missleHeight/2+1 , missleWidth , missleHeight);
- }
- }
- W.badPlanBehavior = [{
- execute:function(sprite,time){
- if(sprite.top > canvas.height || !sprite.visible){
- var random = Math.random();
- if(point>=200&&point<400){
- sprite.fullBlood = 150;
- if(random<0.1){
- sprite.badKind = 2;
- sprite.fullBlood = 250;
- }
- }else if(point>=400&&point<600){
- sprite.fullBlood = 250;
- if(random<0.2){
- sprite.badKind = 2;
- sprite.fullBlood = 400;
- }
- if(random<0.1){
- sprite.badKind = 3;
- sprite.fullBlood = 600;
- }
- }else if(point>=600){
- sprite.fullBlood = 500;
- if(random<0.4){
- sprite.badKind = 2;
- sprite.fullBlood = 700;
- }
- if(random<0.2){
- sprite.badKind = 3;
- sprite.fullBlood = 1000;
- }
- }
- sprite.visible = true;
- sprite.blood = sprite.fullBlood;
- sprite.left = Math.random()*(canvas.width-2*planWidth)+planWidth;
- sprite.top = Math.random()*canvas.height - canvas.height;
- }
- sprite.top += sprite.speed;
- },
- shot:function(sprite){
- this.addMissle(sprite , sprite.rotateAngle);
- var missleAngle = 0.1
- for(var i=1;i
- this.addMissle(sprite , sprite.rotateAngle-i*missleAngle);
- this.addMissle(sprite , sprite.rotateAngle+i*missleAngle);
- }
- },
- addMissle:function(sprite , angle){
- for(var j=0;j
- if(!missles[j].visible){
- missles[j].left = sprite.left;
- missles[j].top = sprite.top;
- missles[j].rotateAngle = angle;
- var missleSpeed = 20;
- missles[j].velocityX = missleSpeed*Math.sin(-missles[j].rotateAngle);
- missles[j].velocityY = missleSpeed*Math.cos(-missles[j].rotateAngle);
- missles[j].visible = true;
- break;
- }
- }
- }
- }];
- W.badPlanPainter = {
- paint:function(sprite){
- var img = new Image();
- img.src="../planGame/image/ship.png"
- switch(sprite.badKind){
- case 1:ctx.drawImage(img , 96 , 0 , planWidth , planWidth , -planWidth/2 , -planHeight/2 , planWidth , planWidth);
- break;
- case 2:ctx.drawImage(img , 120 , 0 , planWidth , planWidth , -planWidth/2 , -planHeight/2 , planWidth , planWidth);
- break;
- case 3:ctx.drawImage(img , 144 , 0 , planWidth , planWidth , -planWidth/2 , -planHeight/2 , planWidth , planWidth);
- break;
- }
- ctx.strokeStyle = "#FFF";
- ctx.fillStyle = "#F00";
- var bloodHeight = 1;
- ctx.strokeRect(-planWidth/2-1 , planHeight+bloodHeight+3 , planWidth+2 , bloodHeight+2);
- ctx.fillRect(planWidth/2-planWidth*sprite.blood/sprite.fullBlood , planHeight+bloodHeight+3 , planWidth*sprite.blood/sprite.fullBlood , bloodHeight);
- }
- }
- W.planSize = function(){
- return {
- w:planWidth,
- h:planHeight
- }
- }
- })(window);
这些绘制方法之类的都相对比较简单。
主要说一下飞机的运动以及对象数量的控制,飞机怎么运动?毫无疑问,通过键盘控制它运动,可能很多人就会想到通过keydown这个方法按下的时候通过判断keyCode来让飞机持续运动。但是有个问题,keydown事件不支持多键按下,也就是说,当你按下X键时,keyCode是88,与此同时你按下方向键后,keyCode会瞬间变成37,也就是说,如果你单纯的想靠keydown来控制飞机运动,飞机就只能做一件事,要么只可以往某个方向移动,要么只会开枪。
所以,我们要通过keydown和keyup来实现飞机的运动,原理很容易理解:当我们按下往左的方向键时,我们给飞机一个往左的状态,也就是让飞机的toLeft属性为true,而在动画循环中,判断飞机的状态,如果toLeft为true则飞机的x值不停地减少,飞机也就会不停地往左移动,然后当我们抬起手指时触发keyup事件,我们就再keyup事件中解除飞机往左的状态。飞机也就停止往左移动了。其他状态也一样的原理,这样写的话,就能够让飞机多种状态于一生了。可以同时开枪同时到处跑了。
实现的代码如下:
XML/HTML Code复制内容到剪贴板
- //keydown/keyup事件的绑定
- window.onkeydown = function(event){
- switch(event.keyCode){
- case 88:myplan.fire = true;
- break;
- case 90:myplan.rotateLeft=true;
- break;
- case 67:myplan.rotateRight=true;
- break;
- case 37:myplan.toLeft = true;
- break;
- case 38:myplan.toTop = true;
- break;
- case 39:myplan.toRight = true;
- break;
- case 40:myplan.toBottom = true;
- break;
- }
- }
- window.onkeyup = function(event){
- switch(event.keyCode){
- case 88:myplan.fire = false;
- break;
- case 90:myplan.rotateLeft=false;
- break;
- case 67:myplan.rotateRight=false;
- break;
- case 37:myplan.toLeft = false;
- break;
- case 38:myplan.toTop = false;
- break;
- case 39:myplan.toRight = false;
- break;
- case 40:myplan.toBottom = false;
- break;
- }
- }
- //飞机每一帧的状态更新处理代码
- execute:function(sprite,time){
- if(sprite.toTop){
- spritesprite.top = sprite.top<planHeight/2? sprite.top : sprite.top-sprite.velocityY;
- }
- if(sprite.toLeft){
- spritesprite.left = sprite.left<planWidth/2? sprite.left : sprite.left-sprite.velocityX;
- }
- if(sprite.toRight){
- spritesprite.left = sprite.left>canvas.width-planWidth/2? sprite.left : sprite.left+sprite.velocityX;
- }
- if(sprite.toBottom){
- spritesprite.top = sprite.top>canvas.height-planHeight/2? sprite.top : sprite.top+sprite.velocityY;
- }
- if(sprite.rotateLeft){
- sprite.rotateAngle -= sprite.rotateSpeed;
- }
- if(sprite.rotateRight){
- sprite.rotateAngle += sprite.rotateSpeed;
- }
- if(sprite.fire&&!sprite.painter.isActive){
- sprite.painter.isActive = true;
- this.shot(sprite);
- }
就是如此简单。
然后说下对象控制,打飞机游戏,会发射大量子弹,产生大量对象,包括爆炸啊,飞机啊,子弹等,如果不停地进行对象的生成和销毁,会让浏览器的负荷变得很大,运行了一段时间后就会卡出翔了。所以,我们要用可以循环利用的对象来解决这个问题,不进行对象的销毁,对所有对象进行保存,循环利用。
我的做法就是,在游戏初始化的时候,直接生成一定数量的对象,存放在数组里面。当我们需要一个对象的时候,就从里面取,当用完后,再放回数组里面。数组里的所有对象都有一个属性,visible,代表对象当前是否可用。