Everything Robocode

robocode

Gun Control

Good link: http://mark.random-article.com/weber/java/robocode/lesson4.html

  The gun is perhaps the most essential part of every robot. Without it, you can not shoot the enemy, and therefore, not win. Some gun commands you can use:
 
  turnGunRight(int degrees);
  turnGunLeft(int degrees);
  fire(int power);
  Let us first examine the default gun code. This turns the gun around in a circle and fires when the scanner, which is mounted on the gun, sees the enemy. This is incredibly inefficient because it spends 99% of its time turning the gun around in a circle and only 1% of its time pointing at the enemy. This little code snippet should be placed inside your onScannedRobot. To implement it, you have to have extended AdvancedRobot and need to import robocode.util.Utils.

  setTurnGunRight(Utils.normalRelativeAngleDegrees(getHeading() + e.getBearing() - getGunHeading()));
 
  There! Now we have to fire at the enemy. As most of you already realized, it is a good idea to fire a large bullet when the enemy is close and a small one when he's far away. Therefor, we have:
 
  double bulletPower = 400 / e.getDistance(); // the farther away the enemy is, the more powerful the bullet.
  fire(bulletPower); // shoot him!

  So the total thing would look something like this:

  setTurnGunRight(Utils.normalRelativeAngleDegrees(getHeading() + e.getBearing() - getGunHeading()));
  double bulletPower = 400 / e.getDistance(); // the farther away the enemy is, the more powerful the bullet.
  fire(bulletPower); // shoot him!

You are viewing the text version of this site.

To view the full version please install the Adobe Flash Player and ensure your web browser has JavaScript enabled.

Need help? check the requirements page.


Get Flash Player