18 April 2022

Game Controller: Pac-Man

 

Controller Description:

The controller that I made for this project was made for the classic arcade game, PAC-Man. The controllers are fairly simple for this controller. You tile the controller left to input the left arrow key, tilt it right for the right arrow key, tilt it forward for the up arrow key, and tilt it back for the down arrow key. To put it simpler, tilt in the direction that you want Pac-Man to start eating. There is also a switch in the middle of the controller. The switch is there to emulate the "Escape" button on the keyboard, which is used to pause the game. All you need to do in order to pause the game is to flip the switch twice like you are turning it on and off simultaneously. Other than that, that's really all there is to the controls. 

I don't know if this sounds cliche, stupid, or cheesy, but I made this controller so that the player has a little more impact or involvement in the game. This way it makes the player feel a little more like they are actually playing as Pac-Man. It sounds a little odd because he is only an arcade game, but I thought it just seemed better than simply pressing arrow keys or directional pads/joysticks. 

I do have a few questions about my controller:
I feel like I made it too small. It's really only the size of your palm. Should I have made it bigger?

In hindsight, I probably should've made the pause switch a bit more external and pop out of the controller. Do you agree?



Complete Code:

#include "Adafruit_TinyUSB.h"
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>

//Code by Cody Hoscheid

uint8_t const desc_hid_report[] =
{
  TUD_HID_REPORT_DESC_KEYBOARD()
};

//USB HID object. For the ESP32 these values cannot be changed after declaration
//desc report, desc lens, 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() {
  
  CircuitPlayground.begin();
  Serial.begin(9600);
  usb_hid.begin();
  
}

void loop() {
  // put your main code here, to run repeatedly:

  //Keyboard report
  uint8_t const report_id = 0;
  uint8_t const modifier = 0;
  uint8_t keycode[6] = {0};

  //rotation input
  float x = CircuitPlayground.motionX();
  float y = CircuitPlayground.motionY();
  int cpepin = 7;

    //Tilt right to "press" right arrow key
  if(x <= -5){
        keycode[0]= HID_KEY_ARROW_RIGHT;
        usb_hid.keyboardReport(report_id, modifier, keycode);
        delay(debounce);
        usb_hid.keyboardRelease(0);
  }

    //Tilt left to "press" left arrrow key
  if(x >= 5 ){
        keycode[0]= HID_KEY_ARROW_LEFT;
        usb_hid.keyboardReport(report_id, modifier, keycode);
        delay(debounce);
        usb_hid.keyboardRelease(0);
  }
  
    //Tilt forward to "press" up arrow key
  if(y <= -5 ){
        keycode[0]= HID_KEY_ARROW_UP;
        usb_hid.keyboardReport(report_id, modifier, keycode);
        delay(debounce);
        usb_hid.keyboardRelease(0);
  }
    //Tilt back to "press" down arrow key
  if(y >= 5 ){
        keycode[0]= HID_KEY_ARROW_DOWN;
        usb_hid.keyboardReport(report_id, modifier, keycode);
        delay(debounce);
        usb_hid.keyboardRelease(0);
  }
    //Flip switch to "press" the Esc. key, or pause the game
  if(digitalRead(cpepin)){
    keycode[0] = HID_KEY_ESCAPE;
        usb_hid.keyboardReport(report_id, modifier, keycode);
        delay(debounce);
        usb_hid.keyboardRelease(0);
  }

}




No comments:

Post a Comment

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