package myrobotpackage;
import robocode.*;
//import java.awt.Color;
/**
* MyFirstRobot - a robot by (your name here)
*/
public class MyFirstRobot extends Robot
{
/**
* run: MyFirstRobot's default behavior
*/
public void run() {
// After trying out your robot, try uncommenting the import at the top,
// and the next line:
//setColors(Color.red,Color.blue,Color.green);
while(true) {
// Replace the next 4 lines with any behavior you would like
ahead(100);
turnGunRight(360);
back(100);
turnGunLeft(360);
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
fire(1);
turnGunLeft(8);
fire(1);
turnGunRight(16);
fire(1);
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
turnLeft(90 - e.getBearing());
ahead(150);
}
}
此博客展示了Robocode中MyFirstRobot的代码。包含包声明、导入语句,定义了MyFirstRobot类并继承Robot类。实现了run方法定义默认行为,onScannedRobot方法处理发现其他机器人的情况,onHitByBullet方法处理被子弹击中的情况。
2万+

被折叠的 条评论
为什么被折叠?



