我写了一个相对简单AdvancedRobot
的方法,它可以转动雷达并记录所有敌人的速度。最终,我注意到机器人在不应该错过的情况下错过了。我从 Robocode/Graphical Debugging wiki 复制了代码并对其进行了测试。这是代码(Wiki目前已关闭):
// The coordinates of the last scanned robot
int scannedX = Integer.MIN_VALUE;
int scannedY = Integer.MIN_VALUE;
// Called when we have scanned a robot
public void onScannedRobot(ScannedRobotEvent e) {
// Calculate the angle to the scanned robot
double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);
// Calculate the coordinates of the robot
scannedX = (int)(getX() + Math.sin(angle) * e.getDistance());
scannedY = (int)(getY() + Math.cos(angle) * e.getDistance());
}
和事件处理程序:
// Paint a transparent square on top of the last scanned robot
public void onPaint(Graphics2D g) {
// Set the paint color to a red half transparent color
g.setColor(new Color(0xff, 0x00, 0x00, 0x80));
// Draw a line from our robot to the scanned robot
g.drawLine(scannedX, scannedY, (int)getX(), (int)getY());
// Draw a filled square on top of the scanned robot that covers it
g.fillRect(scannedX - 20, scannedY - 20, 40, 40);
}
“实心方块”绝对不在机器人的顶部。下面显示了几个屏幕截图。看起来精度取决于距离,但我不确定。这是预期的,还是我做错了什么?