Difference between revisions of "Robocode/My First Robot"

From Robowiki
Jump to navigation Jump to search
m (Add images)
 
(16 intermediate revisions by 9 users not shown)
Line 1: Line 1:
This is the classic '''My First Robot Tutorial''' that tells how to create your first robot.
+
[[Image:MatBot.jpg|thumb|MatBot]]
 +
This is the classic '''My First Robot Tutorial''' that tells how to create your first robot. Here's the fun stuff: This is what [[Robocode]] is all about!
  
== Creating a Robot ==
+
Creating a robot can be easy. Making your robot a winner is not. You can spend only a few minutes on it, or you can spend months and months. I'll warn you that writing a robot can be addictive! Once you get going, you'll watch your creation as it goes through growing pains, making mistakes and missing critical shots. But as you learn, you'll be able to teach your robot how to act and what to do, where to go, who to avoid, and where to fire. Should it hide in a corner, or jump into the fray?
Here's the fun stuff: This is what [[Robocode]] is all about!
 
  
{|
+
== Getting started ==
| Creating a robot can be easy. Making your robot a winner is not. You can spend only a few minutes on it, or you can spend months and months. I'll warn you that writing a robot can be addictive! Once you get going, you'll watch your creation as it goes through growing pains, making mistakes and missing critical shots. But as you learn, you'll be able to teach your robot how to act and what to do, where to go, who to avoid, and where to fire. Should it hide in a corner, or jump into the fray?
 
|| [[Image:MatBot.jpg|Picture of the robot named MatBot, which is one Mathew Nelson's robots]]
 
|}
 
 
 
= My First Robot =
 
 
Ready to create your first robot? I hope you'll find that it's easy, straightforward, fun, and addictive!
 
Ready to create your first robot? I hope you'll find that it's easy, straightforward, fun, and addictive!
  
Line 17: Line 12:
  
 
== The Robot Editor ==
 
== The Robot Editor ==
The first step is to open up the Robot Editor. From the main Robocode screen, click on the '''Robot''' menu, then select '''Editor'''.
+
[[Image:EditorNew.png|thumb|The new-robot dialog]]
 +
The first step is to open up the Robot Editor. From the main Robocode screen, click on the '''Robot''' menu, then select '''Source Editor'''.
  
 
When the editor window comes up, click on the '''File''' menu, then select '''New Robot'''.
 
When the editor window comes up, click on the '''File''' menu, then select '''New Robot'''.
Line 25: Line 21:
 
Voila! You now see the code for your own robot.
 
Voila! You now see the code for your own robot.
  
== A New Robot ==
+
== A new robot ==
This is what you should be looking at (names have been changed to protect the innocent):
+
[[Image:EditorMyFirstRobot.png|thumb|Editing your new robot]]
 +
This is what you should be looking at:
  
  package man;
+
<syntaxhighlight lang="java" line highlight="8-11,16">
  import robocode.*;
+
package pkg;
  &nbsp;
 
  public class MyFirstRobot extends Robot {
 
      public void run() {<span style="color:green"><b>
 
          while (true) {
 
              ahead(100);
 
              turnGunRight(360);
 
              back(100);
 
              turnGunRight(360);
 
          }</b></span>
 
      }
 
  &nbsp;<span style="color:green"><b>
 
      public void onScannedRobot(ScannedRobotEvent e) {
 
          fire(1);
 
      }</b></span>
 
  }
 
  
We're only concerned with the bits in '''<span style="color:green">bold</span>''' here... you won't need to change anything else. Not that much, right?
+
import robocode.*;
  
By the way, if you're REALLY concerned about the rest of it, right now, I describe it here:
+
public class YourRobotNameHere extends Robot {
 +
    public void run() {
 +
        while (true) {
 +
            ahead(100);
 +
            turnGunRight(360);
 +
            back(100);
 +
            turnGunRight(360);
 +
        }
 +
    }
  
  package man;
+
    public void onScannedRobot(ScannedRobotEvent e) {
  import robocode.*;
+
        fire(1);
  &nbsp;
+
    }
  public class MyFirstRobot extends Robot {
+
}
      public void run() {
+
</syntaxhighlight>
      }
 
  }
 
  
{| border="1" style="text-align:left"
+
We're only concerned with lines 8-11 and 16 here... you won't need to change anything else. Not that much, right?
! import robocode.*;
+
 
| Tells Java that you're going to use Robocode objects in your robot.
+
By the way, if you're '''really''' concerned about the rest of it, here it is:
|-
+
 
! public class MyFirstRobot extends Robot
+
* <code>import robocode.*;</code> ‒ Says that you're going to use Robocode objects in your robot.
| Tells Java: "The object I'm describing here is a type of [[Robot]], named MyFirstRobot".
+
* <code>public class MyFirstRobot extends Robot</code> ‒ Says the object I'm describing here is a type of <code>[[Robot]]</code>, named <code>MyFirstRobot</code>.
|-
+
* <code>public void run() { ... }</code> ‒ The game calls your run() method when the battle begins.
! public void run() { }
+
* <code>{ ... }</code> ‒ "Curly brackets" ( '''{ }''' ) group things together. In this case, they're grouping together all the code for the robot.
| The game calls your run() method when the battle begins.
 
|-
 
! { }
 
| "Curly brackets" ( '''{ }''' ) group things together. In this case, they're grouping together all the code for the robot.
 
|}
 
  
 
== Let's move somewhere ==
 
== Let's move somewhere ==
Line 77: Line 60:
 
First, we'll examine the run() method:  
 
First, we'll examine the run() method:  
  
  while (true) {
+
<syntaxhighlight lang="java">
      ahead(100);
+
while(true) {
      turnGunRight(360);
+
    ahead(100);
      back(100);
+
    turnGunRight(360);
      turnGunRight(360);
+
    back(100);
  }
+
    turnGunRight(360);
 +
}
 +
</syntaxhighlight>
  
<code>while(true) { }</code> means: "While the condition <code>true</code> is true, do everything between the curly brackets { }".
+
<code>while(true) { ... }</code> means: "Do the stuff inside my curly brackets, forever".  
 
 
Since <code>true</code> is always true (no kidding? ;-), it means: "Do the stuff inside my curly brackets, forever".  
 
  
 
So this robot will:
 
So this robot will:
# move ahead 100 pixels
+
# Move ahead 100 pixels.
# turn the gun right by 360 degrees
+
# Turn the gun right by 360 degrees.
# move back 100 pixels
+
# Move back 100 pixels.
# turn the gun left/back by 360 degrees
+
# Turn the gun right by 360 degrees again.
  
 
The robot will continue doing this over and over and over, until it dies, due to the <code>while(true)</code> statement.
 
The robot will continue doing this over and over and over, until it dies, due to the <code>while(true)</code> statement.
Line 98: Line 81:
 
Not so bad, right?
 
Not so bad, right?
  
== Fire at Will! ==
+
== Fire at will! ==
 
When our radar scans a robot, we want to fire:
 
When our radar scans a robot, we want to fire:
  
  public void onScannedRobot(ScannedRobotEvent e) {
+
<syntaxhighlight lang="java">
      fire(1);
+
public void onScannedRobot(ScannedRobotEvent e) {
  }
+
    fire(1);
 +
}
 +
</syntaxhighlight>
  
The game calls your '''onScannedRobot''' method whenever you can see another robot.
+
The game calls your <code>onScannedRobot()</code> method whenever ‒ during one of the actions ‒ you see another robot.
It sends along an event that can tell us lots of information about the robot -- its name, how much life it has, where it is, where it's heading, how fast it's going, etc.
+
It sends along an event that can tell us lots of information about the robot its name, how much life it has, where it is, where it's heading, how fast it's going, etc.
  
 
However, since this is a simple robot, we're not going to look at any of that stuff. Let's just fire!
 
However, since this is a simple robot, we're not going to look at any of that stuff. Let's just fire!
Line 115: Line 100:
 
Now, compile it by selecting '''Compile''' in the '''Compiler''' menu.
 
Now, compile it by selecting '''Compile''' in the '''Compiler''' menu.
  
If your robot compiles without any errors, you can start a new battle with your robot. Start a new battle by selecting '''New''' in the '''Battle''' menu. If you cannot see your robot, you might have to refresh the list of robots by '''pressing F5'''. Add your robot to the battle together with at least one other robot as e.g. sample.Target, and press the '''Start Battle''' button to let the games begin!
+
If your robot compiles without any errors, you can start a new battle with your robot. Start a new battle by selecting '''New''' in the '''Battle''' menu. (If you cannot see your robot, you might have to refresh the list of robots by selecting Options -> Clean robot cache). Add your robot to the battle together with at least one other robot as e.g. sample.Target, and press the '''Start Battle''' button to let the games begin!
  
 
== What's next? ==
 
== What's next? ==
 
You should have a look at all the sample robots to see how certain things are done.
 
You should have a look at all the sample robots to see how certain things are done.
 +
 +
After you have gotten used to Robocode, you will probably want to switch your robot to an <code>AdvancedRobot</code>. Read the [[Robocode/FAQ|FAQ]] to learn what that is.
 +
 +
After that, you'll have to decide whether your bot will be a [[One on One|1v1]] bot (a duelist), or a [[melee]] bot (fights multiple opponents). One-on-one bots are simpler, and you'll want to start with them.
  
 
You'll eventually want to look at the [http://robocode.sourceforge.net/docs/robocode/ Robocode API] to see all the other things your robot can do.
 
You'll eventually want to look at the [http://robocode.sourceforge.net/docs/robocode/ Robocode API] to see all the other things your robot can do.
Line 126: Line 115:
 
== See also ==
 
== See also ==
  
=== Robot API ===
+
{{RobocodeDocsList}}
* [http://robocode.sourceforge.net/docs/robocode/ Robot API]
 
 
 
=== Tutorials ===
 
* [[Robocode/System Requirements|System Requirements for Robocode]]
 
* [[Robocode/Download|How to download and install Robocode]]
 
* [[Robocode/Robot Anatomy|The anatomy of a robot]]
 
* [[Robocode/Getting Started|Getting started with Robocode]]
 
* [[Robocode/Game Physics|Robocode Game Physics]]
 
* [[Robocode/Scoring|Scoring in Robocode]]
 
* [[Robocode/Robot Console|Using the robot console]]
 
* [[Robocode/Downloading_Robots|Downloading other robots]]
 
* [[Robocode/Learning from Robots|Learning from other robots]]
 
* [[Robocode/Package Robot|Package your robot]]
 
* [[Robocode/FAQ|Frequently Asked Questions (FAQ)]]
 
* [[Robocode/Articles|Articles about Robocode]]
 
* [[Robocode/Console Usage|Starting Robocode from the command line]]
 
* [[Robocode/Graphical_Debugging|Graphical debugging]]
 
* [[Robocode/Eclipse|Using Eclipse as IDE]]
 
* [[Robocode/Eclipse/Create_a_Project|Creating a project for your robots]]
 
* [[Robocode/Eclipse/Create_a_Robot|Creating a robot in Eclipse]]
 
* [[Robocode/Running from Eclipse|Running your robot from Eclipse]]
 
* [[Robocode/Eclipse/Debugging Robot|Debugging your robot with Eclipse]]
 
 
 
=== News and Releases ===
 
* [http://sourceforge.net/export/rss2_project.php?group_id=37202 RSS Feeds for the Robocode project]
 
* [http://sourceforge.net/project/showfiles.php?group_id=37202&package_id=29609 Robocode file releases]
 
 
 
=== Home pages ===
 
* [http://robocode.sourceforge.net/ Classic homepage]
 
* [http://sourceforge.net/projects/robocode Robocode project at SourceForge]
 
* [http://robocoderepository.com/ Robocode Repository]
 
* [[wikipedia:Robocode|Wikipediaentry for Robocode]]
 
  
 
[[Category:Robocode Documentation]]
 
[[Category:Robocode Documentation]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 23:17, 16 September 2017

MatBot

This is the classic My First Robot Tutorial that tells how to create your first robot. Here's the fun stuff: This is what Robocode is all about!

Creating a robot can be easy. Making your robot a winner is not. You can spend only a few minutes on it, or you can spend months and months. I'll warn you that writing a robot can be addictive! Once you get going, you'll watch your creation as it goes through growing pains, making mistakes and missing critical shots. But as you learn, you'll be able to teach your robot how to act and what to do, where to go, who to avoid, and where to fire. Should it hide in a corner, or jump into the fray?

Getting started

Ready to create your first robot? I hope you'll find that it's easy, straightforward, fun, and addictive!

Robocode ships with a number of sample robots that you can look at for ideas, and to see how things work. You can use the Robot Editor to look at all of them.

In this section, we'll use the Robot Editor to create your very own, brand new robot.

The Robot Editor

The new-robot dialog

The first step is to open up the Robot Editor. From the main Robocode screen, click on the Robot menu, then select Source Editor.

When the editor window comes up, click on the File menu, then select New Robot.

In the dialogs that follow, type in a name for your robot, and enter your initials.

Voila! You now see the code for your own robot.

A new robot

Editing your new robot

This is what you should be looking at:

 1 package pkg;
 2 
 3 import robocode.*;
 4 
 5 public class YourRobotNameHere extends Robot {
 6     public void run() {
 7         while (true) {
 8             ahead(100);
 9             turnGunRight(360);
10             back(100);
11             turnGunRight(360);
12         }
13     }
14 
15     public void onScannedRobot(ScannedRobotEvent e) {
16         fire(1);
17     }
18 }

We're only concerned with lines 8-11 and 16 here... you won't need to change anything else. Not that much, right?

By the way, if you're really concerned about the rest of it, here it is:

  • import robocode.*; ‒ Says that you're going to use Robocode objects in your robot.
  • public class MyFirstRobot extends Robot ‒ Says the object I'm describing here is a type of Robot, named MyFirstRobot.
  • public void run() { ... } ‒ The game calls your run() method when the battle begins.
  • { ... } ‒ "Curly brackets" ( { } ) group things together. In this case, they're grouping together all the code for the robot.

Let's move somewhere

Let's add a couple lines so that it will do something.

First, we'll examine the run() method:

while(true) {
    ahead(100);
    turnGunRight(360);
    back(100);
    turnGunRight(360);
}

while(true) { ... } means: "Do the stuff inside my curly brackets, forever".

So this robot will:

  1. Move ahead 100 pixels.
  2. Turn the gun right by 360 degrees.
  3. Move back 100 pixels.
  4. Turn the gun right by 360 degrees again.

The robot will continue doing this over and over and over, until it dies, due to the while(true) statement.

Not so bad, right?

Fire at will!

When our radar scans a robot, we want to fire:

public void onScannedRobot(ScannedRobotEvent e) {
    fire(1);
}

The game calls your onScannedRobot() method whenever ‒ during one of the actions ‒ you see another robot. It sends along an event that can tell us lots of information about the robot ‒ its name, how much life it has, where it is, where it's heading, how fast it's going, etc.

However, since this is a simple robot, we're not going to look at any of that stuff. Let's just fire!

Compile your robot

First, save your robot by selecting the Save in the File menu. Follow the prompts to save your robot.

Now, compile it by selecting Compile in the Compiler menu.

If your robot compiles without any errors, you can start a new battle with your robot. Start a new battle by selecting New in the Battle menu. (If you cannot see your robot, you might have to refresh the list of robots by selecting Options -> Clean robot cache). Add your robot to the battle together with at least one other robot as e.g. sample.Target, and press the Start Battle button to let the games begin!

What's next?

You should have a look at all the sample robots to see how certain things are done.

After you have gotten used to Robocode, you will probably want to switch your robot to an AdvancedRobot. Read the FAQ to learn what that is.

After that, you'll have to decide whether your bot will be a 1v1 bot (a duelist), or a melee bot (fights multiple opponents). One-on-one bots are simpler, and you'll want to start with them.

You'll eventually want to look at the Robocode API to see all the other things your robot can do.

Above all, good luck, have fun, and enjoy!

See also

Robocode API

Beginner Guides

External Editors

.NET Robots

Links