现在在robocode中创建第一个自己的坦克
1.选择【Robot】->【Source Editor】打开机器人编辑器。
2.选择【File】 ->【New】创建机器人,输入机器人名字,包名,便会创建一个新的机器人,系统已经给出了一段初始代码,能够进行最简单的机器人行动操作
package MyRobot;
import robocode.*;
//import java.awt.Color;此行控制机器人、武器、雷达颜色
// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
/**
* Wangkai - a robot by (your name here)
*/
public class Wangkai extends Robot
{
/**
* run: Wangkai's default behavior
* run方法,控制机器人机动
*/
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.red,Color.blue,Color.green); // body,gun,radar
// Robot main loop
while(true) {
// Replace the next 4 lines with any behavior you would like
ahead(100);//前进100像素
turnGunRight(360);//转动武器360度
back(100);//后退100像素
turnGunRight(360);
}
}
/**
* onScannedRobot: What to do when you see another robot
* 事件触发方法,当雷达扫到敌人时所进行的操作
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Replace the next line with any behavior you would like
fire(1);//发射炮弹,能量为1
}
/**
* onHitByBullet: What to do when you're hit by a bullet
* 事件触发方法,当被别人击中时的操作
*/
public void onHitByBullet(HitByBulletEvent e) {
// Replace the next line with any behavior you would like
back(10);
}
/**
* onHitWall: What to do when you hit a wall
* 撞墙时的操作
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
back(20);
}
}
3.选择【Compller】->【Compile】机器人便添加到了可选择列表中
4.选择【Battle】->【New】选择自己的机器人进入战斗。