设为首页 加入收藏

TOP

原生JavaScript开发仿微信打飞机小游戏(二)
2014-11-24 01:40:27 来源: 作者: 【 】 浏览:2
Tags:原生 JavaScript 开发 飞机 小游戏
window.event){// IE
keynum = e.keyCode
}else if(e.which) {// Netscape/Firefox/Opera
keynum = e.which
}

switch(keynum){
case left:
selfPlane.move(-Main.keyMove,0);
break;
case up:
selfPlane.move(0,-Main.keyMove);
break;
case right:
selfPlane.move(Main.keyMove,0);
break;
case down:
selfPlane.move(0,Main.keyMove);
break;

default:
break;
}

//console.log(keynum);
}

}


}


entity.js:


//自身的对象
var selfPlane={
x:0,
y:0,
score:0,
e:null,
init:function(){
this.x=(Util.windowWidth-Util.selfPlaneElement.width)/2;//相对于父窗体的x偏移(css:left)
this.y=Util.windowHeight-Util.selfPlaneElement.height;//相对于父窗体的y偏移(css:top)
this.e=Util.selfPlaneElement;//对应的dom元素
Util.selfPlaneElement.style.left=this.x+"px";
Util.selfPlaneElement.style.top=this.y+"px";
Util.parentElement.appendChild(this.e);
},
move:function(moveX,moveY){
var x=this.x+moveX;
var y=this.y+moveY;

if(x<0-this.e.width/2||x>Util.windowWidth-this.e.width/2){
return ;
}
if(y<0-this.e.height/2||y>Util.windowHeight-this.e.height/2){
return ;
}
this.x=x;
this.y=y;

this.e.style.left=this.x+"px";
this.e.style.top=this.y+"px";
},
moveTo:function(x,y){

if(x<0-this.e.width/2||x>Util.windowWidth-this.e.width/2){
return ;
}
if(y<0-this.e.height/2||y>Util.windowHeight-this.e.height/2){
return ;
}
this.x=x;
this.y=y;

this.e.style.left=this.x+"px";
this.e.style.top=this.y+"px";
}
}


bullet.prototype.move=function(moveX,moveY){
this.x+=moveX;
this.y+=moveY;
this.e.style.left=this.x+"px";
this.e.style.top=this.y+"px";
}
bullet.prototype.moveTo=function(X,Y){
this.x=X;
this.y=Y;
this.e.style.left=this.x+"px";
this.e.style.top=this.y+"px";
}



//子弹恢复
bullet.prototype.restore=function(){
this.x=Main.self
this.y=-Math.random()*Util.windowHeight-Util.enemyPlaneElement.height;
this.speed=2+Math.random()*4;
this.e.style.left=this.x+"px";
this.e.style.top=this.y+"px";
}
//子弹工厂
var bulletFactory={
bullets:[],
creatBullet:function(n){
for(var i=0;i var b=new bullet(0,-Util.bulletElement.height,20);
this.bullets.push(b);
}
}
}


util.js:


// java script Document
var Util={
windowWidth:350,
windowHeight:480,
selfPlaneElement:null,
enemyPlaneElement:null,
bulletElement:null,
parentElement:null,
scoreSpan:null,
g:function(id){
return document.getElementById(id);
},

init:function(){
this.parentElement=this.g("parent");

this.selfPlaneElement=this._loadImg("images/self.gif");

this.enemyPlaneElement=this._loadImg("images/boss.gif");

this.bulletElement=this._loadImg("images/bullet.jpg");

this.scoreSpan=this.g("score");
},

_loadImg:function(src){
var e=document.createElement("img");
e.style.position="absolute";
e.src=src;
return e;
}
}


原生java script开发仿微信打飞机小游戏源码下载地址


具体下载目录在 /2014年资料/2月/8日/原生java script开发仿微信打飞机小游戏


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Shell脚本学习之expect命令 下一篇Python 程序员必知必会的开发者工..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: