当前位置:网站首页>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
边栏推荐
- I/O多路复用及其相关详解
- ArcGIS table to excel exceeds the upper limit, conversion failed
- positioner
- Auto. JS custom dialog box
- The difference between deep copy and shallow copy
- Re regular expression
- Flash operates on multiple databases
- Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top
- 2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
- Go对文件操作
猜你喜欢
随机推荐
Process management command
C medium? This form of
k8s之实现redis一主多从动态扩缩容
Qtablewidget usage explanation
Cloud native Virtualization: building edge computing instances based on kubevirt
Go file operation
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
读取excel,int 数字时间转时间
Go的Gin框架学习
Rust: a simple example of TCP server and client
Gobang game based on pyGame Library
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
Docker installation MySQL
Implementation of object detection case based on SSD
Mode of interprocess communication
MySQL auto start settings start with systemctl start mysqld
Correct opening method of option
Vite configure proxy proxy to solve cross domain
587. Install fence / Sword finger offer II 014 Anagrams in strings
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资