📄 lintongbot.java
字号:
package mypackage;
import robocode.*;
import java.awt.Color;
public class lintongBot extends AdvancedRobot
{
Enemy target;
final double PI = Math.PI;
int direction = 1;
double firePower; // 设置我们的火力
public void run()
{
target = new Enemy(); //实例化Enemy()类
target.distance = 100000;
setColors(Color.yellow,Color.blue,Color.green);
//让gun,radar独立于坦克车
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
turnRadarRightRadians(2*PI); // 以弧度计算旋转一周
while(true)
{
doMovement();
donothitwall(); // 移动机器人,避免撞墙
doFirePower(); // 选择火力
doScanner(); // 扫描
doGun(); // 预测敌人,调整炮管
out.println(target.distance);
fire(firePower);
execute();
}
}
void doFirePower()
{
firePower = 400/target.distance;//根据敌人距离来选择火力,因为本身前进,后退为300,所以火力不会过大
}
void doMovement()
{
if (getTime()%20== 0)
{ double a=target.distance*10;
direction *= -1;
setAhead(direction*a);
}
setTurnRightRadians(target.bearing + (PI/2));
}
void donothitwall()
{
double p=4*(target.distance/80);//p是周期敌人距离越近周期越短
if(getTime()%p==0){
double move=(Math.random()*2-1)*(p*8-25);
setAhead(move+((move>=0)?25:-25));
double x=getX()+move*Math.sin(target.head);
double y=getY()+move*Math.cos(target.head);
double dwidth=getBattleFieldWidth();
double dheight=getBattleFieldHeight();
if(x<100)
{ direction*=-1;
setAhead(direction*PI);
}
if(x>dwidth-60){
direction*=-1;
setAhead(direction*PI);
}
if(y<100){direction*=-1;
setAhead(direction*PI);
}if(y>dheight-60)
{direction*=-1;
setAhead(direction*300);
}
}
}
void doScanner()
{
double radarOffset; //雷达偏移量
if (getTime() - target.ctime > 4) //来回扫了4个回合都没扫到意味失去了目标,再全扫一遍
{
radarOffset = 360;
}
else
{
radarOffset = getRadarHeadingRadians() - absbearing(getX(),getY(),target.x,target.y);
if (radarOffset < 0)
radarOffset -= PI/8; //(0.375)
else
radarOffset += PI/8;
}
setTurnRadarLeftRadians(NormaliseBearing(radarOffset)); //左转调整转动角度到PI内
}
void doGun()
{
long time = getTime() + (int)(target.distance/(20-(3*firePower)));
double gunOffset = getGunHeadingRadians() - absbearing(getX(),getY(),target.guessX(time),target.guessY(time));
setTurnGunLeftRadians(NormaliseBearing(gunOffset));
}
public double NormaliseBearing(double ang)
{
if (ang > PI)
ang -= 2*PI;
if (ang < -PI)
ang += 2*PI;
return ang;
}
public double getrange(double x1,double y1,double x2,double y2 )//求距离
{
double xo = x2-x1;
double yo = y2-y1;
double h = Math.sqrt( xo*xo + yo*yo );
return h;
}
public double absbearing(double x1,double y1,double x2,double y2 )
{
double xo = x2-x1;
double yo = y2-y1;
double h = getrange( x1,y1, x2,y2 );
if( xo > 0 && yo > 0 )
{
return Math.asin( xo / h );
}
if( xo > 0 && yo < 0 )
{
return Math.PI - Math.asin( xo / h ); //x为正,y为负第二象限角
}
if( xo < 0 && yo < 0 )
{
return Math.PI + Math.asin( -xo / h ); //第三象限内180+角度
}
if( xo < 0 && yo > 0 )
{
return 2.0*Math.PI - Math.asin( -xo / h ); //四象限360-角度
}
return 0;
}
/**
* 扫描事件,也是初始化目标数据的过程
*/
public void onScannedRobot(ScannedRobotEvent e)
{
if ((e.getDistance() < target.distance)||(target.name == e.getName()))
{
double dwidth=getBattleFieldWidth();
double dheight=getBattleFieldHeight();
double absbearing_rad = (getHeadingRadians()+e.getBearingRadians())%(2*PI);
//this section sets all the information about our target
target.name = e.getName();
target.x = getX()+Math.sin(absbearing_rad)*e.getDistance();
target.y = getY()+Math.cos(absbearing_rad)*e.getDistance();
target.bearing = e.getBearingRadians();
target.head = e.getHeadingRadians();
target.ctime = getTime(); //扫描到机器人的游戏时间
target.speed = e.getVelocity(); //得到敌人速度
target.distance = e.getDistance();
}
}
public void onRobotDeath(RobotDeathEvent e)
{
if (e.getName() == target.name)
target.distance = 10000;
}
}
class Enemy
{
String name;
public double bearing;
public double head;
public long ctime;
public double speed;
public double x,y;
public double distance;
public double guessX(long when)
{
//以扫描时和子弹到达的时间差 * 最大速度=距离, 再用对手的坐标加上移动坐标得到敌人移动后的坐标
long diff = when - ctime;
return x+Math.sin(head)*speed*diff; //目标移动后的坐标
}
public double guessY(long when)
{
long diff = when - ctime;
return y+Math.cos(head)*speed*diff;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -