Skip to content

Instantly share code, notes, and snippets.

@ilkinulas
Created December 9, 2016 21:21
Show Gist options
  • Save ilkinulas/1f2cfc30d0cfe045b4f20976e9bf636a to your computer and use it in GitHub Desktop.
Save ilkinulas/1f2cfc30d0cfe045b4f20976e9bf636a to your computer and use it in GitHub Desktop.
package peakrobots.ilkin;
import robocode.AdvancedRobot;
import java.awt.event.KeyEvent;
public class TankTrainer extends AdvancedRobot {
enum State {
STEADY, AHEAD, LEFT, RIGHT
}
private State state = State.STEADY;
private int numKeyPressed = 0;
@Override
public void run() {
while (true) {
switch (state) {
case STEADY:
setStop();
break;
case AHEAD:
setAhead(10000);
break;
case LEFT:
setTurnLeft(10000);
break;
case RIGHT:
setTurnRight(10000);
break;
}
execute();
}
}
@Override
public void onKeyPressed(KeyEvent e) {
numKeyPressed ++;
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : state = State.LEFT; break;
case KeyEvent.VK_RIGHT : state = State.RIGHT; break;
case KeyEvent.VK_UP : state = State.AHEAD; break;
case KeyEvent.VK_SPACE : setFire(1); break;
}
}
@Override
public void onKeyReleased(KeyEvent e) {
numKeyPressed --;
if (numKeyPressed == 0) {
state = State.STEADY;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment