How to detect if 2 movement keys are pressed at same time?

Understanding the language, error messages, etc.

How to detect if 2 movement keys are pressed at same time?

Postby mobbarley » Sun Oct 02, 2016 8:14 pm

Hi All,
I am trying to create a tank based game (something in the lines of [url]Battle City[/url]).

.

The code is below:
Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

//declare all the variables needed for the game :
int img_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int img_y = LCDHEIGHT/2; //vertical position
int img_vx = 1; //horizontal velocity
int img_vy = 1; //vertical velocity
int img_size = 7; //the size of the image in number of pixels
uint8_t rotate = NOROT;
boolean move = true;

const byte tank1[] PROGMEM = {8,8,
B00011000,
B11011011,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11000011,
};

// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  // show the start menu
  gb.titleScreen(F("Controls"));
}

// the loop routine runs over and over again forever
void loop(){
  //updates the gamebuino (the display, the sound, the auto backlight... everything)
  //returns true when it's time to render a new frame (20 times/second)
  if(gb.update()){

    //move the image using the buttons
    if(gb.buttons.repeat(BTN_RIGHT,2)){ //every 2 frames when the right button is held down
      if (move) {
        rotate = ROTCW;
        img_x = img_x + img_vx;
      }
      //gb.sound.playTick(); //play a preset "tick" sound every time the button is pressed
    }
    if(gb.buttons.repeat(BTN_LEFT,2)){
      if (move) {
        rotate = ROTCCW;
        img_x = img_x - img_vx;
      }
      //gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_DOWN,2)){
      if (move) {
        rotate = ROT180;
        img_y = img_y + img_vy;
      //gb.sound.playTick();
    }
  }
    if(gb.buttons.repeat(BTN_UP,2)){
      if (move) {
        rotate = NOROT;
        img_y = img_y - img_vy;
      }
      //gb.sound.playTick();
    }

    if ((gb.buttons.repeat(BTN_UP,2) && gb.buttons.repeat(BTN_DOWN,2)) || (gb.buttons.repeat(BTN_UP,2) && gb.buttons.repeat(BTN_LEFT,2))
    || (gb.buttons.repeat(BTN_UP,2) && gb.buttons.repeat(BTN_RIGHT,2)) || (gb.buttons.repeat(BTN_RIGHT,2) && gb.buttons.repeat(BTN_DOWN,2))
    || (gb.buttons.repeat(BTN_RIGHT,2) && gb.buttons.repeat(BTN_LEFT,2)) || (gb.buttons.repeat(BTN_DOWN,2) && gb.buttons.repeat(BTN_LEFT,2))) {
      move = false;
    }
    else {
      move = true;
    }
    //bonus : play a preset sound when A and B are pressed
    if(gb.buttons.pressed(BTN_A)){
      gb.sound.playOK();
    }
    if(gb.buttons.pressed(BTN_B)){
      gb.sound.playCancel();
    }
    if(gb.buttons.pressed(BTN_C)){
      gb.titleScreen(F("Controls"));
    }

    //check that the image is not going out of the screen
    //if the image is touching the left side of the screen
    if(img_x < 0){
      //bring it back in the screen
      img_x = 0;
    }
    //if the image is touching the right CCWside
    if((img_x + img_size) > LCDWIDTH){
      img_x = LCDWIDTH - img_size;
    }
    //if the image is touching the top side
    if(img_y < 0){
      img_y = 0;
    }
    //if the image is touching the down side
    if((img_y + img_size) > LCDHEIGHT){
      img_y = LCDHEIGHT - img_size;
    }

    //draw the image on the screenBTN_UP
    gb.display.drawBitmap(img_x, img_y, tank1,rotate,NOFLIP);
  }
}


I have till now made a basic program where I move the bitmap across the screen. However when I press either of the 4 movement buttons at once the tank moves diagonally which I dont want. But I can't create a code which detects if two keys are pressed and/or held.

Any help would be highly appreciated.

The hex file is in the zip below:
test1.zip
(13.52 KiB) Downloaded 373 times

