For some reason, the turn my robot fires, it doesn't seem to paint. Any fixes for this? Code is below.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import robocode.AdvancedRobot;
import robocode.Bullet;
import robocode.HitByBulletEvent;
import robocode.HitWallEvent;
import robocode.ScannedRobotEvent;
public class BasicRobot extends AdvancedRobot {
//Attributes
private int scanX, scanY;
private boolean found = false;
private ScannedRobotEvent latestScan = null;
/**
* run: BasicRobot's default behavior
*/
public void run() {
// Initialization of the robot should be put here
// After trying out your robot, try uncommenting the import at the top,
// and the next line:
setColors(Color.BLACK, Color.GRAY, Color.RED); // body,gun,radar
setScanColor(Color.WHITE);
// Robot main loop
while(true) {
// Replace the next 4 lines with any behavior you would like
if (!found) {
turnGunRight(5);
} else {
scan();
}
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
found = true;
latestScan = e;
// Replace the next line with any behavior you would like
if (getGunHeat() == 0)
fireBullet(3);
}
private void drawScannedRobot(ScannedRobotEvent e) {
double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);
scanX = (int) (getX() + Math.sin(angle) * e.getDistance());
scanY = (int) (getY() + Math.cos(angle) * e.getDistance());
Graphics2D g = this.getGraphics();
g.setColor(new Color(0xff, 0x00, 0x00, 0x80));
g.drawLine((int) getX(), (int) getY(), scanX, scanY);
int w = (int) getWidth();
g.fillRect(scanX - w/2, scanY - w/2, 40, 40);
}
public void onPaint(Graphics2D g) {
g.setStroke(new BasicStroke(2));
g.setColor(Color.GRAY);
int r = (int) (getWidth() * 1.5);
if (latestScan != null)
drawScannedRobot(latestScan);
}
private void draw(Graphics2D g) {
g.setStroke(new BasicStroke(2));
g.setColor(Color.GRAY);
int r = (int) (getWidth() * 1.5);
if (latestScan != null)
drawScannedRobot(latestScan);
}
}