第2章
戦車をすこし改造してみよう!
今回の内容としては
戦車の色を変える
この1つです。
改造すると大きいことを言いましたが今回やることとしてはロボットの色を変更するだけです。
零号機の色を変えて自分らしさをだしていくことにします。
まずは、Robot Editorを開きます。
すると、下のようなコードが現れると思います。ふーんってぐらいで眺めてください。
//////////////////////////////////////////////////////////////////////
package tests;
import robocode.*;
//import java.awt.Color;
// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
/**
* TestRobot - a robot by (your name here)
*/
public class TestRobot extends Robot
{
/**
* run: TestRobot'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);
}
}
//////////////////////////////////////////////////////////////////////
どこをいじればよいかわかりましたか?これでわかったのなら、このページは読み飛ばしてもらって結構です。
わからなかった方は、早速やっていきましょう。
コードの3行目付近に//import java.awt.Color;感じの文字があると思います。これは色の指定を行うことを宣言しています。しかし、行頭に//があるためにコメント文として使われてしまい実際には使えない状態です。なので行頭にある//を削除してください。
そして、21行目付近にある // setColors(Color.red,Color.blue,Color.green); // body,gun,radarの行頭にある//も削除してください。これは本体を赤に、放題を青色に、レーダを緑にする処理です。
両方共外したらコンパイルしましょう。
実際にバトルしてみると色が変わっていることが確認できると思います。
僕から以上。
0 件のコメント:
コメントを投稿