JavaScript模拟在雷达上用导弹打下敌机,程序员也过过当炮手的瘾!(1)

最后

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

最后写上我自己一直喜欢的一句名言:世界上只有一种真正的英雄主义就是在认清生活真相之后仍然热爱它

实现思路


1.首先实现一个雷达效果,参考前面的文章(https://blog.csdn.net/dkm123456/article/details/114312198);

2.点击扫描按钮,雷达旋转、同时创建敌机对象,并让敌机运动起来;

3.点击锁定按钮,会在敌机的位置创建锁定的图形,达到锁定的效果(因为雷达和导弹都用了小尾巴效果,所以锁定目标就用了一个新画布);

4.点击发射按钮,创建导弹对象,导弹根据敌机的位置,不停的调整位置,向目标运动,当到达目标一定范围就判定为命中目标;

5.命中目标后产生爆炸效果,并弹出目标已摧毁,同时清除相关对象,比如导弹、敌机、锁定对象等。

创建敌机对象(雷达上运动的小球)


构造函数(圆或者叫球)

//构造函数

function Ball(o){

this.x=0,//圆心X坐标

this.y=0,//圆心Y坐标

this.r=0,//半径

this.startAngle=0,//开始角度

this.endAngle=0,//结束角度

this.anticlockwise=false;//顺时针,逆时针方向指定

this.stroke=false;//是否描边

this.fill=false;//是否填充

this.scaleX=1;//缩放X比例

this.scaleY=1;//缩放Y比例

this.rotate=0;

this.init(o);

}

//初始化

Ball.prototype.init=function(o){

for(var key in o){

this[key]=o[key];

}

}

//绘制

Ball.prototype.render=function(context){

var ctx=context;//获取上下文

ctx.save();

ctx.beginPath();

ctx.translate(this.x,this.y);

if(this.fill){

ctx.moveTo(0,0);

}

//ctx.moveTo(this.x,this.y);

ctx.scale(this.scaleX,this.scaleY);//设定缩放

ctx.arc(0,0,this.r,this.startAngle,this.endAngle);//画圆

if(this.lineWidth){//线宽

ctx.lineWidth=this.lineWidth;

}

if(this.fill){//是否填充

this.fillStyle?(ctx.fillStyle=this.fillStyle):null;

ctx.fill();

}

if(this.stroke){//是否描边

this.strokeStyle?(ctx.strokeStyle=this.strokeStyle):null;

ctx.stroke();

}

ctx.restore();

return this;

}

创建

//创建敌机

Radar.prototype.addEnemy=function(){

//绘制敌机

var enemy = new Ball({

//x:0,y:0,

x:_.getRandom(30,450),//圆心X坐标

y:_.getRandom(30,450),//圆心X坐标

r:3,//半径

startAngle:0,//开始角度

endAngle:2*Math.PI,//结束角度

fill:true,//是否填充

fillStyle:‘red’//填充的样式

})

this.renderArr.push(enemy);

this.enemy=enemy;

}

加入运动

if(!this.anglePi){

this.anglePi=Math.atan2(enemy.y-250,enemy.x-250);

}

console.log(this.anglePi)

var dis=1;

if(this.anglePi<1/2*Math.PI && this.anglePi>0){//0度-90度

dis=-1;

}else if(this.anglePi<-1/2*Math.PI && this.anglePi>-Math.PI){//-180度-90度

dis=-1;

}else if(this.anglePi>-1/2*Math.PI && this.anglePi<0){//-90度-0度

dis=-1;

}else{//90度-180度

dis=1;

}

var disX=Math.cos(this.anglePi)this.enemySpeeddis;

var disY=Math.sin(this.anglePi)this.enemySpeeddis;

//更新敌机的位置

enemy.x+=disX;

enemy.y+=disY;

效果

锁定(绘制几个圆和几个线段来指示被锁定)


//锁定目标(绘制几个圆和几个线段来指示被锁定)

Radar.prototype.aimWeapon=function() {

var aimWeaponDest = this.aimWeaponDest;

if(aimWeaponDest && aimWeaponDest.length>0){

return ;

}

var enemy = this.enemy;

if(!enemy) return ;

var aimWeaponDestArr=[];

//根据敌机来创建一个圆

var aimWeapon = new Ball({

x:enemy.x,//圆心X坐标

y:enemy.y,//圆心X坐标

r:6,//半径

startAngle:0,//开始角度

endAngle:2*Math.PI,//结束角度

stroke:true,//是否描边

strokeStyle:‘red’//样式

})

this.renderArr2.push(aimWeapon);

aimWeaponDestArr.push(aimWeapon);

aimWeapon = new Ball({

x:enemy.x,//圆心X坐标

y:enemy.y,//圆心X坐标

r:8,//半径

startAngle:0,//开始角度

endAngle:2*Math.PI,//结束角度

stroke:true,//是否描边

strokeStyle:‘red’//样式

})

this.renderArr2.push(aimWeapon);

aimWeaponDestArr.push(aimWeapon);

aimWeapon = new Ball({

x:enemy.x,//圆心X坐标

y:enemy.y,//圆心X坐标

r:10,//半径

startAngle:0,//开始角度

endAngle:2*Math.PI,//结束角度

stroke:true,//是否描边

strokeStyle:‘red’//样式

})

this.renderArr2.push(aimWeapon);

aimWeaponDestArr.push(aimWeapon);

aimWeapon = new Ball({

x:enemy.x,//圆心X坐标

y:enemy.y,//圆心X坐标

r:12,//半径

startAngle:0,//开始角度

endAngle:2*Math.PI,//结束角度

stroke:true,//是否描边

strokeStyle:‘red’//样式

})

this.renderArr2.push(aimWeapon);

aimWeaponDestArr.push(aimWeapon);

var line = new Line({

x:enemy.x,

y:enemy.y,

startX:0,

startY:0,

endX:0,

endY:20,

strokeStyle:‘red’,

thin:true

})

this.renderArr2.push(line);

aimWeaponDestArr.push(line);

line = new Line({

x:enemy.x,

y:enemy.y,

startX:0,

startY:0,

endX:0,

endY:-20,

strokeStyle:‘red’,

thin:true

})

this.renderArr2.push(line);

aimWeaponDestArr.push(line);

line = new Line({

x:enemy.x,

y:enemy.y,

startX:0,

startY:0,

endX:20,

endY:0,

strokeStyle:‘red’,

thin:true

})

this.renderArr2.push(line);

aimWeaponDestArr.push(line);

line = new Line({

x:enemy.x,

y:enemy.y,

startX:0,

startY:0,

endX:-20,

endY:0,

strokeStyle:‘red’,

thin:true

最后

前15.PNG

前16.PNG

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

le:‘red’,

thin:true

})

this.renderArr2.push(line);

aimWeaponDestArr.push(line);

line = new Line({

x:enemy.x,

y:enemy.y,

startX:0,

startY:0,

endX:-20,

endY:0,

strokeStyle:‘red’,

thin:true

最后

[外链图片转存中…(img-TqnCmIVd-1715783316575)]

[外链图片转存中…(img-nd3pSVYT-1715783316576)]

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值