贴吧用户_0ECVeNM 梅赛德斯丁字裤
关注数: 13 粉丝数: 104 发帖数: 3,931 关注贴吧数: 8
求助,Robocode雷达锁定之后不能移动 package tank; import java.awt.Color; import java.util.Random; import robocode.AdvancedRobot; import robocode.ScannedRobotEvent; public class WjwRobot3 extends AdvancedRobot { Enemy enemy = new Enemy(); double movementDriection = 1; double previousEnergy = 100; final double PI = Math.PI; double firePower; int direction = 1; Random rand = new Random(); int move = rand.nextInt(200); double goHeading; private double radarMove = 45; private void doFirePower() { firePower = 400 / enemy.distance; } private void doGun() { long time = getTime() + (int) (enemy.distance / (20 - (3 * firePower))); double gunOffset = this.getGunHeadingRadians() - absbearing(getX(), getY(), enemy.guessX(time), enemy.guessY(time)); this.setTurnGunLeftRadians(NormaliseBearing(gunOffset)); } public double absbearing(double x1, double y1, double x2, double y2) { double xo = x2 - x1; double yo = y2 - y1; double h = getrange(x1, y1, x2, y2); if (xo > 0 && yo > 0) { return Math.asin(xo / h); } if (xo > 0 && yo < 0) { return Math.PI - Math.asin(xo / h); } if (xo < 0 && yo < 0) { return Math.PI + Math.asin(-xo / h); } if (xo < 0 && yo > 0) { return 2.0 * Math.PI - Math.asin(-xo / h); } return 0; } public double getrange(double x1, double y1, double x2, double y2) { double xo = x2 - x1; double yo = y2 - y1; double h = Math.sqrt(xo * xo + yo * yo); return h; } public double NormaliseBearing(double ang) { if (ang > PI) ang -= 2 * PI; if (ang < -PI) ang += 2 * PI; return ang; } public void run() { setAdjustGunForRobotTurn(true); setAdjustRadarForGunTurn(true); this.setColors(Color.red, Color.blue, Color.yellow, Color.black, Color.green); while (true) { thRun(); NotWall(); doFirePower(); doGun(); out.println(enemy.distance); if (enemy.name == null) { setTurnRadarRightRadians(2 * PI); } fire(firePower); execute(); } } // 被动移动方式 public void onBulletMissed() { thRun(); } public void onBulletHit() { thRun(); } public void onHitByBullet() { thRun(); } public void onHitRobot() { thRun(); } // 雷达扫描 public void onScannedRobot(ScannedRobotEvent e) { enemy.update(e, this); double Offset = rectify(enemy.direction - getRadarHeadingRadians()); setTurnRadarRightRadians(Offset * 1.5); fire(2); } // 角度修正方法,重要 public double rectify(double angle) { if (angle < -Math.PI) angle += 2 * Math.PI; if (angle > Math.PI) angle -= 2 * Math.PI; return angle; } public class Enemy { public double x, y; public long ctime; public double head; public double speed; public String name = 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 prevHeadingRadian = 0.0D; public double energy = 100.0D; public double guessX(long when) { long diff = when - ctime; return x + Math.sin(head) * speed * diff; } public double guessY(long when) { long diff = when - ctime; return y + Math.cos(head) * speed * diff; } 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; } } // 移动函数 public void thRun() { if (getBattleFieldWidth() == getBattleFieldHeight()) { ahead(rand.nextInt((int) (getBattleFieldWidth() / 3.0))); NotWall(); } else if (getBattleFieldWidth() > getBattleFieldHeight()) { ahead(rand.nextInt((int) (getBattleFieldHeight() / 3.0))); NotWall(); } else { ahead(rand.nextInt((int) (getBattleFieldWidth() / 3.0))); NotWall(); } goHeading = rand.nextInt(360); turnLeft(goHeading); NotWall(); } // 不撞墙函数 public void NotWall() { double heading = getHeadingRadians(); // 取得bot方向的弧度数 double x = getX() + move * Math.sin(heading); // 移动move后将要达到的x坐标 double y = getY() + move * Math.cos(heading); // 移动move后将要达到的y坐标 double dWidth = getBattleFieldWidth(); // 战场的宽度 double dHeight = getBattleFieldHeight(); // 战场的长度 // 当(x,y)超过指定的范围,则反向移动move if (x < 30 || x > dWidth - 30 || y < 30 || y > dHeight - 30) { setBack(move); } turnRadarLeft(radarMove); // 转动雷达 } }
首页 1 2 下一页