Making Things Interactive

March 28, 2008

Simon Says: Macbook Tilt Edition

Filed under: 8: State Machine,Joshua Smith — jssmith44 @ 2:08 pm

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

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: