- out
- Robot
- ahead
- back
- getBattleFieldHeight
- getBattleFieldWidth
- getHeading
- getHeight
- getName
- getWidth
- getX
- getY
- run
- turnLeft
- turnRight
- doNothing
- finalize
- fire
- fireBullet
- getGunCoolingRate
- getGunHeading
- getGunHeat
- getNumRounds
- getOthers
- getRadarHeading
- getRoundNum
- getTime
- getVelocity
- onBulletHit
- onBulletHitBullet
- onBulletMissed
- onDeath
- onHitByBullet
- onHitRobot
- onHitWall
- onRobotDeath
- onScannedRobot
- onWin
- resume
- scan
- setAdjustGunForRobotTurn
- setAdjustRadarForGunTurn
- setColors
- stop
- stop
- turnGunLeft
- turnGunRight
- turnRadarLeft
- turnRadarRight
- getEnergy
- setAdjustRadarForRobotTurn
玩java的应该都有robot经验吧?我希望是,在我学习java的那段时间,我曾经很沉迷。
可是到后来java不能满足我的欲望,于是robot就搁浅下来。至今没有一个我自己独立完成的好的机器人留给大家,抱歉。把这个类好好描述一下,也希望大家能喜欢这个让人兴奋的东西。
robocode
Class Robot
java.lang.Object | +-robocode._Robot | +-robocode.Robot
- All Implemented Interfaces:
- Runnable
- Direct Known Subclasses:
- _AdvancedRobot
The basic robot class that you will extend to create your own robots.
Please note the following standards will be used:
heading - absolute angle in degrees with 0 facing up the screen, positive clockwise. 0 <= heading < 360.
bearing - relative angle to some object from your robot's heading, positive clockwise. -180 < bearing <= 180
All coordinates are expressed as (x,y).
All coordinates are positive.
The origin (0,0) is at the bottom left of the screen.
Positive x is right.
Positive y is up.
Field Summary | |
PrintStream |
out The output stream your robot should use to print. |
Constructor Summary | |
Robot() |
Method Summary | |
void |
ahead(double distance) Moves your robot forward. |
void |
back(double distance) Moves your robot backward. |
void |
doNothing() Do nothing this turn. |
void |
finalize() Called by the system to 'clean up' after your robot. |
void |
fire(double power) Fires a bullet. |
Bullet |
fireBullet(double power) Fires a bullet. |
double |
getBattleFieldHeight() Get height of the current battlefield. |
double |
getBattleFieldWidth() Get width of the current battlefield. |
double |
getEnergy() Returns the robot's current energy |
double |
getGunCoolingRate() Returns the rate at which the gun will cool down. |
double |
getGunHeading() Returns gun heading in degrees. |
double |
getGunHeat() Returns the current heat of the gun. |
double |
getHeading() Returns the direction the robot is facing, in degrees. |
double |
getHeight() Returns the height of the robot |
String |
getName() Returns the robot's name |
int |
getNumRounds() Returns the number of rounds in the current battle |
int |
getOthers() Returns how many opponents are left |
double |
getRadarHeading() Returns radar heading in degrees. |
int |
getRoundNum() Returns the number of the current round (1 to getNumRounds()) in the battle |
long |
getTime() Returns the current game time Note: 1 battle consists of multiple rounds Time is reset to 0 at the beginning of every round. |
double |
getVelocity() Returns the velocity of the robot. |
double |
getWidth() Returns the width of the robot |
double |
getX() Returns the X position of the robot. |
double |
getY() Returns the Y position of the robot. |
void |
onBulletHit(BulletHitEvent event) This method will be called when one of your bullets hits another robot. |
void |
onBulletHitBullet(BulletHitBulletEvent event) This method will be called when one of your bullets hits another bullet. |
void |
onBulletMissed(BulletMissedEvent event) This method will be called when one of your bullets misses (hits a wall). |
void |
onDeath(DeathEvent event) This method will be called if your robot dies You should override it in your robot if you want to be informed of this event. |
void |
onHitByBullet(HitByBulletEvent event) This method will be called when your robot is hit by a bullet. |
void |
onHitRobot(HitRobotEvent event) This method will be called when your robot collides with another robot. |
void |
onHitWall(HitWallEvent event) This method will be called when your robot collides with a wall. |
void |
onRobotDeath(RobotDeathEvent event) This method will be called if another robot dies You should override it in your robot if you want to be informed of this event. |
void |
onScannedRobot(ScannedRobotEvent event) This method will be called when your robot sees another robot. |
void |
onWin(WinEvent event) This method will be called if your robot wins a battle. |
void |
resume() Resume the movement you stopped in stop(), if any. |
void |
run() The main method in every robot. |
void |
scan() Look for other robots. |
void |
setAdjustGunForRobotTurn(boolean newAdjustGunForRobotTurn) Sets the gun to automatically turn the opposite way when the robot turns. |
void |
setAdjustRadarForGunTurn(boolean newAdjustRadarForGunTurn) Sets the radar to automatically turn the opposite way when the gun turns. |
void |
setAdjustRadarForRobotTurn(boolean newAdjustRadarForRobotTurn) Sets the radar to automatically turn the opposite way when the robot turns. |
void |
setColors(Color robotColor, Color gunColor, Color radarColor) Call this method to set your robot's colors. |
void |
stop() Stops all movement, and saves it for a call to resume(). |
void |
stop(boolean overwrite) Stops all movement, and saves it for a call to resume(). |
void |
turnGunLeft(double degrees) Rotates your robot's gun. |
void |
turnGunRight(double degrees) Rotates your robot's gun. |
void |
turnLeft(double degrees) Rotates your robot. |
void |
turnRadarLeft(double degrees) Rotates your robot's radar. |
void |
turnRadarRight(double degrees) Rotates your robot's radar. |
void |
turnRight(double degrees) Rotates your robot. |
Methods inherited from class robocode._Robot |
getBattleNum, getGunCharge, getGunImageName, getLife, getNumBattles, getRadarImageName, getRobotImageName, setGunImageName, setInterruptible, setPeer, setRadarImageName, setRobotImageName |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
out
public PrintStream out
- The output stream your robot should use to print. You can view it by clicking the buttons on the right side of the battle.
Example public void onHitRobot(HitRobotEvent e) { out.println("I hit a robot! My energy: " getEnergy() " his energy: " e.getEnergy()); } System.out will also print to this.
Constructor Detail |
Robot
public Robot()
Method Detail |
ahead
public void ahead(double distance)
- Moves your robot forward. This call executes immediately, and does not return until it is complete. If the robot collides with a wall, the move is complete. If the robot collides with another robot, the move is complete if you are heading toward the other robt.
Example
ahead(50);
- Parameters:
distance
- The distance to move forward- See Also:
onHitWall(robocode.HitWallEvent)
,onHitRobot(robocode.HitRobotEvent)
back
public void back(double distance)
- Moves your robot backward. This call executes immediately, and does not return until it is complete. If the robot collides with a wall, the move is complete. If the robot collides with another robot, the move is complete if you are heading toward the other robt.
Example
back(150);
- Parameters:
distance
- The distance to backward- See Also:
onHitWall(robocode.HitWallEvent)
,onHitRobot(robocode.HitRobotEvent)
getBattleFieldHeight
public double getBattleFieldHeight()
- Get height of the current battlefield.
- Returns:
- The height of the battlefield.
getBattleFieldWidth
public double getBattleFieldWidth()
- Get width of the current battlefield.
- Returns:
- The width of the battlefield.
getHeading
public double getHeading()
- Returns the direction the robot is facing, in degrees. The value returned will be between 0 and 360.
- Returns:
- the direction the robot is facing, in degrees.
getHeight
public double getHeight()
- Returns the height of the robot
- Returns:
- the height of the robot
getName
public String getName()
- Returns the robot's name
- Returns:
- the robot's name
getWidth
public double getWidth()
- Returns the width of the robot
- Returns:
- the width of the robot
getX
public double getX()
- Returns the X position of the robot. (0,0) is at the bottom left of the battlefield.
- Returns:
- the X position of the robot
getY
public double getY()
- Returns the Y position of the robot. (0,0) is at the bottom left of the battlefield.
- Returns:
- the Y position of the robot
run
public void run()
- The main method in every robot. 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 interfaceRunnable
turnLeft
public void turnLeft(double degrees)
- Rotates your robot. This call executes immediately, and does not return until it is complete. Note that the gun and radar will rotate the same amount, as they are attached to the robot.
Example
turnLeft(90);
turnRight
public void turnRight(double degrees)
- Rotates your robot. This call executes immediately, and does not return until it is complete. Note that the gun and radar will rotate the same amount, as they are attached to the robot.
Example
turnRight(90);
doNothing
public void doNothing()
- Do nothing this turn. This call executes immediately.
finalize
public final void finalize()
- Called by the system to 'clean up' after your robot. You may not override this method.
fire
public void fire(double power)
- Fires a bullet. The valid range for power is .1 to 3. The bullet will travel in the direction the gun is pointing. 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. An event will be generated when the bullet hits a robot, wall, or other bullet. This call executes immediately.
- Parameters:
power
- The energy given to the bullet, and subtracted from your energy.- See Also:
fireBullet(double)
,onBulletHit(robocode.BulletHitEvent)
,onBulletHitBullet(robocode.BulletHitBulletEvent)
,onBulletMissed(robocode.BulletMissedEvent)
fireBullet
public Bullet fireBullet(double power)
- Fires a bullet. This call is exactly like fire(double), but returns the Bullet object you fired. This call executes immediately.
- See Also:
fire(double)
getGunCoolingRate
public double getGunCoolingRate()
- Returns the rate at which the gun will cool down.
- Returns:
- the gun cooling rate
- See Also:
getGunHeat()
getGunHeading
public double getGunHeading()
- Returns gun heading in degrees. This is a value from 0 to 360, where 0 points to the top of the screen.
- Returns:
- gun heading
getGunHeat
public double getGunHeat()
- Returns the current heat of the gun. You cannot fire unless this is 0. (Calls to fire will succeed, but will not actually fire unless getGunHeat() == 0
- Returns:
- the current gun heat
getNumRounds
public int getNumRounds()
- Returns the number of rounds in the current battle
- Returns:
- the number of rounds in the current battle
getOthers
public int getOthers()
- Returns how many opponents are left
- Returns:
- how many opponents are left
getRadarHeading
public double getRadarHeading()
- Returns radar heading in degrees. This is a value from 0 to 360, where 0 points to the top of the screen.
- Returns:
- radar heading
getRoundNum
public int getRoundNum()
- Returns the number of the current round (1 to getNumRounds()) in the battle
- Returns:
- the number of the current round in the battle
getTime
public long getTime()
- Returns the current game time Note: 1 battle consists of multiple rounds Time is reset to 0 at the beginning of every round. getTime() is equivalent to the number of frames displayed this round.
- Returns:
- the current game time
getVelocity
public double getVelocity()
- Returns the velocity of the robot.
- Returns:
- the velocity of the robot
onBulletHit
public void onBulletHit(BulletHitEvent event)
- This method will be 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() "!"); }
- Parameters:
event
- The event set by the game- See Also:
BulletHitEvent
,Event
onBulletHitBullet
public void onBulletHitBullet(BulletHitBulletEvent event)
- This method will be 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() "!"); }
- Parameters:
event
- The event set by the game- See Also:
BulletHitBulletEvent
,Event
onBulletMissed
public void onBulletMissed(BulletMissedEvent event)
- This method will be called when one of your bullets misses (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."); }
- Parameters:
event
- The event set by the game- See Also:
BulletMissedEvent
,Event
onDeath
public void onDeath(DeathEvent event)
- This method will be 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 you lose.
- Parameters:
event
- The event set by the game- See Also:
DeathEvent
,Event
onHitByBullet
public void onHitByBullet(HitByBulletEvent event)
- This method will be 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
public void onHitByBullet(HitByBulletEvent event) { out.println(event.getRobotName() " hit me!"); }
- Parameters:
event
- The event set by the game- See Also:
HitByBulletEvent
,Event
onHitRobot
public void onHitRobot(HitRobotEvent event)
- This method will be 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
public 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.
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.
- Parameters:
event
- The event set by the game- See Also:
HitRobotEvent
,Event
onHitWall
public void onHitWall(HitWallEvent event)
- This method will be called when your robot collides with a wall. You should override it in your robot if you want to be informed of this event. Note: 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(e.getBearing()) will point you perpendicular to the wall.
Example
public void onHitWall(HitWallEvent event) { out.println("Ouch, I hit a wall bearing " event.getBearing() " degrees."); }
- Parameters:
event
- The event set by the game- See Also:
HitWallEvent
,Event
onRobotDeath
public void onRobotDeath(RobotDeathEvent event)
- This method will be called if another robot dies You should override it in your robot if you want to be informed of this event.
- Parameters:
event
- The event set by the game- See Also:
RobotDeathEvent
,Event
onScannedRobot
public void onScannedRobot(ScannedRobotEvent event)
- This method will be called when your robot sees 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 will be called automatically if there is a robot in range of your radar.
The bearing is relative to your robot's heading.
Example
public 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. 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)
- Parameters:
event
- The event set by the game- See Also:
ScannedRobotEvent
,Event
onWin
public void onWin(WinEvent event)
- This method will be called if your robot wins a battle. You can do a victory dance here.
resume
public void resume()
- Resume the movement you stopped in stop(), if any. This call executes immediately, and does not return until it is complete.
- See Also:
stop()
scan
public void scan()
- Look for other robots. This method is called automatically by the game, as long as you are moving, turning, turning your gun, or turning your radar. There are 2 reasons to call scan() manually: 1 - You want to scan after you stop moving 2 - 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. Scan will cause
onScannedRobot(robocode.ScannedRobotEvent)
to be called if you see a robot.
setAdjustGunForRobotTurn
public void setAdjustGunForRobotTurn(boolean newAdjustGunForRobotTurn)
- Sets the gun to automatically turn the opposite way when the robot turns. Ok, so this needs some explanation: The gun is mounted on the robot. So, normally, if the robot turns 90 degrees to the right, then the gun will turn with it.
To compensate for this, you can call setAdjustGunForRobotTurn(true). When this is set, the gun will automatically turn in the opposite direction, so that it "stays still" when the robot turns.
Example, assuming both the robot and gun start out facing up (0 degrees):
setAdjustGunForRobotTurn(false); // This is the default turnRight(90); // At this point, both the robot and gun are facing right (90 degrees); turnLeft(90); // Both are back to 0 degrees -- or -- setAdjustGunForRobotTurn(true); turnRight(90); // At this point, the robot is facting right (90 degrees), but the gun is still facing up. turnLeft(90); // Both are back to 0 degrees.
Note: The gun compensating this way does count as "turning the gun". See
setAdjustRadarForGunTurn(boolean)
for details.- See Also:
setAdjustRadarForGunTurn(boolean)
setAdjustRadarForGunTurn
public void setAdjustRadarForGunTurn(boolean newAdjustRadarForGunTurn)
- Sets the radar to automatically turn the opposite way when the gun turns. Make sure you understand how
setAdjustGunForRobotTurn(boolean)
works before reading on...Ok, so now you understand
setAdjustGunForRobotTurn(boolean)
right?Just like the gun is mounted on the robot, the radar is mounted on the gun. So, normally, if the gun turns 90 degrees to the right, then the radar will turn with it.
To compensate for this (if you like), you can call setAdjustRadarForGunTurn(true). When this is set, the radar will automatically turn in the opposite direction, so that it "stays still" when the gun turns (in relation to the body, as of 0.97).
Example, assuming both the radar and gun start out facing up (0 degrees):
setAdjustRadarForGunTurn(false); // This is the default turnGunRight(90); // At this point, both the radar and gun are facing right (90 degrees); -- or -- setAdjustRadarForGunTurn(true); turnGunRight(90); // At this point, the gun is facing right (90 degrees), but the radar is still facing up.
Note: Calling setAdjustRadarForGunTurn will automatically call setAdjustRadarForRobotTurn with the same value, unless you have already called it yourself. This behavior is primarily for backward compatibility with older Robocode robots.
setColors
public void setColors(Color robotColor, Color gunColor, Color radarColor)
- Call this method to set your robot's colors. You may only call this method one time per battle. A null indicates the default (blue-ish) color.
Example: // Don't forget to import java.awt.Color at the top... import java.awt.Color; public void run() { setColors(Color.black,Color.red,new Color(150,0,150)); }
- Parameters:
robotColor
- Your robot's colorgunColor
- Your robot's gun colorradarColor
- Your robot's radar color- See Also:
Color
stop
public void stop()
- Stops all movement, and saves it for a call to resume(). If there is already movement saved from a previous stop, this will have no effect. This method is equivalent to stop(false); This call executes immediately.
- See Also:
stop(boolean)
,resume()
stop
public void stop(boolean overwrite)
- Stops all movement, and saves it for a call to resume(). If there is already movement saved from a previous stop, you can overwrite it by calling stop(true). This call executes immediately.
turnGunLeft
public void turnGunLeft(double degrees)
- Rotates your robot's gun. This call executes immediately, and does not return until it is complete.
Example
turnGunLeft(90);
turnGunRight
public void turnGunRight(double degrees)
- Rotates your robot's gun. This call executes immediately, and does not return until it is complete.
Example
turnGunRight(90);
turnRadarLeft
public void turnRadarLeft(double degrees)
- Rotates your robot's radar. This call executes immediately, and does not return until it is complete. Many robots will use the turnGun functions instead... the radar will rotate when the gun rotates, and you will probably want your gun facing in the same direction anyway.
Example
turnRadarLeft(90);
turnRadarRight
public void turnRadarRight(double degrees)
- Rotates your robot's radar. This call executes immediately, and does not return until it is complete. Many robots will use the turnGun functions instead... the radar will rotate when the gun rotates, and you will probably want your gun facing in the same direction anyway.
Example
turnRadarRight(90);
getEnergy
public double getEnergy()
- Returns the robot's current energy
- Returns:
- the robot's energy
setAdjustRadarForRobotTurn
public void setAdjustRadarForRobotTurn(boolean newAdjustRadarForRobotTurn)
- Sets the radar to automatically turn the opposite way when the robot turns.
The radar is mounted on the gun, which is mounted on the robot. So, normally, if the robot turns 90 degrees to the right, the gun turns, as does the radar.0
To compensate for this (if you like), you can call setAdjustRadarForRobotTurn(true). When this is set, the radar will automatically turn in the opposite direction, so that it "stays still" when the body turns.
Example, assuming the robot, gun, and radar all start out facing up (0 degrees):
setAdjustRadarForRobotTurn(false); // This is the default turnRight(90); // At this point, all three are facing right (90 degrees); -- or -- setAdjustRadarForRobotTurn(true); turnRight(90); // At this point, the robot and gun are facing right (90 degrees), but the radar is still facing up.