//import generic tank settings and tank load outs
import edu.dat.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
@author tpiercey
**/
//Controls,connection to the jar far file that implements the key listener//
public class KeyControlledRobot extends LightTankRobot implements KeyListener {
//ip for local machine, image url for robot//
public static void main(String[] args) {
KeyControlledRobot robo = new KeyControlledRobot ("127.0.0.1","http://i.imgur.com/TEqTK.png");
}
//Strings the IP address and image to the KeyEvent/KeyWindow
public KeyControlledRobot(String ip,String url){
super (ip,url);
//Setting up the Listener to the the KeyWindow
KeyWindow control = new KeyWindow();
control.addKeyListener(this);
}
//Key Pressed//
//Controls
// W Accelerate
// F Decelerate
// A Turn Left
// D Turn Right
// K Gear Up
// L Gear Down
// Spacebar Fire
public void keyPressed (KeyEvent e) {
int keyCode = e.getKeyCode();
//gearing controls for the tank to be able to move forward and backwards
if (keyCode == KeyEvent.VK_K){
forwardGear();
System.out.println(this.getSpeed());
}
//Lower gear to enable reverse of tank
if(e.getKeyCode()==KeyEvent.VK_L){
reverseGear();
System.out.println(this.getSpeed());
}
//allowing tank to move
if(e.getKeyCode()==KeyEvent.VK_W){
accelerate();
System.out.println("accelerate");
}
if(e.getKeyCode()==KeyEvent.VK_S){
decelerate();
System.out.println("decelerate");
}
//Controlling Direction of the Tank
//turn tank left
if(e.getKeyCode()==KeyEvent.VK_A){
turnLeft();
System.out.println("left");
}
//turn tank right
if(e.getKeyCode()==KeyEvent.VK_D){
turnRight();
System.out.println("right");
}
//Allowing the tank to fire
if(e.getKeyCode()==KeyEvent.VK_SPACE){
fire();
System.out.println("fire");
}
}
//terminate
public void keyTyped(KeyEvent e) {
}
//terminate
public void keyReleased (KeyEvent e) {
}
}
No comments:
Post a Comment