当前位置:网站首页>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
边栏推荐
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- Re expression régulière
- C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
- 2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
- Rewrite four functions such as StrCmp in C language
- MySQL_01_简单数据检索
- ES6
- Build openstack platform
- 2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
- Amount input box, used for recharge and withdrawal
猜你喜欢
.104History
Map basemap Library
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
Installation du docker redis
re正則錶達式
Auto.js 自定义对话框
Re regular expression
Data stream encryption and decryption of C
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
随机推荐
Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
C# 的数据流加密与解密
C [file operation] read TXT text by line
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
C medium? This form of
MySQL auto start settings start with systemctl start mysqld
JS high frequency interview questions
C1 notes [task training chapter I]
ArcGIS table to excel exceeds the upper limit, conversion failed
Qt读写XML文件(含源码+注释)
Tensorflow tensor introduction
What are the relationships and differences between threads and processes
Welcome to the markdown editor
Timestamp to formatted date
Correct opening method of option
Identification verification code
7-21 wrong questions involve knowledge points.
C1 notes [task training part 2]
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
Re expression régulière