Robot MethodsRobocode Robot API for .NET
The Robot type exposes the following members.
Methods

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

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.

Examples

// Move the robot 100 pixels forward
Ahead(100);

// Afterwards, move the robot 50 pixels backward
Ahead(-50);
Public methodBack
Immediately moves your robot backward by distance measured in pixels.

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.

Examples

// Move the robot 100 pixels backward
Back(100);

// Afterwards, move the robot 50 pixels forward
Back(-50);
Public methodDoNothing
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.
Public methodFire
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 GetBulletDamage(Double) for getting the damage that a bullet with a specific bullet power will do.

The specified bullet power should be between MIN_BULLET_POWER and MAX_BULLET_POWER.

Note that the gun cannot Fire if the gun is overheated, meaning that GunHeat returns a value > 0.

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

Examples

// Fire a bullet with maximum power if the gun is ready 
if (GetGunHeat() == 0)
{
    Fire(Rules.MAX_BULLET_POWER);
}
Public methodFireBullet
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 GetBulletDamage(Double) for getting the damage that a bullet with a specific bullet power will do.

The specified bullet power should be between MIN_BULLET_POWER and MAX_BULLET_POWER.

Note that the gun cannot Fire if the gun is overheated, meaning that GunHeat returns a value > 0.

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

Examples

// 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.Velocity;
    }
}
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.
Public methodOnBattleEnded
This method is called after end of the battle, even when the battle is aborted. You should override it in your robot if you want to be informed of this event.

Examples

public void OnBattleEnded(BattleEndedEvent evnt)
{
    Out.WriteLine("The battle has ended");
}
Public methodOnBulletHit
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.

Examples

public void OnBulletHit(BulletHitEvent evnt)
{
    Out.WriteLine("I hit " + evnt.Name + "!");
}
Public methodOnBulletHitBullet
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.

Examples

public void OnBulletHitBullet(BulletHitBulletEvent evnt)
{
    Out.WriteLine("I hit a bullet fired by " + evnt.Bullet.Name + "!");
}
Public methodOnBulletMissed
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.

Examples

public void OnBulletMissed(BulletMissedEvent evnt)
{
    Out.WriteLine("Drat, I missed.");
}
Public methodOnDeath
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.
Public methodOnHitByBullet
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.

Examples

void OnHitByBullet(HitByBulletEvent evnt)
{
    Out.WriteLine(event.RobotName + " hit me!");
}
Public methodOnHitRobot
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.

Examples

void OnHitRobot(HitRobotEvent evnt)
{
    if (event.Bearing > -90 && evnt.Bearing <= 90)
    {
        Back(100);
    }
    else
    {
        Ahead(100);
    }
}
public void OnHitRobot(HitRobotEvent evnt)
{
    if (event.Bearing > -90 && evnt.Bearing <= 90)
    {
        SetBack(100);
    }
    else
    {
        SetAhead(100);
    }
}
-- or perhaps, for a more advanced robot --

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 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.
Public methodOnHitWall
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(Double)Bearing will point you perpendicular to the wall.

Examples

void OnHitWall(HitWallEvent evnt)
{
    Out.WriteLine("Ouch, I hit a wall bearing " + evnt.Bearing + " degrees.");
}
Public methodOnKeyPressed
This method is called when a key has been pressed.

See the Sample.Interactive robot for an example of how to use key events.
Public methodOnKeyReleased
This method is called when a key has been released.

See the Sample.Interactive robot for an example of how to use key events.
Public methodOnKeyTyped
This method is called when a key has been typed (pressed and released).

See the Sample.Interactive robot for an example of how to use key events.
Public methodOnMouseClicked
This method is called when a mouse button has been clicked (pressed and released).

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMouseDragged
This method is called when a mouse button has been pressed and then dragged.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMouseEntered
This method is called when the mouse has entered the battle view.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMouseExited
This method is called when the mouse has exited the battle view.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMouseMoved
This method is called when the mouse has been moved.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMousePressed
This method is called when a mouse button has been pressed.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMouseReleased
This method is called when a mouse button has been released.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnMouseWheelMoved
This method is called when the mouse wheel has been rotated.

See the Sample.Interactive robot for an example of how to use mouse events.
Public methodOnPaint
This method is called every time the robot is painted. You should override this method if you want to draw items for your robot on the battle field, e.g. targets, virtual bullets etc.

This method is very useful for debugging your robot.

Note that the robot will only be painted if the "Paint" is enabled on the robot's console window; otherwise the robot will never get painted (the reason being that all robots might have graphical items that must be painted, and then you might not be able to tell what graphical items that have been painted for your robot).

Also note that the coordinate system for the graphical context where you paint items fits for the Robocode coordinate system where (0, 0) is at the bottom left corner of the battlefield, where X is towards right and Y is upwards.
Public methodOnRobotDeath
This method is called when another robot dies. You should override it in your robot if you want to be informed of this event.
Public methodOnRoundEnded
This method is called after the end of a round. You should override it in your robot if you want to be informed of this event.

Examples

