Below is the processing code for the Simon Says game I made involving tilting your macbook along different axis. Im going to build a better GUI for it, and update the code with it soon.
import sms.*;
int gameSize = 100; //max sequence number
int[] gameSeq = new int[gameSize]; // generated sequence (0,1)
int level = 0;
int threshold = 100; // threshold value for tilt sense
int currentX = 0; // current state of tilt (0, 1, -1)
int lastX = 0; // last state of tilt
int userIndex = 0; // where player is in sequence
int displayState = 0; // should display sequence?
void setup() {
size(600,600);
stroke(255);
generateSequence();
}
void draw() {
background(0);
int[] vals = Unimotion.getSMSArray();
//println(vals[0] + " " + vals[1] + " " + vals[2]);
lastX = currentX;
if (vals[0] > threshold)
currentX = 1;
else if (vals[0] < (threshold *-1))
currentX = -1;
else
currentX = 0;
if(displayState == 0)
displaySequence(); // show values up to index, then say go!
if(detectChangeEvent() && currentX != 0)
evaluate();
}
void evaluate() // determine if move matches sequence
{
if(currentX == 1 && gameSeq[userIndex] == 1){
flashCorrect();
if(userIndex < level)
userIndex++;
else
{displayState = 0;
level++;
userIndex=0;
currentX = 0;
println();
println("Round: " + (level+1));
delay(2000);}
}
else if(currentX == -1 && gameSeq[userIndex] == 0){
flashCorrect();
if(userIndex < level)
userIndex++;
else
{displayState = 0;
level++;
userIndex = 0;
currentX = 0;
println();
println("Round: " + (level+1));
delay(2000);}
}
else
reset();
}
void displaySequence() // show sequence up to userIndex
{
for(int i =0; i<level+1; i++){
background(0);
if(gameSeq[i] == 0){
//line(400, 500, 200, 100);
println("negative");
delay(1500);}
else
{ println("positive");
delay(1500);
//line(400, 100, 200, 500);
}
}
println();
println("Now, Your Turn!!");
displayState = 1;
}
boolean detectChangeEvent() //detect change in state
{
if(currentX == lastX)
return false;
else
return true;
}
void generateSequence() //genetare random sequence of size = gameSize
{
for(int i = 0; i< gameSize; i++){
gameSeq[i] = int(random(0,2));}
}
void flashCorrect(){
println("correct!");
}
void reset()
{
println("WRONG!!");
}
Notice this game has to keep track of many states: The Level, The State of the game(Simon Says Part, or the I Show You Back Part) , The state of the position the player is in sequence, and the current state of each move(is it a move to the left, right, or no move).