看过了Robocode的文章,对Robocde有了个大概了解,现在我我们就一个经典的Robocode例子源代码来分析Robocode也java之间是多么的天衣无缝。
    经典Robocode新手入门例子,包括了移动,雷达,炮管。。。,看完它并应用它,保你Robocode一日千里。
注:翻译风格有所改变,有部分没有进行翻译,有些加入了天翼.李(Skyala.Li)的心得。我们在此只注重原理,不重形式。
大家可自行看看没有翻译的部分,也正好学习外语嘛!最后引入了About Duelist,快打到世界第一的机器人发展过程的一段文字。
此物出天上,望君好收藏!源代码也可于此下载
 
package wind;
import robocode.*;
import java.awt.Color;
/**
 * SnippetBot - a robot by Alisdair Owens
 * This bot includes all sorts of useful snippets.  It is not
 * designed to be a good fighter (although it does well 1v1),
 * just to show how certain things are done
 * Bits of code lifted from Nicator and Chrisbot
 * Conventions in this bot include: Use of radians throughout
 * Storing absolute positions of enemy bots rather than relative ones
 * Very little code in events
 * These are all good programming practices for robocode
 * There may also be methods that arent used; these might just be useful for you.
 */
public class SnippetBot extends AdvancedRobot
{
    /**
     * run: SnippetBot's default behavior
     */
    Enemy target;                   //our current enemy  代表对手,包括了对手的所有有用参数
    final double PI = Math.PI;          //just a constant
    int direction = 1;              //direction we are heading...1 = forward, -1 = backwards
                                    //我们坦克车头的方向
    double firePower;                   //the power of the shot we will be using - set by do firePower() 设置我们的火力
 
    public void run()
    {
        target = new Enemy();               //实例化Enemy()类
        target.distance = 100000;           //initialise the distance so that we can select a target
        setColors(Color.red,Color.blue,Color.green);    //sets the colours of the robot
        //the next two lines mean that the turns of the robot, gun and radar are independant
        //让gun,radar独立于坦克车
        setAdjustGunForRobotTurn(true);
        setAdjustRadarForGunTurn(true);
        turnRadarRightRadians(2*PI);            //turns the radar right around to get a view of the field 以弧度计算旋转一周
       
        while(true)
        {
            doMovement();               //Move the bot 移动机器人
            doFirePower();              //select the fire power to use 选择火力
            doScanner();                //Oscillate the scanner over the bot 扫描
            doGun();                    //move the gun to predict where the enemy will be 预测敌人,调整炮管
            out.println(target.distance);      
            fire(firePower);            //所有动作完成后,开火
            execute();              //execute all commands  上面使用的都为AdvancedRobot类中的非阻塞调用
                                    //控制权在我们,所有这里用阻塞方法返回控制给机器人
        }
    }
 
    /*
      * This simple function calculates the fire power to use (from 0.1 to 3)
      * based on the distance from the target.  We will investigate the data structure
      * holding the target data later.
      */
    void doFirePower()
    {
        firePower = 400/target.distance;//selects a bullet power based on our distance away from the target
                                        //根据敌人距离来选择火力,因为本身前进,后退为300,所以火力不会过大
    }
 
    /*
      * This is the movememnt function.  It will cause us
      * to circle strafe the enemy (ie move back and forward,
      * circling the enemy.  if you don't know what strafing means
      * play more quake.
      * The direction variable is global to the class.  Passing a
      * negative number to setAhead causes the bot to go backwards
      * 以目标主中心来回摆动
      */
    void doMovement()
    {
        if (getTime()%20 == 0)  //?过20的倍数时间就反转方向
        {
            //every twenty 'ticks'
            direction *= -1;        //reverse direction
            setAhead(direction*300);    //move in that direction
        }
        setTurnRightRadians(target.
1 2 3 下一页

评论】【加入收藏夹】【打印】【关闭

  评论人 评论内容 发表时间
发表评论
用户名:   匿名发表