Not Painting When Firing

66 views
Skip to first unread message

Vegeta

unread,
Oct 22, 2020, 1:55:55 PM10/22/20
to 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

unread,
Oct 22, 2020, 1:58:21 PM10/22/20
to 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

unread,
Oct 22, 2020, 2:58:28 PM10/22/20
to 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

unread,
Oct 22, 2020, 2:58:32 PM10/22/20
to 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

unread,
Aug 3, 2021, 9:25:30 AM8/3/21
to 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.
Reply all
Reply to author
Forward
0 new messages