wiki:ControlSystems/SoftwareTeam/Training/VirtualTraining/HelloRoboCode

Build a Bot

The real fun of RoboCode is that it lets you build your own robot and program it in Java. Let's build a simple robot and battle it against one of the trainer bots: Robot Editor

  1. Launch RoboCode and choose Robot->Source editor
  2. If asked, answer Yes, then OK to use the Java language compiler you installed

You're now in the Robot Editor where you can create the program for your robot.

  1. Choose File->New->Junior Robot
  2. Give your robot a name like RoboJojo - note that the name must start with an upper-case letter.
  3. Create a package name for your robot. Packages are like teams of robots; you can create many robots and add all of them to your package. Give it a unique name that's easy to identify so when you are doing battles with teammates and in competitions, it's easy to identify your robots. For example: daa2537 (your initials and team)




The editor starts with a pre-created super-simple robot that you can modify and extend. here's the code for the robot:

package daa2537;
import robocode.*;

// API help : https://robocode.sourceforge.io/docs/robocode/robocode/JuniorRobot.html

/** RoboJojo - a robot by (your name here)*/
public class RoboJojo extends JuniorRobot
{
	/** run: RoboJojo's default behavior */
	public void run() {
		// Initialization of the robot should be put here

		// Some color codes: blue, yellow, black, white, red, pink, brown, grey, orange...
		// Sets these colors (robot parts): body, gun, radar, bullet, scan_arc
		setColors(orange, blue, white, yellow, black);

		// Robot main loop
		while(true) {
			// Replace the next 4 lines with any behavior you would like
			ahead(100);
			turnGunRight(360);
			back(100);
			turnGunRight(360);
		}
	}

	/** onScannedRobot: What to do when you see another robot */
	public void onScannedRobot() {
		// Replace the next line with any behavior you would like
		fire(1);
	}

	/** onHitByBullet: What to do when you're hit by a bullet */
	public void onHitByBullet() {
		// Replace the next line with any behavior you would like
		back(10);
	}
	
	/** onHitWall: What to do when you hit a wall */
	public void onHitWall() {
		// Replace the next line with any behavior you would like
		back(20);
	}	
}

Let's use it as-is to see what it does. We'll review it in the next lesson to understand the code better. RoboCode Compilation

  1. Save the robot: File->Save accept the default filename (e.g. RoboJojo.java)
  2. Compile the robot Compiler->Compile and press OK when the compilation finishes. Compiling a program converts the text of your robot program into a program the computer can understand and battle in the arena.
  3. Close the Robot Editor

Finally, create a new battle and test your robot against one of the supplied trainers. When you choose robots, you'll see a new package under Available Robots (e.g. daa2537) and the robot you created (e.g. RoboMojo) in the list of robots. Add it to the battle and then choose add of the trainer robots from the sample package. Finally, start your battle!


RoboJojo Battle 1

Last modified 9 months ago Last modified on Aug 31, 2020, 10:37:03 PM

Attachments (3)

Download all attachments as: .zip