当前位置:网站首页>Robocode tutorial 5 - enemy class

Robocode tutorial 5 - enemy class

2022-04-23 18:05:00 dawnsun001

We talked about the first robot Tiny,Tiny It is inherited from Robot This class , It can be seen that Tiny Limited IQ , And almost all battlefield robots are inherited from AdvancedRobot class , We'll talk about the difference between the two classes later . First of all, let's start with a simple advanced robot .

In this advanced robot , First of all, we want to declare a Enemy class ,Enemy, It's the enemy ,Enemy Class is used to encapsulate the enemy's information for our use .

stay onScannedRobot(ScannedRobotEvent e) In the method , Every time the radar scans the enemy , Will execute this method , meanwhile ,ScannedRobotEvent e Gave us some information about the enemy , This information is very important , Specific view API in , Below I list some

double      getDistance()   Return the distance from the enemy .

double      getEnergy()    Return the enemy's energy , Because every bullet fired consumes energy , Through the detection of enemy energy , Can judge the enemy's firing time , So as to adopt the corresponding avoidance strategy , This is a very common practice .

double      getHeading()  Return to the enemy's Heading, toward , The unit of

double  getHeadingRadians() Return to the enemy's Heading, toward , Unit radian

String   getName() Return the enemy's name

 

API Very detailed , Let's see for ourselves

 

With these , Let's build our Enemy class , First analysis Enemy What data members are there :

   private double x,y; //x,y coordinate

   private Stringname;// Enemy name

   private double headingRadian; // toward

   private double bearingRadian; // be relative to ‘ I ’ The direction of , Specific to see bearing and heading The difference between

   private double distance; // distance

   private double direction; // Absolute direction

   private double velocity; // Speed of motion

   private double energy; // Energy value

 

Of course, these are basic data members , Later, when we need some other members, we can add .

 

With members , We have to think about how to assign it , So the first member method is :

   public void update(ScannedRobotEvent e,AdvancedRobot me){

      name = e.getName();

      headingRadian = e.getHeadingRadians();

      bearingRadian = e.getBearingRadians();

      this.energy = e.getEnergy();

      this.velocity = e.getVelocity();

      this.distance = e.getDistance();

      direction = bearingRadian + me.getHeadingRadians();

      x = me.getX() + Math.sin(direction ) * distance;

      y= me.getY() + Math.cos(direction ) * distance;  

}

Updata Method has two parameters , The first is ScannedRobotEvent e, Both scan Events , Contains enemy information , The second is AdvancedRobotme, contain ‘ I ’ Information about , The methods encountered later basically include these two methods , because robocode Fighting is nothing more than to ‘ The enemy ’ and ‘ I ’ Data processing .

The specific operation is very simple , The only thing to notice is :

direction = bearingRadian +me.getHeadingRadians();

There is one direction Variable , It is a very common variable , Almost all intermediate and advanced robots are useful to . See the picture , In the picture , Because the target is the robot heading The left side of the direction , the bearingRadian The variable is negative . According to this figure, it is not difficult to calculate directon = bearingRadian + headingRadian. so direction Is to take their own robot as the center of the circle , Vertically up is 0 degree , The target is relative to its own machine

The angle of the machine man .


So we are complete ( Complete for temporary , For the first advanced machine we will talk about later, it is enough ) Of Enemy Class is :

public class Enemy {

          public double x,y;

          public Stringname = null;

          public double headingRadian = 0.0D;

          public double bearingRadian = 0.0D;

          public double distance = 1000D;

          public double direction = 0.0D;

          public double velocity = 0.0D;

          public double energy = 100.0D;

          

     public void update(ScannedRobotEvent e,AdvancedRobot me){

             name = e.getName();

             headingRadian =e.getHeadingRadians();

             bearingRadian =e.getBearingRadians();

             this.energy = e.getEnergy();

             this.velocity = e.getVelocity();

             this.distance = e.getDistance();

             direction = bearingRadian +me.getHeadingRadians();

             x = me.getX() + Math.sin(direction) *distance;

             y=  me.getY() + Math.cos(direction ) * distance;     

           }

       }

版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289481.html