5 – Bounce Off the Walls

When your robot collides with the arena boundary, the onHitWall method runs.

What happens?

  • Robocode alerts your bot that it hit a wall.
  • We’ll turn around and drive away to stay in the fight.

Code

@Override
public void onHitWall(HitWallEvent botHitWallEvent) {
    // 1. Turn left 180° away from the wall
    turnLeft(180);
    // 2. Move forward to escape
    forward(100);
}

Parameters

  • botHitWallEvent – event details from the game.

Why it works

  • Turning around keeps your bot from getting stuck.
  • Moving forward puts space between you and the wall.

⬅️ Back: HitByBulletEvent ➡️ Next: Day 5