Robocode攻略之第一个Robot

时间:2019-01-17
本文章向大家介绍Robocode攻略之第一个Robot,主要包括Robocode攻略之第一个Robot使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

目录

开始

Robot Editor

新的robot

让我们来移动吧

想射哪就射哪

编译你的robot

下一步?

其他


这是经典的“第一个Robot”教程,告诉你如何创建你的第一个Robot。创建一个robot很简单,但想要赢没那么简单。你可以只花几分钟也可以花上上把个月。小心上瘾!你会经历成长的烦恼,但是你将教会robot如何行动,躲闪以及作战。是否该躲在某个角落或者冲进战场?

开始

  • 准备好创建第一个robot了吗?希望你发现这很简单直接并且令人着迷!
  • Robocode自带许多样板robot,可以用Robot Editor来查看。
  • 在这部分,我们将用Robot Editor来创建真正属你的,全新的robot。

Robot Editor

第一步是打开Robot Editor

  1. 点击Robot菜单
  2. 选择Source Editor
  3. 点击File菜单
  4. 选择New Robot
  5. 输入robot的名字和robot所在包的包名

新的robot

现在看到的是如下代码

package seu184636;
import robocode.*;
//import java.awt.Color;

// API help : https://robocode.sourceforge.io/docs/robocode/robocode/Robot.html

/**
 * MyFirstRobot - a robot by (your name here)
 */
public class MyFirstRobot extends Robot
{
	/**
	 * run: MyFirstRobot's default behavior
	 */
	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);
			turnGunRight(360);
			back(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);
	}

	/**
	 * 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);
	}	
}

关于代码的一些说明:

  • import robocode.*说明你将使用Robocode提供的对象来创建robot
  • public class MyFirstRobot extends Robot说明创建的是一个Robot类型对象
  • public void run() 战斗开始时,调用run函数

让我们来移动吧

	while(true) {
			// Replace the next 4 lines with any behavior you would like
			ahead(100);
			turnGunRight(360);
			back(100);
			turnGunRight(360);
		}

想射哪就射哪

当雷达扫描到robot时,fire:

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

当你看见另一个robot时,游戏将调用onScannedRobot()函数,可以通过事件获得那个robot的信息:名字,血量,位置,前进方向,速度等。此处简单robot仅执行fire

编译你的robot

点击Save保存你的robot

点击Compile进行编译

编译通过就可以开始战斗了,点击Battel菜单里的New,添加写好的robot

下一步?

其他