public void OnRoundEnded(RoundEndedEvent event)
{
    Out.WriteLine("The round has ended");
}
Public methodOnScannedRobot
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 RADAR_SCAN_RADIUS (1200 pixels).

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

Examples

void OnScannedRobot(ScannedRobotEvent evnt)
{
    // Assuming radar and gun are aligned... 
    if (event.Distance < 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, Robot.Fire(double) 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(Double) 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).
Public methodOnStatus
This method is called every turn in a battle round in order to provide the robot status as a complete snapshot of the robot's current state at that specific time.

The main benefit of this method is that you'll automatically receive all current data values of the robot like e.g. the x and y coordinate, heading, gun heat etc., which are grouped into the exact same time/turn.

This is the only way to map the robots data values to a specific time. For example, it is not possible to determine the exact time of the robot's heading by calling first calling Time and then Heading afterwards, as the time might change after between the Time and Heading call.
Public methodOnWin
This method is called if your robot wins a battle.

Your robot could perform a victory dance here! :-)
Public methodResume
Immediately resumes the movement you stopped by Stop , if any.

This call executes immediately, and does not return until it is complete.
Public methodRun
The main method in every robot. You must override this to set up your robot's basic behavior.

Examples

// A basic robot that moves around in a square 
public void Run()
{
    while (true)
    {
        Ahead(100);
        TurnRight(90);
    }
}
Public methodScan
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.

There are 2 reasons to call Scan  manually:
  1. You want to scan after you stop moving.
  2. You want to interrupt the OnScannedRobot(ScannedRobotEvent) event. This is more likely. If you are in OnScannedRobot(ScannedRobotEvent) and call Scan , and you still see a robot, then the system will interrupt your OnScannedRobot(ScannedRobotEvent) event immediately and start it from the top.

This call executes immediately.
Public methodSetAllColors
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.

Examples

// Don't forget to using System.Drawing at the top... 
using System.Drawing;
...

public void Run()
{
    SetAllColors(Color.Red);
    ...
}
Public methodSetColors(Color, Color, Color)
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.

Examples

// Don't forget to using System.Drawing at the top... 
using System.Drawing;
...

public void Run()
{
    SetColors(null, Color.Red, Color.fromArgb(150, 0, 150));
    ...
}
Public methodSetColors(Color, Color, Color, Color, Color)
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.

Examples

// Don't forget to using System.Drawing at the top... 
using System.Drawing;
...

public void Run()
{
    SetColors(null, Color.Red, Color.Greeen, null, Color.fromArgb(150, 0, 150));
    ...
}
Public methodStop 
Immediately 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).
Public methodStop(Boolean)
Immediately 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).
Public methodTurnGunLeft
Immediately turns the robot's gun to the left by degrees.

This call executes immediately, and does not return until it is complete, i.e. when the angle remaining in the gun's turn is 0.

Note that both positive and negative values can be given as input, where negative values means that the robot's gun is set to turn right instead of left.

Examples

// Turn the robot's gun 180 degrees to the left
TurnGunLeft(180);

// Afterwards, turn the robot's gun 90 degrees to the right
TurnGunLeft(-90);
Public methodTurnGunRight
Immediately turns the robot's gun to the right by degrees. This call executes immediately, and does not return until it is complete, i.e. when the angle remaining in the gun's turn is 0.

Note that both positive and negative values can be given as input, where negative values means that the robot's gun is set to turn left instead of right.

Examples

// Turn the robot's gun 180 degrees to the right
TurnGunRight(180);

// Afterwards, turn the robot's gun 90 degrees to the left
TurnGunRight(-90);
Public methodTurnLeft
Immediately turns the robot's body to the left by 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.

Examples

// Turn the robot 180 degrees to the left
TurnLeft(180);

// Afterwards, turn the robot 90 degrees to the right
TurnLeft(-90);
Public methodTurnRadarLeft
Immediately turns the robot's radar to the left by degrees.

This call executes immediately, and does not return until it is complete, i.e. when the angle remaining in the radar's turn is 0.

Note that both positive and negative values can be given as input, where negative values means that the robot's radar is set to turn right instead of left.

Examples

// Turn the robot's radar 180 degrees to the left
TurnRadarLeft(180);

// Afterwards, turn the robot's radar 90 degrees to the right
TurnRadarLeft(-90);
Public methodTurnRadarRight
Immediately turns the robot's radar to the right by degrees. This call executes immediately, and does not return until it is complete, i.e. when the angle remaining in the radar's turn is 0.

Note that both positive and negative values can be given as input, where negative values means that the robot's radar is set to turn left instead of right.

Examples

// Turn the robot's radar 180 degrees to the right
TurnRadarRight(180);

// Afterwards, turn the robot's radar 90 degrees to the left
TurnRadarRight(-90);
Public methodTurnRight
Immediately turns the robot's body to the right by 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.

Examples

// Turn the robot 180 degrees to the right
TurnRight(180);

// Afterwards, turn the robot 90 degrees to the left
TurnRight(-90);
Back to Top
See Also