java robot api_java机器人API学习笔记

本文详细介绍了Java Robot API,包括`ahead()`、`back()`等方法的使用,以及如何获取战场信息、机器人自身状态等。通过示例展示了如何控制机器人移动、转向,并了解如何设置和获取机器人的颜色。同时,提到了与其它机器人碰撞时的行为。
摘要由CSDN通过智能技术生成

robocode 部分 API 中文参考

ahead 向前

public void ahead(double distance)

Immediately moves your robot ahead (forward) by distance measured in pixels.

马上将你的机器人向前移动 以 distance 指定的 多少个像素

This call executes immediately, and does not return until it is complete, i.e. when the remaining

distance to move is 0.

这个函数会马上执行,并且直到完成了任务才返回,比如,当距离已前进完时。

If the robot collides with a wall, the move is complete, meaning that the robot will not move any

further. If the robot collides with another robot, the move is

complete if you are heading toward the other robot.

当机器人撞到墙时,动作也是完成了,意味着此时机器人将不再向前进。当你的机器人撞到

其它的机器人时,如果你是头部撞到其它的机器人时动作也就完成。

Note that both positive and negative values can be given as input, where negative values means

that the robot is set to move backward instead of forward.

记住正数和负数都可以作为距离的值, 当距离为负值时表示机器人向后退距离的绝对值个像

素。

Example:

比如:

// Move the robot 100 pixels forward

将机器人向前移动 100 个像素

ahead(100);

// Afterwards, move the robot 50 pixels backward

然后,向后移动 50 个像素 ahead(-50);

Parameters:

参数:

distance - the distance to move ahead measured in pixels. If this value is negative, the robot will

move back instead of ahead

distanse-向前移动的距离,单位是像素。如果其值是负数,机器人会用向后来代

替向前。

See Also:

可以参考:

back(double), onHitWall(HitWallEvent), onHitRobot(HitRobotEvent)

back 向后

public void back(double distance)

Immediately moves your robot backward by distance measured in pixels.

马上将你的机器人向后移动 以 distance 指定的 多少个像素

This call executes immediately, and does not return until it is complete, i.e. when the remaining

distance to move is 0.

这个函数会马上执行,并且直到完成了任务才返回,比如,当距离已前进完时。

If the robot collides with a wall, the move is complete, meaning that the robot will not move any

further. If the robot collides with another robot, the move is

complete if you are heading toward the other robot.

当机器人撞到墙时,动作也是完成了,意味着此时机器人将不再向前进。当你的机器人撞到

其它的机器人时,如果你是头部撞到其它的机器人时动作也就完成。

Note that both positive and negative values can be given as input, where negative values means

that the robot is set to move forward instead of backward.

记住正数和负数都可以作为距离的值, 当距离为负值时表示机器人向前进距离的绝对值个像

素。

Example:

比如:

// Move the robot 100 pixels backward

将机器人向后移动 100 个像素

back(100);

// Afterwards, move the robot 50 pixels forward

然后,向前移动 50 个像素 ahead(-50);

back(-50);

Parameters:

参数:

distance - the distance to move back measured in pixels. If this value is negative, the robot will

move ahead instead of back.

distanse-向后移动的距离,单位是像素。如果其值是负数,机器人会用向前来代

替向后。

See Also:

可以参考:

ahead(double), onHitWall(HitWallEvent), onHitRobot(HitRobotEvent)

getBattleFieldWidth 得到战场宽度

public double getBattleFieldWidth()

Returns the width of the current battlefield measured in pixels.

返回值为以像素为单位表示的当前战场的宽度

Returns:

the width of the current battlefield measured in pixels.

--------------------------------------------------------------------------------

getBattleFieldHeight 得到战场高度

public double getBattleFieldHeight()

Returns the height of the current battlefield measured in pixels.

返回值为能像素为单位的当前战场的高度

Returns:

the height of the current battlefield measured in pixels.

getHeading 得到自己的方向

public double getHeading()

Returns the direction that the robot's body is facing, in degrees. The value returned will be

between 0 and 360 (is excluded).

返回机器人面对的方向,用角度表示。返回值的范围是 0 到 360 之间(不含 360) 。

Note that the heading in Robocode is like a compass, where 0 means North, 90 means East, 180

means South, and 270 means West.

记住在机器人软件中的坐标系就像一个罗盘, 0 表示正北, 90 表示正东,180 表示正南,270

表示正西。

Returns:

the direction that the robot's body is facing, in degrees.

返回机器人面对的方向,用角度表示

See Also:

getGunHeading(), getRadarHeading()

getHeight

public double getHeight()

Returns the height of the robot measured in pixels.

返回机器人自己的高度,单位为像素。

Returns:

the height of the robot measured in pixels.

See Also:

getWidth()

--------------------------------------------------------------------------------

getWidth

public double getWidth()

Returns the width of the robot measured in pixels.

返回机器人自己的宽度,单位为像素。

Returns:

the width of the robot measured in pixels.

See Also:

getHeight()

getName 返回自己的名字

public String getName()

Returns the robot's name.

返回机器人自己的名字

Returns:

the robot's name.

--------------------------------------------------------------------------------

getX 得到 X 坐标

public double getX()

Returns the X position of the robot. (0,0) is at the bottom left of the battlefield.

返回值为机器人的 X 坐标, (0,0)坐标在战场的左下角。 (译者注:向右为 X 正向,向上为

Y 正向) 。

Returns:

the X position of the robot.

See Also:

getY()

--------------------------------------------------------------------------------

getY 得到 Y 坐标

public double getY()

Returns the Y position of the robot. (0,0) is at the bottom left of the battlefield.

返回值为机器人的 Y 坐标, (0,0)坐标在战场的左下角。 (译者注:向右为 X 正向,向上为

Y 正向) 。

Returns:

the Y position of the robot.

See Also:

getX()

run

public void run()

The main method in every robot. You must override this to set up your robot's basic behavior.

在每个机器人中的主方法。为了你的机器人有一些基本行为,你必须重写这个方法。

Example:

例如:

// A basic robot that moves around in a square

一个在一个正方形里反复行走的基本的机器人

public void run() {

while (true) {

ahead(100);

turnRight(90);

}

}

Specified by:

run in interface Runnable

可以行走的时候才会行走。

--------------------------------------------------------------------------------

turnLeft

public void turnLeft(double degrees)

Immediately turns the robot's body to the left by degrees.

马上将机器人向左转以 degrees 指写的度数

This call executes immediately, and does not return until it is complete, i.e. when the angle

remaining in the robot's turn is 0.

这个指令马上被调用,直到做完才会返回。比如,当角度已经转完时就会返回。

Note that both positive and negative values can be given as input, where negative values means

that the robot's body is set to turn right instead of left.

正数和负数都可以作为参数,是负数意味着会向右转,转动角度的大小是一样的。

Example:

// Turn the robot 180 degrees to the left

将机器人向左转 180 度。

turnLeft(180);

// Afterwards, turn the robot 90 degrees to the right

然后,向右转 90 度。

turnLeft(-90);

Parameters:

degrees - the amount of degrees to turn the robot's body to the left. If degrees > 0 the robot will

turn left. If degrees < 0 the robot will turn right. If

degrees = 0 the robot will not turn, but execute.

degrees- 机器人向左转动的角度的大小。如果 degrees>0,那么机器人会向左转,如果

degrees<0,那么机器人会像右转。如果 degrees=0,那么机器人不会转动,但指令还是执行了

的。

See Also:

可参考:

turnRight(double), turnGunLeft(double), turnGunRight(double), turnRadarLeft(double),

turnRadarRight(double)

--------------------------------------------------------------------------------

turnRight 向右转

public void turnRight(double degrees)

Immediately turns the robot's body to the right by degrees.

马上将机器人向右转以 degrees 指写的度数

This call executes immediately, and does not return until it is complete, i.e. when the angle

remaining in the robot's turn is 0.

这个指令马上被调用,直到做完才会返回。比如,当角度已经转完时就会返回。

Note that both positive and negative values can be given as input, where negative values means

that the robot's body is set to turn left instead of right.

正数和负数都可以作为参数,是负数意味着会向左转,转动角度的大小是一样的。

Example:

// Turn the robot 180 degrees to the right

将机器人向右转 180 度。

turnRight(180);

// Afterwards, turn the robot 90 degrees to the left

然后,向左转 90 度。

turnRight(-90);

Parameters:

degrees - the amount of degrees to turn the robot's body to the right. If degrees > 0 the robot

will turn right. If degrees < 0 the robot will turn left. If

degrees = 0 the robot will not turn, but execute.

degrees- 机器人向右转动的角度的大小。如果 degrees>0,那么机器人会向右转,如果

degrees<0,那么机器人会像左转。如果 degrees=0,那么机器人不会转动,但指令还是执行了

的。

See Also:

turnLeft(double), turnGunLeft(double), turnGunRight(double), turnRadarLeft(double),

turnRadarRight(double)

--------------------------------------------------------------------------------

doNothing 什么都不做

public void doNothing()

Do nothing this turn, meaning that the robot will skip it's turn.

在这一轮中什么都不做,其实也就表示机器人将退出这一轮的比赛。

This call executes immediately, and does not return until the turn is over.

这个指令会马上执行,直到这轮比赛结束才返回。

finalize 最后的事情

protected final void finalize()

throws ThrowableCalled by the system to 'clean up' after your robot.

You may not override this method.

抛出一个 一个调用给系统,当你的机器人没了的时候,它会自动做一些清理工作。你不应

该重写这个方法。

Overrides:

finalize in class Object

Throws:

Throwable

--------------------------------------------------------------------------------

fire 开火

public void fire(double power) power 是开火时的能量,也即子弹的能量

Immediately fires a bullet. The bullet will travel in the direction the gun is pointing.

马上射出子弹。子弹会沿着枪口的方向前进。

The specified bullet power is an amount of energy that will be taken from the robot's energy.

Hence, the more power you want to spend on the bullet, the more energy is taken from your

robot.

指定的子弹力量会从你的机器人的能量里带走。因此,你设定的的子弹的能量越大,发射时

会从你的机器人里带走的能量也越多。

The bullet will do (4 * power) damage if it hits another robot. If power is greater than 1, it will do

an additional 2 * (power - 1) damage. You will get (3 * power) back if you hit the other robot. You

can call Rules.getBulletDamage(double) for getting the damage that a bullet with a specific bullet

power will do.

当子弹击中其它机器人时,会使那个机器人的能量损害 (4*power) 。如果设定的能量值大

于 1,还会使对方增加 2*(power-1) 的损害。当你的机器人击中其它机器人时,你会获

得 (3*power)的收益。你可以调用 Rules.getBulletDamage(double)来知道一个指定的子弹

能量能造成对方多大的损害(译者:当然,应该是指击中了对方时) 。

The specified bullet power should be between Rules.MIN_BULLET_POWER and

Rules.MAX_BULLET_POWER.

指定的子弹能量应该在Rules.MIN_BULLET_POWER 和Rules.MAX_BULLET_POWER两个值之间。

Note that the gun cannot fire if the gun is overheated, meaning that getGunHeat() returns a

value > 0.

请注意,当枪太热了的时候它就不能射击,意味此时 getGunHeat()返回值大于 0。

A event is generated when the bullet hits a robot (BulletHitEvent), wall (BulletMissedEvent), or

another bullet (BulletHitBulletEvent).

当子弹击中一个机器人,墙,或者另一粒子弹时,分别会产生 BulletHitEvent 事件,

BulletMissedEvent 事件,BulletHitBulletEvent 事件。

Example:

例如:

// Fire a bullet with maximum power if the gun is ready

当枪已经准备好时(不过热)射出一个最大能量的子弹

if (getGunHeat() == 0) {

fire(Rules.MAX_BULLET_POWER);

}

Parameters:

power - the amount of energy given to the bullet, and subtracted from the robot's energy.

power- 射击时给子弹的能量,并会从机器人中减去这个能量。

See Also:

fireBullet(double), getGunHeat(), getGunCoolingRate(), onBulletHit(BulletHitEvent),

onBulletHitBullet(BulletHitBulletEvent), onBulletMissed(BulletMissedEvent)

--------------------------------------------------------------------------------

fireBullet

public Bullet fireBullet(double power)

Immediately fires a bullet. The bullet will travel in the direction the gun is pointing.

马上射出子弹。子弹会沿着枪口的方向前进。

The specified bullet power is an amount of energy that will be taken from the robot's energy.

Hence, the more power you want to spend on the bullet, the more energy is taken from your

robot.

指定的子弹力量会从你的机器人的能量里带走。因此,你设定的的子弹的能量越大,发射时

会从你的机器人里带走的能量也越多。

The bullet will do (4 * power) damage if it hits another robot. If power is greater than 1, it will do

an additional 2 * (power - 1) damage. You will get (3 * power) back if you hit the other robot. You

can call Rules.getBulletDamage(double) for getting the damage that a bullet with a specific bullet

power will do.

当子弹击中其它机器人时,会使那个机器人的能量损害 (4*power) 。如果设定的能量值大

于 1,还会使对方增加 2*(power-1) 的损害。当你的机器人击中其它机器人时,你会获

得 (3*power)的收益。你可以调用 Rules.getBulletDamage(double)来知道一个指定的子弹

能量能造成对方多大的损害(译者:当然,应该是指击中了对方时) 。

The specified bullet power should be between Rules.MIN_BULLET_POWER and

Rules.MAX_BULLET_POWER.

指定的子弹能量应该在Rules.MIN_BULLET_POWER 和Rules.MAX_BULLET_POWER两个值之间。

Note that the gun cannot fire if the gun is overheated, meaning that getGunHeat() returns a

value > 0.

请注意,当枪太热了的时候它就不能射击,意味此时 getGunHeat()返回值大于 0。

A event is generated when the bullet hits a robot (BulletHitEvent), wall (BulletMissedEvent), or

another bullet (BulletHitBulletEvent).

当子弹击中一个机器人,墙,或者另一粒子弹时,分别会产生 BulletHitEvent 事件,

BulletMissedEvent 事件,BulletHitBulletEvent 事件。

Example:

// Fire a bullet with maximum power if the gun is ready

当枪已经准备好时(不过热)射出一个最大能量的子弹

if (getGunHeat() == 0) {

Bullet bullet = fireBullet(Rules.MAX_BULLET_POWER);

// Get the velocity of the bullet

得到子弹的速度

if (bullet != null) {

double bulletVelocity = bullet.getVelocity();

}

}

Parameters:

power - the amount of energy given to the bullet, and subtracted from the robot's energy.

power- 射击时给子弹的能量,并会从机器人中减去这个能量。

Returns:

a Bullet that contains information about the bullet if it was actually fired, which can be used for

tracking the bullet after it has been fired. If the bullet was not fired, null is returned.

返回值:一个存放了一些关于子弹的信息的 Bullet 类型(如果子弹真的射出去了) ,这个返

回值可以用来在已经射出后跟踪子弹。如果子弹没能射出去,返回值会是 null。

See Also:

可以参考:

fire(double), Bullet, getGunHeat(), getGunCoolingRate(), onBulletHit(BulletHitEvent),

onBulletHitBullet(BulletHitBulletEvent), onBulletMissed(BulletMissedEvent)

--------------------------------------------------------------------------------

getGunCoolingRate

public double getGunCoolingRate()

Returns the rate at which the gun will cool down, i.e. the amount of heat the gun heat will drop

per turn.

返回枪的冷却速度,也就是说,枪的热量会每一轮减少一些。

The gun cooling rate is default 0.1 / turn, but can be changed by the battle setup. So don't count

on the cooling rate being 0.1!

枪的冷却速度默认是 0.1 每轮,但是可以被战斗时的安装设置改变。 所以不要将冷却速率

看作一定是 0.1!

Returns:

the gun cooling rate

返回值:

枪的冷却速率

See Also:

getGunHeat(), fire(double), fireBullet(double)

--------------------------------------------------------------------------------

getGunHeading 得到枪的方向

public double getGunHeading()

Returns the direction that the robot's gun is facing, in degrees. The value returned will be

between 0 and 360 (is excluded).

Note that the heading in Robocode is like a compass, where 0 means North, 90 means East, 180

means South, and 270 means West.

返回机器人的枪的指向的方向,用角度表示。返回值在 0 到 360(不含 360)之间。

请注意在机器人编程中指向就像一个罗盘, 在这里 0 表示正北, 90 表示正东, 180 表示正南,

270 表示正西。

Returns:

the direction that the robot's gun is facing, in degrees.

返回值:

机器人的枪指向的方向,会用角度表示。

See Also:

getHeading(), getRadarHeading()

--------------------------------------------------------------------------------

getGunHeat 得到枪的热度

