?? lintongbot.java
字號(hào):
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; // 設(shè)置我們的火力
public void run()
{
target = new Enemy(); //實(shí)例化Enemy()類
target.distance = 100000;
setColors(Color.yellow,Color.blue,Color.green);
//讓gun,radar獨(dú)立于坦克車
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
turnRadarRightRadians(2*PI); // 以弧度計(jì)算旋轉(zhuǎn)一周
while(true)
{
doMovement();
donothitwall(); // 移動(dòng)機(jī)器人,避免撞墻
doFirePower(); // 選擇火力
doScanner(); // 掃描
doGun(); // 預(yù)測(cè)敵人,調(diào)整炮管
out.println(target.distance);
fire(firePower);
execute();
}
}
void doFirePower()
{
firePower = 400/target.distance;//根據(jù)敵人距離來選擇火力,因?yàn)楸旧砬斑M(jìn),后退為300,所以火力不會(huì)過大
}
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; //雷達(dá)偏移量
if (getTime() - target.ctime > 4) //來回掃了4個(gè)回合都沒掃到意味失去了目標(biāo),再全掃一遍
{
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)); //左轉(zhuǎn)調(diào)整轉(zhuǎn)動(dòng)角度到PI內(nèi)
}
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為負(fù)第二象限角
}
if( xo < 0 && yo < 0 )
{
return Math.PI + Math.asin( -xo / h ); //第三象限內(nèi)180+角度
}
if( xo < 0 && yo > 0 )
{
return 2.0*Math.PI - Math.asin( -xo / h ); //四象限360-角度
}
return 0;
}
/**
* 掃描事件,也是初始化目標(biāo)數(shù)據(jù)的過程
*/
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(); //掃描到機(jī)器人的游戲時(shí)間
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)
{
//以掃描時(shí)和子彈到達(dá)的時(shí)間差 * 最大速度=距離, 再用對(duì)手的坐標(biāo)加上移動(dòng)坐標(biāo)得到敵人移動(dòng)后的坐標(biāo)
long diff = when - ctime;
return x+Math.sin(head)*speed*diff; //目標(biāo)移動(dòng)后的坐標(biāo)
}
public double guessY(long when)
{
long diff = when - ctime;
return y+Math.cos(head)*speed*diff;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -