18 April 2021

Tetris Game Controller

 


Originally I had intended to create a controller for HellTaker, but reconsidered it after feedback from the progress review. I instead decided to go with Tetris, which is a much more tame and family friendly game than HellTaker, which is about acquiring a harem of demons. I took my concepts from the first assignment in this project and applied it to Tetris, controlling the game through motion controls. I was then able to have fun with designing the new controller which I thought a Tetris block suited it best. Essentially the CPE within the Tetris block responds to the directions it is tilted in as well as light exposed to it. Tilting the block left or right would result in the on-screen block being moved accordingly. Tilting the controller towards me would rotate the on-screen block, and tilting it away from me would instantly drop the block down. I made a hole in the top of the controller that would allow light to spill in, setting off the CPE's light reading functionality. This would activate the "hold" function in Tetris and hold whatever block was on-screen for later. It can be swapped out for another block via the same function in game.

Was there any visual feedback you think I could have given the player from the CPE itself or was the feedback on the screen enough?



#include <Adafruit_CircuitPlayground.h>

#include <Adafruit_Circuit_Playground.h>

#include <math.h>

#include <Keyboard.h>


 

/*

Code by Nicholas Suarez

Controller for Tetris

*/



//init variables 

bool xDown;

bool yDown;

int xMove;

int yMove;


void setup() {


  Serial.begin(9600);

  Keyboard.begin(); 

  CircuitPlayground.begin();

}


void loop() {


  float x = CircuitPlayground.motionX(); //Gets motion in X direction

  float y = CircuitPlayground.motionY(); //Gets motion in Y direction (technically Z)


//motion outputs thresholds to integers

  xMove = map(x, -10, 10, -10, 10);

  yMove = map(y, -10, 10, -10, 10);


  Serial.print(x);

  Serial.println(y);

  

//Tilting the CPE past the number would fire the function

    switch(xMove) {

      case -9 :

        if(!xDown) { 

          Keyboard.write('A');

          xDown = true;

        }

        break;

      case 9 :

        if(!xDown) {

          Keyboard.write('D');

          xDown = true;

        }

        break;

      case 0 : xDown = false; 

        break;

    }

    

    switch(yMove) {

      case -9 :

        if(!yDown) {

          Keyboard.write('S');

          yDown = true;

        }

        break;

      case 9 :

        if(!yDown) {

          Keyboard.write('W');

          yDown = true;

        }

        break;

      case 0 : yDown = false;

        break;

    }

    

     int light = analogRead(A8); //Reads the light sensor to press Space. Activates Hold function.

  if(light > 30) {

    Keyboard.press(' '); 

                 }

  else if(light <30) {

    Keyboard.release(' ');

                 }

}









No comments:

Post a Comment

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