|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectrobocode._Robot
robocode.Robot
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 | |
java.io.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 |
java.lang.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 (0 to getNumRounds()-1) 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 |
onPaint(java.awt.Graphics2D g)
This method is called every time the robot is painted if the robot painting feature is enabled on your robot. |
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(java.awt.Color robotColor,
java.awt.Color gunColor,
java.awt.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, uninitializedException |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public java.io.PrintStream out
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 |
public Robot()
Method Detail |
public void ahead(double distance)
Example
ahead(50);
distance
- The distance to move forwardonHitWall(robocode.HitWallEvent)
,
onHitRobot(robocode.HitRobotEvent)
public void back(double distance)
Example
back(150);
distance
- The distance to backwardonHitWall(robocode.HitWallEvent)
,
onHitRobot(robocode.HitRobotEvent)
public double getBattleFieldHeight()
public double getBattleFieldWidth()
public double getHeading()
public double getHeight()
public java.lang.String getName()
public double getWidth()
public double getX()
public double getY()
public void run()
Example
// A basic robot that moves around in a square public void run() { while (true) { ahead(100); turnRight(90); }
run
in interface java.lang.Runnable
public void turnLeft(double degrees)
Example
turnLeft(90);
degrees
- How many degrees to rotate left.public void turnRight(double degrees)
Example
turnRight(90);
degrees
- How many degrees to rotate right.public void doNothing()
public final void finalize()
public void fire(double power)
power
- The energy given to the bullet, and subtracted from your energy.fireBullet(double)
,
onBulletHit(robocode.BulletHitEvent)
,
onBulletHitBullet(robocode.BulletHitBulletEvent)
,
onBulletMissed(robocode.BulletMissedEvent)
public Bullet fireBullet(double power)
fire(double)
public double getGunCoolingRate()
getGunHeat()
public double getGunHeading()
public double getGunHeat()
public int getNumRounds()
public int getOthers()
public double getRadarHeading()
public int getRoundNum()
public long getTime()
public double getVelocity()
public void onBulletHit(BulletHitEvent event)
Example
public void onBulletHit(BulletHitEvent event) { out.println("I hit " + event.getName() + "!"); }
event
- The event set by the gameBulletHitEvent
,
Event
public void onBulletHitBullet(BulletHitBulletEvent event)
Example
public void onBulletHitBullet(BulletHitBulletEvent event) { out.println("I hit a bullet fired by " + event.getBullet().getName() + "!"); }
event
- The event set by the gameBulletHitBulletEvent
,
Event
public void onBulletMissed(BulletMissedEvent event)
Example
public void onBulletHit(BulletMissedEvent event) { out.println("Drat, I missed."); }
event
- The event set by the gameBulletMissedEvent
,
Event
public void onDeath(DeathEvent event)
event
- The event set by the gameDeathEvent
,
Event
public void onHitByBullet(HitByBulletEvent event)
Example
public void onHitByBullet(HitByBulletEvent event) { out.println(event.getRobotName() + " hit me!"); }
event
- The event set by the gameHitByBulletEvent
,
Event
public void onHitRobot(HitRobotEvent 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.
event
- The event set by the gameHitRobotEvent
,
Event
public void onHitWall(HitWallEvent event)
Example
public void onHitWall(HitWallEvent event) { out.println("Ouch, I hit a wall bearing " + event.getBearing() + " degrees."); }
event
- The event set by the gameHitWallEvent
,
Event
public void onRobotDeath(RobotDeathEvent event)
event
- The event set by the gameRobotDeathEvent
,
Event
public void onScannedRobot(ScannedRobotEvent event)
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)
event
- The event set by the gameScannedRobotEvent
,
Event
public void onWin(WinEvent event)
event
- The event set by the gameWinEvent
,
Event
public void resume()
stop()
public void scan()
onScannedRobot(robocode.ScannedRobotEvent)
to be called if you see a robot.
onScannedRobot(robocode.ScannedRobotEvent)
,
ScannedRobotEvent
public void setAdjustGunForRobotTurn(boolean newAdjustGunForRobotTurn)
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.
newAdjustGunForRobotTurn
- true
if the gun must move independent of the robot's turn;
false
if the gun must move dependent/relative to of the robot's turnsetAdjustRadarForGunTurn(boolean)
public void setAdjustRadarForGunTurn(boolean newAdjustRadarForGunTurn)
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.
newAdjustRadarForGunTurn
- true
if the radar must move independent of the gun's turn;
false
if the radar must move dependent/relative to of the gun's turnsetAdjustRadarForRobotTurn(boolean)
,
setAdjustGunForRobotTurn(boolean)
public void setColors(java.awt.Color robotColor, java.awt.Color gunColor, java.awt.Color radarColor)
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)); }
robotColor
- Your robot's colorgunColor
- Your robot's gun colorradarColor
- Your robot's radar colorColor
public void stop()
stop(boolean)
,
resume()
public void stop(boolean overwrite)
resume()
,
stop()
public void turnGunLeft(double degrees)
Example
turnGunLeft(90);
degrees
- How many degrees to rotate the gun left.public void turnGunRight(double degrees)
Example
turnGunRight(90);
degrees
- How many degrees to rotate the gun right.public void turnRadarLeft(double degrees)
Example
turnRadarLeft(90);
degrees
- How many degrees to rotate the radar left.public void turnRadarRight(double degrees)
Example
turnRadarRight(90);
degrees
- How many degrees to rotate the radar right.public double getEnergy()
public void setAdjustRadarForRobotTurn(boolean newAdjustRadarForRobotTurn)
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.
newAdjustRadarForRobotTurn
- true
if the radar must move independent of the robots's turn;
false
if the radar must move dependent/relative to of the robots's turnsetAdjustGunForRobotTurn(boolean)
,
setAdjustRadarForGunTurn(boolean)
public void onPaint(java.awt.Graphics2D g)
g
- The graphics context to use for painting
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |