当前位置:网站首页>Robocode教程5——Enemy类
Robocode教程5——Enemy类
2022-04-23 05:45:00 【dawnsun001】
前面我们讲了第一个机器人Tiny,Tiny是继承自Robot这个类,可以看出Tiny智商有限,而几乎所有的战场机器人都继承自AdvancedRobot类,关于这两个类的区别我们以后再讲。首先我们还是从一个简单的高级机器人讲起。
在这个高级机器人中,首先我们要声明一个Enemy类,Enemy,就是敌人,Enemy类的作用是将敌人的信息封装起来供我们使用。
在onScannedRobot(ScannedRobotEvent e)方法中,雷达每次扫描到敌人时,都会执行这个方法,同时,ScannedRobotEvent e给我们提供了一些关于敌人的信息,这些信息是非常重要的,具体见API中,下图我列出一些
double getDistance() 返回离敌人的距离。
double getEnergy() 返回敌人的能量,由于每发出一个子弹是要消耗能量的,通过对敌人能量的检测,可以判断敌人的开火时间,从而采用相应的躲避策略,这是很普遍的做法。
double getHeading() 返回敌人的Heading,朝向,单位度
double getHeadingRadians() 返回敌人的Heading,朝向,单位弧度
String getName() 返回敌人名字
API中都很详细,大家自己看看吧
有了这些,我们来建立我们的Enemy类,首先分析Enemy有哪些数据成员:
private double x,y; //x,y坐标
private Stringname;//敌人名字
private double headingRadian; //朝向
private double bearingRadian; //相对于‘我’的方向,具体查看bearing和heading的区别
private double distance; //距离
private double direction; //绝对方向
private double velocity; //运动速度
private double energy; //能量值
当然这些都是基础的数据成员,到后面我们需要一些其他成员时可以再添加。
有了成员之后,我们就要考虑怎样给其赋值了,所以第一个成员方法为:
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方法的参数有两个,第一个为ScannedRobotEvent e,既扫描事件,包含敌人信息,第二个为AdvancedRobotme,包含‘我’的信息,后面遇到的方法基本上都要包含这两个方法,因为robocode战斗无非就是对‘敌人’和‘我’的数据进行处理。
具体操作都很简单,唯一要注意的是:
direction = bearingRadian +me.getHeadingRadians();
有一个direction变量,它是很常用的一个变量,几乎所有的中高级机器人都有用到。见图,在图中,因为目标在机器人heading方向的左边,所bearingRadian变量为负。根据此图不难算得directon = bearingRadian + headingRadian。可见direction就是以自己的机器人为圆心,垂直向上为0度,目标相对自己机
器人的角度。

所以我们完整(完整为暂时的,对于我们以后要讲的第一个高级机器已经足够)的Enemy类为:
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://blog.csdn.net/dawnsun2013/article/details/17718507
边栏推荐
- Rust 的多线程安全引用 Arc
- Algèbre linéaire chapitre 1 - déterminants
- The most practical chrome plug-in
- [leetcode 19] delete the penultimate node of the linked list
- Customized communication between threads (reentrantlock)
- 批量导出Arcgis属性表
- -- SQL query and return limit rows
- Advanced operation of idea debug
- Code neat way to learn
- Cf6d lizards and fundamentals 2 problem solving
猜你喜欢

Export of data

Mysql database foundation
![[leetcode 19] delete the penultimate node of the linked list](/img/ba/3c73fba8c4b4e3de7e506670144890.png)
[leetcode 19] delete the penultimate node of the linked list

SQL sorts according to the specified content

MySQL advanced query
![[leetcode 67] sum of two binary numbers](/img/91/afdd8197ca44ee910e2ee490929df1.png)
[leetcode 67] sum of two binary numbers

Database - sorting data

Solution to the trial of ycu Blue Bridge Cup programming competition in 2021

基于pygame库编写的五子棋游戏

Guaba and Computational Geometry
随机推荐
Basic knowledge of network in cloud computing
Detection technology and principle
程序设计训练
1. Calculate a + B
[leetcode 459] duplicate substring
@Problems caused by internal dead loop of postconstruct method
[untitled] database - limit the number of returned rows
Linux 用rpm的方式安装mysql(超简单)
Miscellaneous 1
MySQL best practices for creating tables
Generate excel template (drop-down selection, multi-level linkage)
St table template
SVN简单操作命令
Rust 的多线程安全引用 Arc
安全授信
Cf6d lizards and fundamentals 2 problem solving
几行代码教你爬取LOL皮肤图片
MySQL table constraints and table design
C language file operation
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition