1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import robocode.*; public class DodgeBot extends AdvancedRobot double previousEnergy = 100; int movementDirection = 1; int gunDirection = 1; public void run() { setTurnGunRight(99999); } public void onScannedRobot( ScannedRobotEvent e) { // Stay at right angles to the opponent setTurnRight(e.getBearing()+90- 30*movementDirection); // If the bot has small energy drop, // assume it fired double changeInEnergy = previousEnergy-e.getEnergy(); if (changeInEnergy>0 && changeInEnergy<=3) { // Dodge! movementDirection = -movementDirection; setAhead((e.getDistance()/4+25)movementDirection); } // When a bot is spotted, // sweep the gun and radar gunDirection = -gunDirection; setTurnGunRight(99999*gunDirection); // Fire directly at target f i r e ( 2 ) ; // Track the energy level previousEnergy = e.getEnergy(); } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |