06 April 2022

Controller Update

     I wanted my controller to give the player a better sense of the gameplay while playing. Galaga is a game that requires the player to move left and right with the spaceship. I want to mimic the spaceships movement in the game with the controller the player uses. The player will have to tilt the controller left and right to move left and right in the game. The CPE will be located in the middle of the controller to be able to read the movement of the controller tilting left or right. This direction of design makes the game more immersive for the player.

#include "Adafruit_TinyUSB.h"

#include <Adafruit_CircuitPlayground.h>

#include <Adafruit_Circuit_Playground.h>


//Made by Mohammad Saadeh


int shakeThresh = 25;

// Single Report (no ID) descriptor

uint8_t const desc_hid_report[] =

{

  TUD_HID_REPORT_DESC_KEYBOARD()

};

// USB HID object. For ESP32 these values cannot be changed after this declaration

// desc report, desc len, protocol, interval, use out endpoint

Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);


const int debounce = 100;

const int thresh = 500;


void setup() {

  //Initialize CPE and Keyboard output

  CircuitPlayground.begin();

  pinMode(CPLAY_RIGHTBUTTON, INPUT_PULLDOWN); //Button B

  

   usb_hid.begin();

   delay(1000);

}


void loop() {

  // put your main code here, to run repeatedly:

  //Variables for values


  //Accelerometer axis values

  float x = CircuitPlayground.motionX();

  float y = CircuitPlayground.motionY();

  float rad = atan2(y,x);

  

  if(x < 0) {

    CircuitPlayground.setPixelColor(1,0,0,map(x, -10, 0, 255, 0));

  } else {

    CircuitPlayground.setPixelColor(8,0,0,map(x, 0, 10, 0, 255));

  }

  Serial.println(rad);

  

  float shake = abs(CircuitPlayground.motionX())

    + abs(CircuitPlayground.motionY())

    + abs(CircuitPlayground.motionZ());


  uint8_t const report_id = 0; //unsigned 8bit int

  uint8_t const modifier = 0; //used by libraries to save space

  uint8_t keycode[6] = {0};   // normal int that is 16 bit


  if(digitalRead(CPLAY_RIGHTBUTTON)){

    keycode[0] = HID_KEY_SPACE;

    usb_hid.keyboardReport(report_id, modifier, keycode);

    delay(debounce);

    usb_hid.keyboardRelease(0);

  }

  

  //Tilt Right

  if(rad > -1.05 && rad < 0.3){

    keycode[0] = HID_KEY_ARROW_RIGHT;

    usb_hid.keyboardReport(report_id, modifier, keycode);

    delay(debounce);

    usb_hid.keyboardRelease(0);

  }

  

  //Tilt Left

  if(rad < -2.05 || rad > 2.85){

    keycode[0] = HID_KEY_ARROW_LEFT;

    usb_hid.keyboardReport(report_id, modifier, keycode);

    delay(debounce);

    usb_hid.keyboardRelease(0);

  }

    

  //Shoot

  if(shake > shakeThresh) {

    CircuitPlayground.playTone(440, 200);

    keycode[0] = HID_KEY_SPACE;

    usb_hid.keyboardReport(report_id, modifier, keycode);

    delay(debounce);

    usb_hid.keyboardRelease(0);

   }

}







Is shaking the controller a fun interactive way for the player to shoot as an input? 
Does the design of the controller fit the mechanics?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.