Tuesday, 22 November 2011

working version final

//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, provides loadout setting for LightTank use
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 ("141.163.83.16","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


//When Key Pressed 'KEY CODED PRESSED' Make event happened 'CONTROL OF ROBOT'
 public void keyPressed (KeyEvent e) {
  int keyCode = e.getKeyCode();
  
 //gearing controls for the tank to be able to move forward and backwards, also print out speed of tank and show in the console below
  if (keyCode == KeyEvent.VK_K){
   forwardGear();
    System.out.println(this.getSpeed());
  
    }
  
//Lower gear to enable reverse of tank, also print out speed of tank and show in the console below   
  if(e.getKeyCode()==KeyEvent.VK_L){
   reverseGear();
    System.out.println(this.getSpeed());
       
    }
  
 //allowing tank to move forwards and backwards, print accelerate in console below  
  if(e.getKeyCode()==KeyEvent.VK_W){
   accelerate();
   System.out.println("accelerate"); 
    }
   
  //allows tank to break and decrease speed for gear change, print out decelerate below
  if(e.getKeyCode()==KeyEvent.VK_S){
   decelerate();
   System.out.println("decelerate"); 
    }
      
  
 //Controlling Direction of the Tank 
  
  //turn tank left, print left in console
  if(e.getKeyCode()==KeyEvent.VK_A){
   turnLeft();
   System.out.println("left");
    }
  //turn tank right, print right in console 
  if(e.getKeyCode()==KeyEvent.VK_D){
   turnRight();
   System.out.println("right");
    }
  
  //Allowing the tank to fire, print fire in the console
  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