public double getGunHeat()

Returns the current heat of the gun. The gun cannot fire unless this is 0. (Calls to fire will succeed,

but will not actually fire unless getGunHeat() == 0).

返回枪现在的热量。只有这个值为 0 时你才能开火。 (值不为 0 时调用开火的命令会成功,

但直到 getGunHeat() == 0 即枪的热度为 0 时才会真正执行) 。

The amount of gun heat generated when the gun is fired is 1 + (firePower / 5). Each turn the gun

heat drops by the amount returned by getGunCoolingRate(), which is a battle setup.

当枪开火时产生的热量是 1 + (firePower / 5)(译者:firePower 即枪开火时的能量) 。每一轮

枪都会减少 getGunCoolingRate()返回值的那么多热量,但那是一个战斗设置值。

Note that all guns are "hot" at the start of each round, where the gun heat is 3.

请注意,在每一轮开始时所有的枪都是热的,这时枪的热量为 3。

Returns:

the current gun heat

返回:

现在枪的热量

See Also:

getGunCoolingRate(), fire(double), fireBullet(double)

--------------------------------------------------------------------------------

getNumRounds

public int getNumRounds()

Returns the number of rounds in the current battle.

返回当前在当前的战斗轮数。

Returns:

the number of rounds in the current battle

返回当前在当前的战斗轮数。

See Also:

getRoundNum()

--------------------------------------------------------------------------------

getOthers

public int getOthers()

Returns how many opponents that are left in the current round.

返回当前这一轮中还有多少个对手。

Returns:

how many opponents that are left in the current round.

返回值:当前这一轮中还有多少个对手

getRadarHeading 得到雷达的角度

public double getRadarHeading()

Returns the direction that the robot's radar is facing, in degrees. The value returned will be

between 0 and 360 (is excluded).

Note that the heading in Robocode is like a compass, where 0 means North, 90 means East, 180

means South, and 270 means West.

得到雷达正对着的方向,用角度表示。返回值在 0 到 360(不含 360)之间。

请注意在机器人编程中指向就像一个罗盘, 在这里 0 表示正北, 90 表示正东, 180 表示正南,

270 表示正西。

Returns:

the direction that the robot's radar is facing, in degrees.

返回值:

雷达正对着的方向,用角度表示

See Also:

getHeading(), getGunHeading()

--------------------------------------------------------------------------------

getRoundNum 得到当前轮数

public int getRoundNum()

Returns the current round number (0 to getNumRounds() - 1) of the battle.

返回当前回合是第几回合,值的范围是[0, to getNumRounds() - 1],闭区间。

Returns:

the current round number of the battle

返回值:

当前的回合数

See Also:

getNumRounds()

--------------------------------------------------------------------------------

getTime 得到时间

public long getTime()

Returns the game time of the current round, where the time is equal to the current turn in the

round.

A battle consists of multiple rounds.

返回当前回合的时间,这个值和这个回合的 turn 值是一样的。

Time is reset to 0 at the beginning of every round.

在每一个回合时间被重新设置为 0。

Returns:

the game time/turn of the current round.

返回值:

当前回合的时间。

--------------------------------------------------------------------------------

getVelocity 得到速度

public double getVelocity()

Returns the velocity of the robot measured in pixels/turn.

The maximum velocity of a robot is defined by Rules.MAX_VELOCITY (8 pixels / turn).

返回这个机器人的速度,以像素/一个时间单位为单位。 (译者注:在 robocode 中,turn 表

示为一个单位,可以认是一声滴答的时间) 。

Returns:

the velocity of the robot measured in pixels/turn.

返回值:

机器人的速度,以像素/单位时间为单位。

See Also:

Rules.MAX_VELOCITY

--------------------------------------------------------------------------------

onBulletHit 当击中对方时

public void onBulletHit(BulletHitEvent event)

This method is called when one of your bullets hits another robot. You should override it in your

robot if you want to be informed of this event.

当你的子弹击中对方时就会调用这个方法。 如果你想在子弹击中对方时作处理的话, 你应当

重写这个方法。

Example:

例如:

public void onBulletHit(BulletHitEvent event) {

out.println("I hit " + event.getName() + "!");

}

Specified by:

onBulletHit in interface IBasicEvents

这个方法的原始定义在 IBasicEvents 中

Parameters:

event - the bullet-hit event set by the game

参数:

event - the bullet-hit 游戏自己设置的(译者:这应当是默认的参数吧)

See Also:

BulletHitEvent, Event

--------------------------------------------------------------------------------

onBulletHitBullet 当子弹击中子弹时

public void onBulletHitBullet(BulletHitBulletEvent event)

This method is called when one of your bullets hits another bullet. You should override it in your

robot if you want to be informed of this event.

当你的子弹击中别人的子弹时这个方法会被调用。 如果你想在此时作一些处理, 你应当重写

这个方法。

Example:

例如:

public void onBulletHitBullet(BulletHitBulletEvent event) {

out.println("I hit a bullet fired by " + event.getBullet().getName() + "!");

}

Specified by:

onBulletHitBullet in interface IBasicEvents

原始定义在 IBasicEvents 中

Parameters:

event - the bullet-hit-bullet event set by the game

参数是游戏默认参数。

See Also:

BulletHitBulletEvent, Event

--------------------------------------------------------------------------------

onBulletMissed 当子弹迷路时

public void onBulletMissed(BulletMissedEvent event)

This method is called when one of your bullets misses, i.e. hits a wall. You should override it in

your robot if you want to be informed of this event.

当你的子弹迷路时就会调用这个方法,也就是说,此时你的子弹打到墙了。如果你想在此时

做一些处理,你应当重写这个方法。

Example:

public void onBulletHit(BulletMissedEvent event) {

out.println("Drat, I missed.");

}

Specified by:

onBulletMissed in interface IBasicEvents

Parameters:

event - the bullet-missed event set by the game

参数是游戏默认的参数。

See Also:

BulletMissedEvent, Event

--------------------------------------------------------------------------------

onDeath 当死了的时候

public void onDeath(DeathEvent event)

This method is called if your robot dies.

You should override it in your robot if you want to be informed of this event. Actions will have no

effect if called from this section. The intent is to allow

you to perform calculations or print something out when the robot is killed.

当你的机器人被消灭时这个方法会被调用。

如果你想在此时做一些处理, 你应当重写这个方法。 在这个区域写的所有的动作都没有任何

效果。这个函数的目的是允许你在机器人死的时候计算或是打印一些数据。

Specified by:

onDeath in interface IBasicEvents

Parameters:

event - the death event set by the game

参数是游戏默认的参数。

See Also:

DeathEvent, Event

onHitByBullet 当被击中时

public void onHitByBullet(HitByBulletEvent event)

This method is called when your robot is hit by a bullet. You should override it in your robot if you

want to be informed of this event.

当你的机器人被子弹击中时这个方法会被调用。 如果你想在此时做一些处理, 你应当重写这

个方法。

Example:

void onHitByBullet(HitByBulletEvent event) {

out.println(event.getRobotName() + " hit me!");

}

Specified by:

onHitByBullet in interface IBasicEvents

Parameters:

event - the hit-by-bullet event set by the game

参数是游戏的默认参数

See Also:

HitByBulletEvent, Event

--------------------------------------------------------------------------------

onHitRobot 当撞到别的机器人时

public void onHitRobot(HitRobotEvent event)

This method is called when your robot collides with another robot. You should override it in your

robot if you want to be informed of this event.

当你的机器人撞到别的机器人时,这个方法就会被调用。如果你想在此时做一些处理,你应

当重写这个方法。

Example:

void onHitRobot(HitRobotEvent event) {

if (event.getBearing() > -90 && event.getBearing() <= 90) {

back(100);

} else {

ahead(100);

}

}

-- or perhaps, for a more advanced robot --

或者也许对一个更高级的机器人,可以这样子:

public void onHitRobot(HitRobotEvent event) {

if (event.getBearing() > -90 && event.getBearing() <= 90) {

setBack(100);

} else {

setAhead(100);

}

}

The angle is relative to your robot's facing. So 0 is straight ahead of you.

这个角度(译者:此处应指 event.getBearing()返回的角度)和你的机器人的面向有关。所以

若是 0 的话就是在你的正前方。

This event can be generated if another robot hits you, in which case event.isMyFault() will return

false. In this case, you will not be automatically stopped by the game -- but if you continue

moving toward the robot you will hit it (and generate another event). If you are moving away,

then you won't hit it.

当另一个机器人撞到你时就会发生这个事件,此时 event.isMyFault()方法会返回 false 值。此

时,你不会被游戏自动停止行走。但是如果你继续向这个机器人前进的话你就会撞到它(然

后形成另一个事件) 。如果你离开,你就不会撞到它了。

Specified by:

onHitRobot in interface IBasicEvents

Parameters:

event - the hit-robot event set by the game

游戏默认的参数。

See Also:

HitRobotEvent, Event

--------------------------------------------------------------------------------

onHitWall 当撞到墙时

public void onHitWall(HitWallEvent event)

This method is called when your robot collides with a wall. You should override it in your robot if

you want to be informed of this event.

The wall at the top of the screen is 0 degrees, right is 90 degrees, bottom is 180 degrees, left is

270 degrees. But this event is relative to your heading, so: The bearing is such that turnRight

(event.getBearing()) will point you perpendicular to the wall.

当你的机器人撞到墙时这个事件会发生。 如果你想在些时做一些处理, 你应当重写这个方法。

在顶上的墙是 0 度,在右边的是 90 度,在左边的是 270 度, 。当是这个事件也和你的面向有

关,所以:turnRight (event.getBearing())调用会让你的机器人向墙垂直走。

Example:

void onHitWall(HitWallEvent event) {

out.println("Ouch, I hit a wall bearing " + event.getBearing() + " degrees.");

}

Specified by:

onHitWall in interface IBasicEvents

Parameters:

event - the hit-wall event set by the game

参数是游戏默认的参数。

See Also:

HitWallEvent, Event

--------------------------------------------------------------------------------

onRobotDeath 当其它机器人死时

public void onRobotDeath(RobotDeathEvent event)

This method is called when another robot dies. You should override it in your robot if you want to

be informed of this event.

当其它机器人被消灭时这个方法会被调用。如果你想做一些处理的话你应当重写这个方法。

Specified by:

onRobotDeath in interface IBasicEvents

Parameters:

event - The robot-death event set by the game

这是游戏默认的参数。

See Also:

RobotDeathEvent, Event

--------------------------------------------------------------------------------

onScannedRobot 当发现有其它机器人时

public void onScannedRobot(ScannedRobotEvent event)

This method is called when your robot sees another robot, i.e. when the robot's radar scan "hits"

another robot. You should override it in your robot if you want to be informed of this event.

(Almost all robots should override this!)

This event is automatically called if there is a robot in range of your radar.

当你的机器人发现了其它机器人时这个方法会被调用, 也就是此时你的雷达扫描到了其它的

机器人。如果你想在些时做一些处理,你应当重写这个方法。

(几乎所有机器人都应当重写这个方法! )

当有机器人进入你的雷达的范围时这个方法会被自动调用。

Note that the robot's radar can only see robot within the range defined by

Rules.RADAR_SCAN_RADIUS (1200 pixels).

请注意,机器人的雷达只能看到以 Rules.RADAR_SCAN_RADIUS(1200 像素)定义的范围。

Also not that the bearing of the scanned robot is relative to your robot's heading.

同时,扫描到的机器人的角度和你的面向无关。

Example:

void onScannedRobot(ScannedRobotEvent event) {

// Assuming radar and gun are aligned...

if (event.getDistance() < 100) {

fire(3);

} else {

fire(1);

}

}

Note:

请注意:

The game assists Robots in firing, as follows:

在开火时会有游戏帮助,规则如下:

If the gun and radar are aligned (and were aligned last turn),

and the event is current,

and you call fire() before taking any other actions, fire() will fire directly at the robot.

In essence, this means that if you can see a robot, and it doesn't move, then fire will hit it.

如果枪和雷达是对齐的(并且上一时刻也是对齐的) ,

并且这个事件是同时发生的,

并且在你调用 fire() 之前没有做其它任何动作,fire()会直接向那个机器人开火。

本质上说,这意味着如你扫描到了一个机器人,并且它不动,那么就会击中它。

AdvancedRobots will NOT be assisted in this manner, and are expected to examine the event to

determine if fire() would hit. (i.e. you are spinning your gun around, but by the time you get the

event, your gun is 5 degrees past the robot).

高级机器人不会以这个方式被帮助,它们想去检查这个事件看 fire()能否击中。 (也就是说,

你在旋转你的枪,但当你得到这个事件的消息时,你的枪与机器人偏离了 5 度) 。

Specified by:

onScannedRobot in interface IBasicEvents

Parameters:

event - the scanned-robot event set by the game

参数是游戏默认的参数。

See Also:

ScannedRobotEvent, Event, Rules.RADAR_SCAN_RADIUS

onWin 当胜利时

public void onWin(WinEvent event)

This method is called if your robot wins a battle.

Your robot could perform a victory dance here! :-)

当你的机器人赢了战斗时这个方法会被调用,你的机器人能在这时跳一个支胜利之舞!

Specified by:

onWin in interface IBasicEvents

Parameters:

event - the win event set by the game

参数值是游戏设定的

See Also:

WinEvent, Event

--------------------------------------------------------------------------------

scan 扫描

public void scan()

Scans for other robots. This method is called automatically by the game, as long as the robot is

moving, turning its body, turning its gun, or turning its radar.

Scan will cause onScannedRobot(ScannedRobotEvent) to be called if you see a robot.

扫描其它机器人。 这个方法是游戏自动调用的, 每当你的机器人在移动时, 转动它的身体时,

转动它的枪时,或是转动它的雷达时。

扫描会在看到一个机器人时调用 onScannedRobot(ScannedRobotEvent)方法。

There are 2 reasons to call scan() manually:

有两个要手动调用 scan()的原因:

You want to scan after you stop moving.

You want to interrupt the onScannedRobot event. This is more likely. If you are in

onScannedRobot and call scan(), and you still see a robot, then the system will interrupt your

onScannedRobot event immediately and start it from the top.

This call executes immediately.

在你停止移动时你想扫描。

你想中断 onScannedRobot 事件。这种似乎更有可能。如果你在 onScannedRobot 中调用

scan(),并且你仍看到一个机器人,那么系统会马上中断你的 onScannedRobot 事件,并从开

始处执行它。

这个调用会马上执行。

See Also:

onScannedRobot(ScannedRobotEvent), ScannedRobotEvent

--------------------------------------------------------------------------------

setAdjustGunForRobotTurn

public void setAdjustGunForRobotTurn(boolean independent)

Sets the gun to turn independent from the robot's turn.

使枪独立于机器人转动。

Ok, so this needs some explanation: The gun is mounted on the robot's body. So, normally, if the

robot turns 90 degrees to the right, then the gun will turn with it as it is mounted on top of the

robot's body. To compensate for this, you can call setAdjustGunForRobotTurn(true). When this is

set, the gun will turn independent from the robot's turn, i.e. the gun will compensate for the

robot's body turn.

好的,这就要一些解释了:枪是固定在机器人上的。因此,一般来说,如果机器人向右转

90 度 , 枪 也 会 因 为 架 在 机 器 人 上 而 转 动 。 作 为 一 种 补 偿 , 你 可 以 调 用

setAdjustGunForRobotTurn(true),当这个被设定时,枪会独立于机器人转动。

Example, assuming both the robot and gun start out facing up (0 degrees):

例如,设你的机器人和枪在开始时都向上(0 度角) 。

// Set gun to turn with the robot's turn

设置使枪和机器人一起转

setAdjustGunForRobotTurn(false); // This is the default 这是默认的

turnRight(90);

// At this point, both the robot and gun are facing right (90 degrees)

在这一点,机器人和枪都面向右边(90 度角) 。

turnLeft(90);

// Both are back to 0 degrees

都回到 0 度角

-- or --或者

// Set gun to turn independent from the robot's turn

设置使枪独立于机器人转动

setAdjustGunForRobotTurn(true);

turnRight(90);

// At this point, the robot is facing right (90 degrees), but the gun is still facing up.

在这一点,机器人面向右,但枪面向上

turnLeft(90);

// Both are back to 0 degrees.

它们都回到 0 度角

Note: The gun compensating this way does count as "turning the gun". See

setAdjustRadarForGunTurn(boolean) for details.

注 意 : 枪 独 立 的 这 种 方 式 被 认 为 是 转 动 枪 。 要 更 详 细 , 请 看

setAdjustRadarForGunTurn(boolean)

Parameters:

independent - true if the gun must turn independent from the robot's turn; false if the gun must

turn with the robot's turn.

independent - true 如果枪必须独立于机器人而转动;false 如果枪必须随着机器人转动。

See Also:

setAdjustRadarForGunTurn(boolean)

--------------------------------------------------------------------------------

setAdjustRadarForRobotTurn

public void setAdjustRadarForRobotTurn(boolean independent)

Sets the radar to turn independent from the robot's turn.

设置使雷达独立于机器人转动

Ok, so this needs some explanation: The radar is mounted on the gun, and the gun is mounted on

the robot's body. So, normally, if the robot turns 90 degrees to the right, the gun turns, as does

the radar. Hence, if the robot turns 90 degrees to the right, then the gun and radar will turn with

it as the radar is mounted on top of the gun. To compensate for this, you can call

setAdjustRadarForRobotTurn(true). When this is set, the radar will turn independent from the

robot's turn, i.e. the radar will compensate for the robot's turn.

好的, 这就要一些解释了: 雷达是固定在枪上的, 枪是固定在机器人上的。 因此, 一般来说,

如果机器人向右转 90 度,枪也会因为架在机器人上而转动,雷达也一样。因此,如果机器

人向右转 90 度,那么枪和雷达会一起转,因为雷达因定在枪上。作为一种补偿,你可以调

用 setAdjustGunForRobotTurn(true),当这个被设定时,枪会独立于机器人转动。

Example, assuming the robot, gun, and radar all start out facing up (0 degrees):

例如,设你的机器人和枪,雷在开始时都向上(0 度角) 。

// Set radar to turn with the robots's turn

设置使雷达和机器人一起转

setAdjustRadarForRobotTurn(false); // This is the default 这是默认的

turnRight(90);

// At this point, the body, gun, and radar are all facing right (90 degrees);

在这一点,机器人和枪,雷达都面向右边(90 度角) 。

都回到 0 度角

-- or --或者

// Set radar to turn independent from the robot's turn

设置使雷达独立于机器人转动

setAdjustRadarForRobotTurn(true);

turnRight(90);

// At this point, the robot and gun are facing right (90 degrees), but the radar is still facing up.

在这一点,机器人和枪面向右(90 度角) ,但雷达仍向上。

Parameters:

independent - true if the radar must turn independent from the robots's turn; false if the radar

must turn with the robot's turn.

参数

independent - true 如果雷达必须独立于机器人转动, false 如果雷达必须和机器人一起转动

See Also:

setAdjustGunForRobotTurn(boolean), setAdjustRadarForGunTurn(boolean)

--------------------------------------------------------------------------------

setAdjustRadarForGunTurn

public void setAdjustRadarForGunTurn(boolean independent)

Sets the radar to turn independent from the gun's turn.

使雷达独立于枪转动。

Ok, so this needs some explanation: The radar is mounted on the robot's gun. So, normally, if the

gun turns 90 degrees to the right, then the radar will turn with it as it is mounted on top of the

gun. To compensate for this, you can call setAdjustRadarForGunTurn(true). When this is set, the

radar will turn independent from the robot's turn, i.e. the radar will compensate for the gun's

turn.

好的,这就要一些解释了:雷达是固定在枪上的。因此,一般来说,如果枪向右转 90 度,

雷达也会因为架在枪上而转动。 作为一种补偿, 你可以调用 setAdjustRadarForGunTurn(true),

当这个被设定时,雷达会独立于枪转动。

Example, assuming both the gun and radar start out facing up (0 degrees):

例如,设你的枪和雷达在开始时都向上(0 度角) 。

// Set radar to turn with the gun's turn

设置使雷达和枪一起转

setAdjustRadarForGunTurn(false); // This is the default 这是默认的

turnGunRight(90);

// At this point, both the radar and gun are facing right (90 degrees);

在这一点,雷达和枪都面向右边(90 度角) 。

都回到 0 度角

-- or --或者

// Set radar to turn independent from the gun's turn

设置使雷独立于枪转动

setAdjustRadarForGunTurn(true);

turnGunRight(90);

// At this point, the gun is facing right (90 degrees), but the radar is still facing up.

在这一点,枪面向右,但雷达面向上

Note: Calling setAdjustRadarForGunTurn(boolean) will automatically call

setAdjustRadarForRobotTurn(boolean) with the same value, unless you have already called it

earlier. This behavior is primarily for backward compatibility with older Robocode robots.

请 注 意 : 调 用 setAdjustRadarForGunTurn(boolean) 时 会 自 动 用 同 样 的 参 数 调 用

setAdjustRadarForRobotTurn(boolean) , 除 非 你 已 经 调 用 过

setAdjustRadarForRobotTurn(boolean)。这主要是为了向后兼容旧的机器人。

Parameters:

independent - true if the radar must turn independent from the gun's turn; false if the radar must

turn with the gun's turn.

参数

independent - true 如果雷达必须独立于枪转动, false 如果雷达必须和枪一起转动

See Also:

setAdjustRadarForRobotTurn(boolean), setAdjustGunForRobotTurn(boolean)

setColors 设置颜色

public void setColors(Color bodyColor,

Color gunColor,

Color radarColor)

Sets the color of the robot's body, gun, and radar in the same time.

You may only call this method one time per battle. A null indicates the default (blue) color.

同时设置机器人的身体,枪,雷达的颜色。

你每次战斗只能调用这个方法一次,参数为空的话就用默认的颜色(蓝) 。

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setColors(null, Color.RED, new Color(150, 0, 150));

...

}

Parameters: 参数

bodyColor - the new body color 身体的新颜色

gunColor - the new gun color 枪的新颜色

radarColor - the new radar color 雷达的新颜色

See Also:

setColors(Color, Color, Color, Color, Color), setAllColors(Color), setBodyColor(Color),

setGunColor(Color), setRadarColor(Color), setBulletColor(Color),

setScanColor(Color), Color

--------------------------------------------------------------------------------

setColors

public void setColors(Color bodyColor,

Color gunColor,

Color radarColor,

Color bulletColor,

Color scanArcColor)

Sets the color of the robot's body, gun, radar, bullet, and scan arc in the same time.

You may only call this method one time per battle. A null indicates the default (blue) color for the

body, gun, radar, and scan arc, but white for the bullet

color.

同时设置机器人的身体,枪,雷达,子弹,扫描的颜色。

你每次战斗只能调用这个方法一次,参数为空的话就为身体,枪,雷达,扫描角用默认的颜

色(蓝) ,但会为子弹用白色。

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setColors(null, Color.RED, Color.GREEN, null, new Color(150, 0, 150));

...

}

Parameters: 参数

bodyColor - the new body color 身体的新颜色

gunColor - the new gun color 枪的新颜色

radarColor - the new radar color 雷达的新颜色

bulletColor - the new bullet color 子弹的新颜色

scanArcColor - the new scan arc color 扫描角的新颜色

Since:

1.1.3

See Also:

setColors(Color, Color, Color), setAllColors(Color), setBodyColor(Color), setGunColor(Color),

setRadarColor(Color), setBulletColor(Color), setScanColor(Color),

Color

--------------------------------------------------------------------------------

setAllColors

public void setAllColors(Color color)

Sets all the robot's color to the same color in the same time, i.e. the color of the body, gun, radar,

bullet, and scan arc.

You may only call this method one time per battle. A null indicates the default (blue) color for the

body, gun, radar, and scan arc, but white for the bullet

color.

将机器人上所有物件设置为同样的颜色,包括身体,枪,雷达,子弹,扫描角。

你每次战斗只能调用这个方法一次,参数为空的话就为身体,枪,雷达,扫描角用默认的颜

色(蓝) ,但会为子弹用白色。

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setAllColors(Color.RED);

...

}

Parameters: 参数

color - the new color for all the colors of the robot 所有物件的新颜色

Since:

1.1.3

See Also:

setColors(Color, Color, Color), setColors(Color, Color, Color, Color, Color), setBodyColor(Color),

setGunColor(Color), setRadarColor(Color), setBulletColor

(Color), setScanColor(Color), Color

--------------------------------------------------------------------------------

setBodyColor 设置身体颜色

public void setBodyColor(Color color)

Sets the color of the robot's body.

A null indicates the default (blue) color.

设置身体的颜色

参数为空的话就用默认的颜色(蓝)

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setBodyColor(Color.BLACK);

...

}

Parameters: 参数

color - the new body color 身体的新颜色

Since:

1.1.2

See Also:

setColors(Color, Color, Color), setColors(Color, Color, Color, Color, Color), setAllColors(Color),

setGunColor(Color), setRadarColor(Color), setBulletColor

(Color), setScanColor(Color), Color

--------------------------------------------------------------------------------

setGunColor 设置枪的颜色

public void setGunColor(Color color)Sets the color of the robot's gun.

A null indicates the default (blue) color.

设置枪的颜色

参数为空的话就用默认的颜色(蓝)

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setGunColor(Color.RED);

...

}

Parameters: 参数

color - the new gun color 枪的新颜色

Since:

1.1.2

See Also:

setColors(Color, Color, Color), setColors(Color, Color, Color, Color, Color), setAllColors(Color),

setBodyColor(Color), setRadarColor(Color), setBulletColor

(Color), setScanColor(Color), Color

--------------------------------------------------------------------------------

setRadarColor 设置雷达的颜色

public void setRadarColor(Color color)Sets the color of the robot's radar.

A null indicates the default (blue) color.

设置雷达的颜色

参数为空的话就用默认的颜色(蓝)

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setRadarColor(Color.YELLOW);

...

}

Parameters: 参数

color - the new radar color 雷达的新颜色

Since:

1.1.2

See Also:

setColors(Color, Color, Color), setColors(Color, Color, Color, Color, Color), setAllColors(Color),

setBodyColor(Color), setGunColor(Color), setBulletColor

(Color), setScanColor(Color), Color

--------------------------------------------------------------------------------

setBulletColor 设置子弹的颜色

public void setBulletColor(Color color)Sets the color of the robot's bullets.

A null indicates the default white color.

设置子弹的颜色

参数为空的话就用默认的颜色(蓝)

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setBulletColor(Color.GREEN);

...

}

Parameters: 参数

color - the new bullet color 子弹的新颜色

Since:

1.1.2

See Also:

setColors(Color, Color, Color), setColors(Color, Color, Color, Color, Color), setAllColors(Color),

setBodyColor(Color), setGunColor(Color), setRadarColor

(Color), setScanColor(Color), Color

--------------------------------------------------------------------------------

setScanColor 设置扫描的颜色

public void setScanColor(Color color)Sets the color of the robot's scan arc.

A null indicates the default (blue) color.

设置扫描的颜色

参数为空的话就用默认的颜色(蓝)

Example:

// Don't forget to import java.awt.Color at the top...

不要忘记在顶上导入 java.awt.Color

import java.awt.Color;

...

public void run() {

setScanColor(Color.WHITE);

...

}

Parameters: 参数

color - the new scan arc color 扫描的新颜色

Since:

1.1.2

See Also:

setColors(Color, Color, Color), setColors(Color, Color, Color, Color, Color), setAllColors(Color),

setBodyColor(Color), setGunColor(Color), setRadarColor

(Color), setBulletColor(Color), Color

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值