Not Painting When Firing

已查看 74 次
跳至第一个未读帖子

Vegeta

未读,
2020年10月22日 13:55:552020/10/22
收件人 robocode
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);
}
}

fnl

未读,
2020年10月22日 13:58:212020/10/22
收件人 robocode
This is my design.
If every robot paint itself, it will be hard to see what is going on on the battlefield. Hence, the developer needs to enable painting for the individual robot in the robot console.

This is described on this RoboWiki page:
https://robowiki.net/wiki/Robocode/Graphical_Debugging

Vegeta

未读,
2020年10月22日 14:58:282020/10/22
收件人 robo...@googlegroups.com
I have painting enabled as per the Wiki, but it doesn't paint anything the turn the robot fires.

--
You received this message because you are subscribed to a topic in the Google Groups "robocode" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robocode/tJdttapWeqM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robocode+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/robocode/921c3b8c-8ba5-4f5c-acd0-517795911f9fn%40googlegroups.com.

Vegeta

未读,
2020年10月22日 14:58:322020/10/22
收件人 robocode
When I run this robot, it still doesn't work. It seems to not paint the turn it fires and then double paints the turn after.

Davide Cappellini

未读,
2021年8月3日 09:25:302021/8/3
收件人 Robocode

I've tested your code myself and your problem is related with the way you move your radar and scan with it.
try to replace this code

// Robot main loop
while(true) {
   // Replace the next 4 lines with any behavior you would like
   if (!found) {
      turnGunRight(5);
   } else {
     scan();
   }
}


with this code:

// Robot main loop
while(true) {
   // Replace the next 4 lines with any behaviour you would like
   setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
   scan();
}


this simple code will make your radar spin forever and and every time the radar hits the target the paint (aka the latestScan variable) is updated.

This will make you see that the paint is not blocked by the firing but just painting the same not up-to-date latestScan variable giving the impression that the paint method hanged.

From the code itself I can see that you are at the beginning of this, don't give up and you will find a beautiful journey in front of you.
回复全部
回复作者
转发
0 个新帖子