[ + | - ]

How To...

Create Multiple Key Control

Building on the use of keyPressed() and keyReleased() and keyCode from Simple Key Control, here we add checks for the up and down arrows, and for the spacebar (to fire a bullet). The tricky bit is that the bullet needs to fire while the arrow keys are also held down, and needs to keep traveling even when the spacebar is released. This code does that by using detection of the spacebar to initiate the firing of the bullet, but the release of the spacebar to stop the bullet (which is how the arrow keys work).

PROCESSING

Multiple Key Control

// Multiple Key Keyboard Control
// Up, Down, Left, Right arrow for motion
// And spacebar for firing weapon

// Variable for the size of robot 
// (required for passing edges of window)
int robotWidth = 100; 
int robotHeight = 100;
// Robot movement variables
float robotSpeed = 2.5; // Speed of movement
float newX = 200;       // Center robot at start
float newY = 200;       //
float changeX = 0;      // How much to change the X location
float changeY = 0;      // How much to change the Y location
// keyPressed Variables
int whichKeyUp;         // Variable to hold key released
int whichKeyDown;       // Variable to hold key pressed
// Gun control variables
boolean fireGun = false;// Gun not fired
float bulletPos = 1;    // Bullet starting position

void setup() {
  size(500, 500);
}

void draw() {
  background(128);
  // ----------------------------------------------------
  // Draw key control text
  fill(#000000);
  textAlign(CENTER);
  text("USE ARROW KEYS TO MOVE", width/2, 24);
  text("SPACEBAR TO FIRE", width/2, 48);
  // ----------------------------------------------------
  // Check the Keys -------------------------------------
  // keyCodes: left 37, up 38, right 39, down 40, space 32
  // LEFT 37
  if (whichKeyDown == 37) { 
    changeX = -robotSpeed;
  } 
  if (whichKeyUp == 37) {
    changeX = 0;
    whichKeyUp = 0;
    whichKeyDown = 0;
  }
  // UP 38
  if (whichKeyDown == 38) {
    changeY = -robotSpeed;
  } 
  if (whichKeyUp == 38) {
    changeY = 0;
    whichKeyUp = 0;
    whichKeyDown = 0;
  }
  // RIGHT 39
  if (whichKeyDown == 39) {
    changeX = robotSpeed;
  } 
  if (whichKeyUp == 39) {
    changeX = 0;
    whichKeyUp = 0;
    whichKeyDown = 0;
  }
  // DOWN 40
  if (whichKeyDown == 40) { 
    changeY = robotSpeed;
  } 
  if (whichKeyUp == 40) {
    changeY = 0;
    whichKeyUp = 0;
    whichKeyDown = 0;
  }
  // SPACEBAR - Doesn't need whichKeyUp check because
  // it's set in the bullet function
  if (whichKeyDown == 32) {
    fireGun = true;
  } 
  // ----------------------------------------------------
  // Robot
  // Wrap robot
  if(newX > width) {newX = -robotWidth;}
  if(newX < -robotWidth)        {newX = width;}
  if(newY > height)             {newY = -robotHeight;}
  if(newY < -robotHeight)       {newY = height;}
  newX += changeX;
  newY += changeY;
  // Move robot
  translate(newX, newY);
  // Draw robot
  drawRobot();
  // Fire gun
  if (fireGun) {
    bullet();
  }
}

// ------------------------------------------------------
// Fire Bullet Function
void bullet() {
  fill(0);
  ellipse(bulletPos + robotWidth, robotHeight/2, 10,10);
  bulletPos *= 1.2; // Accelerate bullet
  if (bulletPos > width) {
    bulletPos = 1;
    fireGun = false;
    whichKeyDown = 0;
  } 
}

// ------------------------------------------------------
// Draw Robot Function
void drawRobot() {
  ellipseMode(CORNER);
  fill(255);    
  ellipse(0, 0, robotWidth, robotHeight);
}

// ------------------------------------------------------
// Get key pressed and released events
void keyPressed() {
  whichKeyDown = keyCode;
}

void keyReleased() {
  whichKeyUp = keyCode;
}