Regards,
mobbarley
User avatar
mobbarley
 
Posts: 4
Joined: Thu Sep 29, 2016 3:41 pm

Re: How to detect if 2 movement keys are pressed at same tim

Postby naed » Sun Oct 02, 2016 9:56 pm

this is how i made your code work

Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

//declare all the variables needed for the game :
int img_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int img_y = LCDHEIGHT/2; //vertical position
int img_vx = 1; //horizontal velocity
int img_vy = 1; //vertical velocity
int img_size = 7; //the size of the image in number of pixels
uint8_t rotate = NOROT;
int move = true;

const byte tank1[] PROGMEM = {8,8,
B00011000,
B11011011,
B11111111,
B11111111,
B11111111,
B11111111,
B11111111,
B11000011,
};

// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  // show the start menu
  gb.titleScreen(F("Controls"));
}

// the loop routine runs over and over again forever
void loop(){
  //updates the gamebuino (the display, the sound, the auto backlight... everything)
  //returns true when it's time to render a new frame (20 times/second)
  if(gb.update()){

    //move the image using the buttons
    if((gb.buttons.repeat(BTN_RIGHT,2)) && move == true){ //every 2 frames when the right button is held down
      if (move) {
        rotate = ROTCW;
        img_x = img_x + img_vx;
      }
      //gb.sound.playTick(); //play a preset "tick" sound every time the button is pressed
    }
    if(gb.buttons.repeat(BTN_LEFT,2)){
      if (move) {
        rotate = ROTCCW;
        img_x = img_x - img_vx;
      }
      //gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_DOWN,2)){
      if (move) {
        rotate = ROT180;
        img_y = img_y + img_vy;
      //gb.sound.playTick();
    }
  }
    if(gb.buttons.repeat(BTN_UP,2)){
      if (move) {
        rotate = NOROT;
        img_y = img_y - img_vy;
      }
      //gb.sound.playTick();
    }

                               ///////////////////////CHANGED VALUES HERE TO A 1/////////////////////
if ((gb.buttons.repeat(BTN_UP,1) && gb.buttons.repeat(BTN_DOWN,1)) || (gb.buttons.repeat(BTN_UP,1) && gb.buttons.repeat(BTN_LEFT,1))
    || (gb.buttons.repeat(BTN_UP,1) && gb.buttons.repeat(BTN_RIGHT,1)) || (gb.buttons.repeat(BTN_RIGHT,1) && gb.buttons.repeat(BTN_DOWN,1))
    || (gb.buttons.repeat(BTN_RIGHT,1) && gb.buttons.repeat(BTN_LEFT,1)) || (gb.buttons.repeat(BTN_DOWN,1) && gb.buttons.repeat(BTN_LEFT,1))) {
      move = false;
    }                           ///////////////////////CHANGED VALUES HERE TO A 1/////////////////////
    else {
      move = true;
    }

    //bonus : play a preset sound when A and B are pressed
    if(gb.buttons.pressed(BTN_A)){
      gb.sound.playOK();
    }
    if(gb.buttons.pressed(BTN_B)){
      gb.sound.playCancel();
    }
    if(gb.buttons.pressed(BTN_C)){
      gb.titleScreen(F("Controls"));
    }

    //check that the image is not going out of the screen
    //if the image is touching the left side of the screen
    if(img_x < 0){
      //bring it back in the screen
      img_x = 0;
    }
    //if the image is touching the right CCWside
    if((img_x + img_size) > LCDWIDTH){
      img_x = LCDWIDTH - img_size;
    }
    //if the image is touching the top side
    if(img_y < 0){
      img_y = 0;
    }
    //if the image is touching the down side
    if((img_y + img_size) > LCDHEIGHT){
      img_y = LCDHEIGHT - img_size;
    }

    //draw the image on the screenBTN_UP
    gb.display.drawBitmap(img_x, img_y, tank1,rotate,NOFLIP);
  }
}


i changed your
boolean move = true;

to an
int move = true;

then i changed your button values to a 1 as seen in the code above, this seems to work for me in your tank game
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 2 guests