로보코드 내 첫 로봇 만들기

로그[log]/로보코드 2010. 11. 1. 23:53



파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음
728x90
'

 

로보코드 배틀을 위한 내 로봇을 만드는 방법에 대해 소개 하겠습니다.

내 로봇을 만드는것은 비교적 간단합니다.


로보코드 에디터

첫 단계는 로보코드 에디터를 실행하는것입니다.

로보코드 메인화면에서 Robot 메뉴를 누르고 Editor 를 선택합니다.

Editor 화면이 올라오면 File 메뉴를 선택하고 New Robot 을 클릭합니다.

다이얼로그의 지시에 따라 내 로봇의 이름과 패키지명을 입력하면 내 로봇의 소스파일을 얻을 수 있습니다.



A New Robot

 package man;
 import robocode.*;
  
 public class MyFirstRobot extends Robot {
     public void run() {
         while (true) {
             ahead(100);
             turnGunRight(360);
             back(100);
             turnGunRight(360);
         }
     }
  
     public void onScannedRobot(ScannedRobotEvent e) {
         fire(1);
     }
 }
 

We're only concerned with the bits in bold 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, right now, I describe it here:

 package man;
 import robocode.*;
  
 public class MyFirstRobot extends Robot {
     public void run() {
     }
 }
import robocode.*; Tells Java that you're going to use Robocode objects in your robot.
public class MyFirstRobot extends Robot Tells Java: "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: "While the condition true is true, do everything between the curly brackets { }".

Since true is always true (no kidding? ;-), it 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 left/back by 360 degrees

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 you can 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 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!


What's next?

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

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

 

'

 

 
728x90