Category Archives: Final Project

Cursor Craze

Here is my final project in all of its glory: Cursor Craze.

Can you get to the other side??? There’s only one way to find out!!!

I’m not exactly sure how to post a downloadable link to the full folder- but i’ll figure it out. For now, all of the images I used are in the “Media” library in WordPress.

Screen Shot 2014-05-05 at 1.42.20 PM

 

Here’s the code:

PImage  starthere;
PImage finishhere;
PImage title;
PImage rules;
PImage youlose;
PImage youlose27;
PImage youwin;
PImage pressq;

PImage almostthere;
PImage youcandoit;
PImage greatstart;
PImage keepitup;
PImage woo;

PImage greatstartcover;
PImage youcandoitcover;
PImage keepitupcover;
PImage woocover;
PImage almosttherecover;

int gap = 20;

void setup() {
  size (800, 400);
  background (204, 204, 204);
  starthere = loadImage("starhere.png");
  finishhere = loadImage("finishhere.png");
  title = loadImage("cursorcraze.png");
  rules = loadImage("mazerules2.0.png");
  youlose = loadImage("youlose2.png");
  youlose27 = loadImage("youlose27.png");
  youwin = loadImage("youwin.png");
  pressq = loadImage("pressQ.png");
  almostthere = loadImage("almostthere.png");
  youcandoit = loadImage("youcandoit.png");
  greatstart = loadImage("greatstart.png");
  keepitup = loadImage("keepitup.png");
  woo = loadImage("woo.png");
  greatstartcover = loadImage("greatstartcover.png");
  youcandoitcover= loadImage("youcandoitcover.png");
  keepitupcover = loadImage("keepitupcover.png");
  woocover = loadImage("woocover.png");
  almosttherecover = loadImage("almosttherecover.png");

  stroke (0);
  fill (0);
  //top and bottom
  //1
  rect (0, 0, gap, 160); 
  rect (0, 200, gap, 200); 
  //2
  rect (20, 0, gap, 160); 
  rect (20, 200, gap, 220); 
  //3
  rect (40, 0, gap, 140); 
  rect (40, 180, gap, 240); 
  //4
  rect (60, 0, gap, 120); 
  rect (60, 160, gap, 260); 
  //5
  rect (80, 0, gap, 100); 
  rect (80, 140, gap, 280); 
  //6
  rect (100, 0, gap, 100); 
  rect (100, 140, gap, 280); 
  //7
  rect (120, 0, gap, 120); 
  rect (120, 160, gap, 260); 
  //8
  rect (140, 0, gap, 140); 
  rect (140, 180, gap, 280); 
  //9
  rect (160, 0, gap, 160); 
  rect (160, 200, gap, 200); 
  //10
  rect (180, 0, gap, 180); 
  rect (180, 220, gap, 200); 
  //11
  rect (200, 0, gap, 200); 
  rect (200, 240, gap, 200); 
  //12
  rect (220, 0, gap, 220); 
  rect (220, 260, gap, 200);
  //13
  rect (240, 0, gap, 240); 
  rect (240, 280, gap, 200); 
  //14
  rect (260, 0, gap, 260); 
  rect (260, 300, gap, 200);
  //15
  rect (280, 0, gap, 280); 
  rect (280, 320, gap, 200); 
  //16
  rect (300, 0, gap, 300);
  rect (300, 340, gap, 200); 
  //17
  rect (320, 0, gap, 320); 
  rect (320, 360, gap, 200);
  //18
  rect (340, 0, gap, 320);
  rect (340, 360, gap, 200); 
  //19
  rect (360, 0, gap, 300);
  rect (360, 340, gap, 200);
  //20
  rect (380, 0, gap, 280);
  rect (380, 320, gap, 200); 
  //21
  rect (400, 0, gap, 260);
  rect (400, 300, gap, 200); 
  //22 
  rect (420, 0, gap, 240); 
  rect (420, 280, gap, 200);
  //23
  rect (440, 0, gap, 220); 
  rect (440, 260, gap, 200); 
  //24
  rect (460, 0, gap, 220); 
  rect (460, 260, gap, 200);
  //25
  rect (480, 0, gap, 240);
  rect (480, 280, gap, 200); 
  //26
  rect (500, 0, gap, 260);
  rect (500, 300, gap, 200); 
  //27
  rect (520, 0, gap, 280);
  rect (520, 320, gap, 200); 
  //28
  rect (540, 0, gap, 280);
  rect (540, 320, gap, 200);  
  //29
  rect (560, 0, gap, 260);
  rect (560, 300, gap, 200); 
  //30 
  rect (580, 0, gap, 240); 
  rect (580, 280, gap, 200); 
  //31
  rect (600, 0, gap, 220); 
  rect (600, 260, gap, 200); 
  //32
  rect (620, 0, gap, 200);
  rect (620, 240, gap, 220); 
  //33
  rect (640, 0, gap, 180);
  rect (640, 220, gap, 240); 
  //34
  rect (660, 0, gap, 160); 
  rect (660, 200, gap, 260);
  //35
  rect (680, 0, gap, 140);
  rect (680, 180, gap, 280); 
  //36
  rect (700, 0, gap, 120);
  rect (700, 160, gap, 300); 
  //37
  rect (720, 0, gap, 120);
  rect (720, 160, gap, 320);  
  //38
  rect (740, 0, gap, 140); 
  rect (740, 180, gap, 300);
  //39
  rect (760, 0, gap, 160);
  rect (760, 200, gap, 320); 
  //40
  rect (780, 0, gap, 160); 
  rect (780, 200, gap, 320); 

  image(starthere, 0, 200);
  image(finishhere, 700, 200);
  image(title, 210, 0);
  image(rules, 220, 70);
  image(pressq, 680, 5);
}

void draw() {

  if ((mouseX > 0 && mouseX < 20) && (mouseY < 160  || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 20 && mouseX < 40 ) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 40 && mouseX < 60) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 60 && mouseX < 80) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 80 && mouseX < 100) && (mouseY < 100 || mouseY > 140)) {
    background (youlose);
  }

  else if ((mouseX > 80 && mouseX < 100) && (mouseY > 100 || mouseY < 140)) { 
    image(greatstart, 50, 75);
  }

  else if ((mouseX > 100 && mouseX < 120) && (mouseY < 100 || mouseY > 140)) {
    background (youlose);
  }

  else if ((mouseX > 120 && mouseX < 140) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 120 && mouseX < 140) && (mouseY > 120 || mouseY < 160)) {
    image (greatstartcover, 50, 75);
  }

  else if ((mouseX > 140 && mouseX < 160) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 160 && mouseX < 180) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }
  //10
  else if ((mouseX > 180 && mouseX < 200) && (mouseY < 180 || mouseY > 220)) {
    background (youlose);
  }

  else if ((mouseX > 200 && mouseX < 220) && (mouseY < 200 || mouseY > 240)) {
    background (youlose);
  }

  else if ((mouseX > 220 && mouseX < 240) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 240 && mouseX < 260) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 260 && mouseX < 280) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }

  else if ((mouseX > 280 && mouseX < 300) && (mouseY < 280 || mouseY > 320)) {
    background (youlose);
  }

  else if ((mouseX > 300 && mouseX < 320) && (mouseY < 300 || mouseY > 340)) {
    background (youlose);
  }

  else if ((mouseX > 320 && mouseX < 340) && (mouseY < 320 || mouseY > 360)) {
    background (youlose);
  }

  else if ((mouseX > 320 && mouseX < 340) && (mouseY > 320 || mouseY < 360)) {
    image(youcandoit, 310, 360) ;
  }

  else if ((mouseX > 340 && mouseX < 360) && (mouseY < 320 || mouseY > 360)) {
    background (youlose);
  }

  else if ((mouseX > 360 && mouseX < 380) && (mouseY < 300 || mouseY > 340)) {
    background (youlose);
  }

  else if ((mouseX > 360 && mouseX < 380) && (mouseY > 300 || mouseY < 340)) {
    image (youcandoitcover, 310, 360);
  }

  //20
  else if ((mouseX > 380 && mouseX < 400) && (mouseY < 280 || mouseY > 320)) {
    background (youlose);
  }

  else if ((mouseX > 400 && mouseX < 420) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }

  else if ((mouseX > 420 && mouseX < 440) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 440 && mouseX < 460) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 440 && mouseX < 460) && (mouseY > 220 || mouseY < 260)) {
    image (keepitup, 430, 180);
  }

  else if ((mouseX > 460 && mouseX < 480) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 480 && mouseX < 500) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 480 && mouseX < 500) && (mouseY > 240 || mouseY < 280)) {
    image (keepitupcover, 430, 180);
  }

  else if ((mouseX > 500 && mouseX < 520) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }
  //27
  else if ((mouseX > 520 && mouseX < 540) && (mouseY < 280 || mouseY > 320)) {
    background (youlose27);
  }

  else if ((mouseX > 520 && mouseX < 540) && (mouseY > 280 || mouseY < 320)) {
    image (woo, 520, 320);
  }

  else if ((mouseX > 540 && mouseX < 560) && (mouseY < 280 || mouseY > 320)) {
    background (youlose);
  }

  else if ((mouseX > 560 && mouseX < 580) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }

  else if ((mouseX > 560 && mouseX < 580) && (mouseY > 260 || mouseY < 300)) {
    image (woocover, 520, 320);
  }

  //30
  else if ((mouseX > 580 && mouseX < 600) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 600 && mouseX < 620) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 640 && mouseX < 660) && (mouseY < 180 || mouseY > 220)) {
    background (youlose);
  }

  else if ((mouseX > 660 && mouseX < 680) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 680 && mouseX < 700) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 700 && mouseX < 720) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 700 && mouseX < 720) && (mouseY > 120 || mouseY < 160)) {
    image(almostthere, 690, 85);
  }

  else if ((mouseX > 720 && mouseX < 740) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 740 && mouseX < 760) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 740 && mouseX < 760) && (mouseY > 140 || mouseY < 180)) {
    image (almosttherecover, 690, 85);
  }

  //final 2 boxes
  else if ((mouseX > 760 && mouseX < 780) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }
  //40
  else if ((mouseX > 780 && mouseX < 800) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 760 && mouseX < 780) && (mouseY > 160 || mouseY < 200)) {
    background (youwin);
  }

  else if ((mouseX > 780 && mouseX < 800) && (mouseY > 160 || mouseY < 200)) {
    background (youwin);
  }

  if (keyPressed) {
    if (key == 'q') {
      background (204, 204, 204);
      stroke (0);
      fill (0);
      //top and bottom
      //1
      rect (0, 0, gap, 160); 
      rect (0, 200, gap, 200); 
      //2
      rect (20, 0, gap, 160); 
      rect (20, 200, gap, 220); 
      //3
      rect (40, 0, gap, 140); 
      rect (40, 180, gap, 240); 
      //4
      rect (60, 0, gap, 120); 
      rect (60, 160, gap, 260); 
      //5
      rect (80, 0, gap, 100); 
      rect (80, 140, gap, 280); 
      //6
      rect (100, 0, gap, 100); 
      rect (100, 140, gap, 280); 
      //7
      rect (120, 0, gap, 120); 
      rect (120, 160, gap, 260); 
      //8
      rect (140, 0, gap, 140); 
      rect (140, 180, gap, 280); 
      //9
      rect (160, 0, gap, 160); 
      rect (160, 200, gap, 200); 
      //10
      rect (180, 0, gap, 180); 
      rect (180, 220, gap, 200); 
      //11
      rect (200, 0, gap, 200); 
      rect (200, 240, gap, 200); 
      //12
      rect (220, 0, gap, 220); 
      rect (220, 260, gap, 200);
      //13
      rect (240, 0, gap, 240); 
      rect (240, 280, gap, 200); 
      //14
      rect (260, 0, gap, 260); 
      rect (260, 300, gap, 200);
      //15
      rect (280, 0, gap, 280); 
      rect (280, 320, gap, 200); 
      //16
      rect (300, 0, gap, 300);
      rect (300, 340, gap, 200); 
      //17
      rect (320, 0, gap, 320); 
      rect (320, 360, gap, 200);
      //18
      rect (340, 0, gap, 320);
      rect (340, 360, gap, 200); 
      //19
      rect (360, 0, gap, 300);
      rect (360, 340, gap, 200);
      //20
      rect (380, 0, gap, 280);
      rect (380, 320, gap, 200); 
      //21
      rect (400, 0, gap, 260);
      rect (400, 300, gap, 200); 
      //22 
      rect (420, 0, gap, 240); 
      rect (420, 280, gap, 200);
      //23
      rect (440, 0, gap, 220); 
      rect (440, 260, gap, 200); 
      //24
      rect (460, 0, gap, 220); 
      rect (460, 260, gap, 200);
      //25
      rect (480, 0, gap, 240);
      rect (480, 280, gap, 200); 
      //26
      rect (500, 0, gap, 260);
      rect (500, 300, gap, 200); 
      //27
      rect (520, 0, gap, 280);
      rect (520, 320, gap, 200); 
      //28
      rect (540, 0, gap, 280);
      rect (540, 320, gap, 200);  
      //29
      rect (560, 0, gap, 260);
      rect (560, 300, gap, 200); 
      //30 
      rect (580, 0, gap, 240); 
      rect (580, 280, gap, 200); 
      //31
      rect (600, 0, gap, 220); 
      rect (600, 260, gap, 200); 
      //32
      rect (620, 0, gap, 200);
      rect (620, 240, gap, 220); 
      //33
      rect (640, 0, gap, 180);
      rect (640, 220, gap, 240); 
      //34
      rect (660, 0, gap, 160); 
      rect (660, 200, gap, 260);
      //35
      rect (680, 0, gap, 140);
      rect (680, 180, gap, 280); 
      //36
      rect (700, 0, gap, 120);
      rect (700, 160, gap, 300); 
      //37
      rect (720, 0, gap, 120);
      rect (720, 160, gap, 320);  
      //38
      rect (740, 0, gap, 140); 
      rect (740, 180, gap, 300);
      //39
      rect (760, 0, gap, 160);
      rect (760, 200, gap, 320); 
      //40
      rect (780, 0, gap, 160); 
      rect (780, 200, gap, 320); 

      image(starthere, 0, 200);
      image(finishhere, 700, 200);
      image(title, 210, 0);
      image(rules, 220, 70);
      image(pressq, 680, 5);
    }
  }
}

Food for thought:

– would be great to add a timer

– would be even greater to have the map move

– Different levels would be awesome

 

 

Pusheen – Too Busy Eating Edition

Pusheen - 3

Pusheen has to save the princess!

But oh no, why is all this food here? As one of our favorite internet cats, pusheen’s pretty notorious for eating everything…and avoiding adventures.

This is a tribute to not only Pusheen but also: a much-loved unicorn game, the classic Mario “The princess is in another castle!” level endings, and of course, food.

Things to update:

– Copy on the level/title screens so it’s clear what the objective of the game is. Add a video before the game, maybe?

– Level 4 is supposed to be a boss battle, with the flame in the middle and the skulls floating around. Since “eating” the skulls shouldn’t really be an option, I would add a collision function, so that every time Pusheen collides with the skulls, you have to start over from level 1.

– A timer class exists, but isn’t used: add that in levels 1-3 where the player has to eat everything in under x amount of time.

– Like Nicole and Dan mentioned, there is a great chance to incorporate inheritance for the food items.

Notes:

Although the game isn’t quite finished, and I solved the problem with the levels not appearing whilst in class, I’m happy the game (which would be the first I’ve made) works. I’m very glad I got to use and understand arraylists for the food objects eaten in the levels, and states for the actual levels. Many, many thanks to Pauline for helping me understand arraylists and for screaming with me when we got the boxes to disappear for the first time. (And also to everyone who pours their heart and code into processing tutorials.)

Download the game & all of the related files.

Code below:

FOR THE GAME:

//Pusheen objects:
Pusheen pusheen;

//Timer:
Timer timer;

//Box Arrays:
ArrayList<Box> box_list; //level 1
ArrayList<Box> box_list2; //level 2
ArrayList<Box> box_list3; //level 3
ArrayList<Box> box_list4; //level 4

//importing minim and serial libraries
import ddf.minim.*;
import processing.serial.*;

//for to play music
Minim minim; 
AudioPlayer player; 

//Background images:
PImage bg1;
PImage bg2;
PImage bg3;
PImage bg4;
PImage bg5;
PImage bg6;
//PImage bg7;
int y;

//Play button:
int playButtonWidth, playButtonHeight;
int playButtonX, playButtonY;

//Levels: 
final int START = 1;
final int LEVEL1 = 2;
final int LEVEL2 = 3;
final int LEVEL3 = 4; 
final int LEVEL4 = 5;
final int WIN = 6;
final int GAME_OVER = 7; 
int state = START; //initial value 

void setup () {
  size(880, 680);
  //load background images:
  bg1 = loadImage("level1bg.png");
  bg2 = loadImage("level2bg.png");
  bg3 = loadImage("level3bg.png");
  bg4 = loadImage("level4bg.png");
  bg5 = loadImage("castlebg.png");
  bg6 = loadImage("startbg.png");
  // bg7 = loadImage("bg7.png");

  minim = new Minim (this);
  player = minim.loadFile ("Always.mp3");
  //Because this is obviously THAT unicorn song.
  //player = minim.loadFile ("Outro.mp3");
  //This is a different song
  player.play ( );

  //array for box, change number of i for levels 
  //from box constructor

  //array for level 1 - img = muffin
  box_list = new ArrayList<Box>();
  for (int i=0; i<31; i++) {
    box_list.add (new Box (random (0, width), random (0, height), random(1, 4), random(1, 4), "muffin.png"));
  }

  //array for level 2 - img = hamburger
  box_list2 = new ArrayList<Box>();
  for (int i=0; i<41; i++) {
    box_list2.add (new Box (random (0, width), random (0, height), random(1, 4), random(1, 4), "hamburger.png"));
  }

  //array for level 3 - img = donut/candy/hotodg ??
  box_list3 = new ArrayList<Box>();
  for (int i=0; i<51; i++) {
    box_list3.add (new Box (random (0, width), random (0, height), random(1, 4), random(1, 4), "donut.png"));
  }

  //array for level 4 - img = skulls (boss-ish level)
  box_list4 = new ArrayList<Box>();
  for (int i=0; i<10; i++) {
    box_list4.add (new Box (random (0, width), random (0, height), random(1, 4), random(1, 4), "skull.png"));
  }

  //things from Pusheen constructor
  pusheen = new Pusheen(400, 200, "unipic.png");

  //play button:
  playButtonWidth = width / 5;
  playButtonHeight = height / 8;
  playButtonX = width / 2;
  playButtonY = 4 * height / 5;
}

void draw () {
  //background(255);
  noCursor();

  //Magic pusheen adventure time! 
  pusheen.unicorn_Draw();
  pusheen.unicorn_Move();

  switch (state)
  {
  case START: 
    showWelcomeScreen();
    pusheen.unicorn_Draw();
    pusheen.unicorn_Move();
    break;
  case LEVEL1:
    background(bg1);
    pusheen.unicorn_Draw();
    pusheen.unicorn_Move();
    for (int i=0; i<box_list.size()-1; i++) {
      box_list.get(i).drawBox();
      box_list.get(i).moveBox();

      if (box_list.get(i).checkBoundaries()); 
      {
        box_list.get(i).bounce_box();
        box_list.get(i).drawBox();
        box_list.get(i).moveBox();
      }

      if (pusheen.check_Collision(box_list.get(i)))
      {
        box_list.remove(i);
      }

      /* else if (box_list.size()==1);
       {
       //println("WHAT HAVE YOU DONE WITH THE COOKIES");
       }
       */
    }//end of FOR (in Level1)
    break;

  case LEVEL2:
    background(bg2);
    pusheen.unicorn_Draw();
    pusheen.unicorn_Move();
    for (int i=0; i<box_list2.size()-1; i++) {
      box_list2.get(i).drawBox();
      box_list2.get(i).moveBox();

      if (box_list2.get(i).checkBoundaries()); 
      {
        box_list2.get(i).bounce_box();
        box_list2.get(i).drawBox();
        box_list2.get(i).moveBox();
      }

      if (pusheen.check_Collision(box_list2.get(i)))
      {
        box_list2.remove(i);
      }
    }//end of FOR (in Level2)
    break;

  case LEVEL3:
    background(bg3);
    pusheen.unicorn_Draw();
    pusheen.unicorn_Move();
    for (int i=0; i<box_list3.size()-1; i++) {
      box_list3.get(i).drawBox();
      box_list3.get(i).moveBox();

      if (box_list3.get(i).checkBoundaries()); 
      {
        box_list3.get(i).bounce_box();
        box_list3.get(i).drawBox();
        box_list3.get(i).moveBox();
      }

      if (pusheen.check_Collision(box_list3.get(i)))
      {
        box_list3.remove(i);
      }
    }//end of FOR (in Level3)
    break;

  case LEVEL4:
    background(bg4);
    pusheen.unicorn_Draw();
    pusheen.unicorn_Move();
    for (int i=0; i<box_list4.size()-1; i++) {
      box_list4.get(i).drawBox();
      box_list4.get(i).moveBox();

      if (box_list4.get(i).checkBoundaries()); 
      {
        box_list4.get(i).bounce_box();
        box_list4.get(i).drawBox();
        box_list4.get(i).moveBox();
      }

      if (pusheen.check_Collision(box_list4.get(i)))
      {
        box_list4.remove(i);
      }
    }//end of FOR (in Level4)
    break;

  case GAME_OVER:
    showGameOverScreen();
    break;
  }//ends state

  //CHANGING LEVELS!!!!: 
  if (state == LEVEL1);
  {
    if (box_list.size() == 1) {
      println("Box array empty, going to level 2");
      println(box_list.size());
      state = LEVEL2;
      //return;
    }
  }

  if (state == LEVEL2);
  {
    if (box_list2.size() == 1) {
      println("MEOW");
      println(box_list2.size());
      state = LEVEL3;
      //return;
    }
  }

  if (state == LEVEL3);
  {
    if (box_list3.size() == 1) {
      println("level 3");
      println(box_list3.size());
      state = LEVEL4;
      // return;
    }
  }

  if (state == LEVEL4);
  {
    if (box_list4.size() == 1) {
      println("level 4");
      println(box_list4.size());
      state = GAME_OVER;
      return;
    }
  }

} //END OF DRAW  

void showGameOverScreen()
{
  background(bg5);
  pusheen.unicorn_Draw();
  pusheen.unicorn_Move();

  String s = "Look's like there's a castle...but you've been too busy eating all the food! The princess is gone...";
  fill(50);
  textSize(20);
  fill(#6C5B7B);
  text(s, width/2, 500, 500, 500);
}

void showWelcomeScreen()
{
  background(bg6);

  showTitle();

  // Play button
  fill(#79BD9A);
  stroke(#3B8686);
  rectMode(CENTER);
  rect(playButtonX, playButtonY, playButtonWidth, playButtonHeight);
  fill(#A8DBA8);
  textSize(32);
  text("PLAY", width /2, 4 * height/5);

  // Instructions
  textSize(24);
  fill(#0B486B);
  text("Will he ever get there???", width/2, height/2 + 50);
}

void showTitle()
{
  // Title
  textAlign(CENTER, CENTER);
  textSize(48);
  fill(#6C5B7B);
  text("Pusheen Saves the Princess!", width/2, height/2);
}

//Boolean & mousePressed to check for player interaction with play button. Starts game. 
boolean isMouseOverPlayButton()
{
  // Half-wdith
  int hw = playButtonWidth / 2;
  // Half-height
  int hh = playButtonHeight / 2;
  // Returns true if mouse position is within the bounds of the button
  return mouseX >= playButtonX - hw && mouseX <= playButtonX + hw &&
    mouseY >= playButtonY - hh && mouseY <= playButtonY + hh;
}

void mousePressed()
{
  // Check if we are at the stat screen, and if the click is within the button bounds
  if (state == START && isMouseOverPlayButton())
  {
    // Now, we can start!
    state = LEVEL1;
    return;
  }
}

BOX CLASS: (What I called the food items)

class Box {

  //box position:
  float x;
  float y;

  int w = 20;
  int h = 20;

  //CHANGING image of items (item, skull, etc)
  // Each item is 40x40 px
  PImage item;

  //Getter functions
  float get_x() {
    return x;
  } //getter function for x position 
  float get_y() {
    return y;
  } //get y position
  int get_w() {
    return w;
  } //get width (of box)
  int get_h() {
    return h;
  } //get height

  //box velocity/speed:
  float x_velocity;
  float y_velocity;  

  //Constructor: 
  Box(float _x, float _y, float _x_velocity, float _y_velocity, String filename) {
    x = _x;
    y = _y; 
    x_velocity = _x_velocity;
    y_velocity = _y_velocity;
    item = loadImage(filename);
  }

  void drawBox() {
    // rect(x, y, w, h);
    image(item, x, y);
  }

  void moveBox() {
    x += x_velocity; 
    y += y_velocity;

    //  this.drawBox();
  }

  //checks to see if boxes go off the screen
  boolean checkBoundaries() {
    boolean out_of_bounds = false;
    //Out of screen (horizontal)
    if (x>(width+(1.5* w))|| x < (0 - (1.5*w))) {
      out_of_bounds = true;
    }

    //Out of screen (vertical)
    if (y>(height+(1.5*h)) || y < (0 - (1.5*h))) {
      out_of_bounds = true;
    }
    return out_of_bounds;
  }

  void bounce_box() {
    //makes box array bounce

    //left side of screen
    if (x<=0) {
      x_velocity*=-1;
    }

    //right side of screen
    if (x+50>=width) {
      x_velocity*=-1;
    }

    //top side of screen
    if (y<=0) {
      y_velocity*=-1;
    }

    //right side of screen
    if (y+50>=height) {
      y_velocity*=-1;
    }
  }
  //End of Box Class
}

PUSHEEN CLASS:

class Pusheen {

  //Pusheen image:
  int x; 
  int y;

  int w = 162; //width
  int h = 148; //height

  PImage unicorn;

  //Constructor:
  Pusheen(int _x, int _y, String filename) {
    x = _x; 
    y = _y; 
    unicorn = loadImage(filename);
  }

  void unicorn_Draw() {
    image(unicorn, x, y);
  }

  void unicorn_Move() {
    x = mouseX-(w/2);
    y = mouseY-(h/2);
  }

  //collision function goes here, 
  boolean check_Collision(Box temporary) {
    boolean signal = false;
    float temp_x = temporary.get_x();
    float temp_y = temporary.get_y();
    int temp_box_height = temporary.get_h();
    int temp_box_width = temporary.get_w();

    if (x < (temp_x + temp_box_width) && (x+w) > (temp_x ))    
      if (y < (temp_y + temp_box_height) && (y+h) > (temp_y )) {
        signal = true;
      }
      else {
        signal = false;
      }

    return signal;
  }  

  //end of pusheen class
}

 

 

Final Project: Caramelldansen SUper FUn

import processing.video.*;
import ddf.minim.*;

Capture video;
PImage prevFrame;

Movie instructionsScreen;
Movie player1_screen;
Movie player2_screen;
Movie scoreBar1;
Movie scoreBar2;
Movie player1_wins;
Movie player2_wins;
Movie stop1;
Movie stop2;

Minim minim;
AudioPlayer player;

float threshold = 50;
int counter = 0;
int counter2 = 0;
boolean round1 = false;
boolean round2 = false;

float ypos1, ypos2; //location of score bar

Timer theStartTimer1;
Timer theGameTimer1;
Timer theStartTimer2;
Timer theGameTimer2;
boolean gameStart1 = false;
boolean gameStart2 = false;

void setup() {
  size(1280, 720);
  smooth();
  noStroke();
  video = new Capture(this, width, height);
  video.start();
  prevFrame = createImage(video.width, video.height, RGB);

  instructionsScreen = new Movie(this, "instructions.mov");
  player1_screen = new Movie(this, "player1_screen.mov");
  player2_screen = new Movie(this, "player2_screen.mov");
  scoreBar1 = new Movie(this, "score_bar.mov");
  scoreBar2 = new Movie(this, "score_bar.mov");
  player1_wins = new Movie(this, "player1_wins.mov");
  player2_wins = new Movie(this, "player2_wins.mov");
  stop1 = new Movie(this, "stop1.mov");
  stop2 = new Movie(this, "stop2.mov");

  minim = new Minim(this);
  player = minim.loadFile("Caramelldansen.wav");

  theStartTimer1 = new Timer(6000); 
  theGameTimer1 = new Timer (26000); //actual gameplay is 20sec
  theStartTimer2 = new Timer(6000);
  theGameTimer2 = new Timer (26000);
}

void draw() {
  image(instructionsScreen, 0, 0);
  instructionsScreen.play();

  if (gameStart1 == true) {
    instructionsScreen.stop();
    player.play();

    //round 1
    if (theStartTimer1.timeOver()) {
      if (theGameTimer1.timeOver()) {
        image(stop1, 0, 0);
        stop1.play();
        round1 = true;
      }
      else {
        image(player1_screen, 0, 0);
        player1_screen.play();
        theGameTimer1.countDown();

        textSize(height/10);
        fill(255, 0, 255);
        text (counter, width/30, height/10);
        textSize(height/10);
        fill(0, 255, 255);
        text (counter2, width-width/7.5, height/10);

        // Capture video
        if (video.available()) {
          // Save previous frame for motion detection
          prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
          prevFrame.updatePixels();
          video.read();
        }

        loadPixels();
        video.loadPixels();
        prevFrame.loadPixels();

        float totalMotion = 0;

        // Sum the brightness of each pixel
        for (int i = 0; i < video.pixels.length; i ++ ) {
          // Step 2, what is the current color
          color current = video.pixels[i];

          // Step 3, what is the previous color
          color previous = prevFrame.pixels[i];

          // Step 4, compare colors (previous vs. current)
          float r1 = red(current);
          float g1 = green(current);
          float b1 = blue(current);
          float r2 = red(previous);
          float g2 = green(previous);
          float b2 = blue(previous);

          // Motion for an individual pixel is the difference between the previous color and current color.
          float diff = dist(r1, g1, b1, r2, g2, b2);
          // totalMotion is the sum of all color differences.
          totalMotion += diff;
        }

        // averageMotion is total motion divided by the number of pixels analyzed.
        float avgMotion = totalMotion / video.pixels.length;

        // average motion
        smooth();
        noStroke();
        fill(0);
        counter += avgMotion/20;

        float ypos = map (counter, 0, 1200, 0, height);
        image(scoreBar1, width/32, height-ypos);
        ypos1 = height-ypos;
        scoreBar1.loop();
      }
    } 
    else
    {
      image(player1_screen, 0, 0);
      player1_screen.play();
    }
  }

  //round 2
  if (gameStart2 == true) {
    player1_screen.stop();

    if (theStartTimer2.timeOver()) {
      if (theGameTimer2.timeOver()) {
        image(stop2, 0, 0);
        stop2.play();
      }
      else {
        image(player2_screen, 0, 0);
        player2_screen.play();
        theGameTimer2.countDown();
        textSize(height/10);
        fill(255, 0, 255);
        text (counter, width/30, height/10);
        textSize(height/10);
        fill(0, 255, 255);
        text (counter2, width-width/7.5, height/10);
        image(scoreBar1, width/32, ypos1);

        // Capture video
        if (video.available()) {
          // Save previous frame for motion detection
          prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
          prevFrame.updatePixels();
          video.read();
        }

        loadPixels();
        video.loadPixels();
        prevFrame.loadPixels();

        float totalMotion = 0;

        // Sum the brightness of each pixel
        for (int i = 0; i < video.pixels.length; i ++ ) {
          // Step 2, what is the current color
          color current = video.pixels[i];

          // Step 3, what is the previous color
          color previous = prevFrame.pixels[i];

          // Step 4, compare colors (previous vs. current)
          float r1 = red(current);
          float g1 = green(current);
          float b1 = blue(current);
          float r2 = red(previous);
          float g2 = green(previous);
          float b2 = blue(previous);

          // Motion for an individual pixel is the difference between the previous color and current color.
          float diff = dist(r1, g1, b1, r2, g2, b2);
          // totalMotion is the sum of all color differences.
          totalMotion += diff;
        }

        // averageMotion is total motion divided by the number of pixels analyzed.
        float avgMotion = totalMotion / video.pixels.length;

        // average motion
        smooth();
        noStroke();
        fill(0);
        counter2 += avgMotion/20;

        float ypos2A = map (counter2, 0, 1200, 0, height);
        image(scoreBar2, width- width/7, height-ypos2A);
        ypos2 = height-ypos2A;
        scoreBar2.loop();
      }
    } 
    else
    {
      image(player2_screen, 0, 0);
      player2_screen.play();
    }
  }

  if (round1 == true && round2 == true) {
    //instructionsScreen.stop();
    player1_screen.stop();
    player2_screen.stop();
    //image(scoreBar1, width/32, ypos1);
    image(scoreBar2, width - width/7, ypos2);

    //if player2 wins
    if (counter2 > counter) {
      image(player2_wins, 0, 0);
      player2_wins.play();
    }
    //if player1 wins
    if (counter > counter2) {
      image(player1_wins, 0, 0);
      player1_wins.play();
    }
  }
}

void keyPressed() {
  if (key == '2') {
    gameStart2 = true;
    theStartTimer2.start();
    theGameTimer2.start();
  }
  else {
    gameStart2 = false;
  }

  if (key == '1') {
    gameStart1 =  true;
    theStartTimer1.start();
    theGameTimer1.start();
  } 

  if (key == 'w') {
    round2 = true;
  }

  if (key == 'q') {
    gameStart1 = false;
    gameStart2 = false;
    round1 = false;
    round2 = false;
    counter2 = 0;
    counter = 0;
    ypos1 = height;
    ypos2 = height;
    scoreBar1.stop();
    scoreBar2.stop();
  }
}

void movieEvent(Movie m) {
  m.read();
}

Timer Class:

//New Class

PFont font;

class Timer {
  int savedTime;
  int totalTime;
  int countDown;
  int passedTime;
  int screenSeconds;

  Timer(int temp_totalTime) {
    totalTime = temp_totalTime;

  }

  void start() {
    savedTime = millis();
  }

  void countDown(){
    screenSeconds = passedTime / 1000;
     countDown = 26 - screenSeconds;
    font = loadFont("KarmaSuture.vlw");
    fill(50, 255, 0);
    textFont(font, 100);
    text(countDown, width/2.2, height/7); 
  }

  boolean timeOver() {
  passedTime = millis() - savedTime;
    if (passedTime > totalTime) {
      return true;
    }    
    else {
      return false;
    }
  }
}

Link to all media files:

https://drive.google.com/a/nyu.edu/folderview?id=0BxwejuremnKLYmZGTkFCU0lfRVU&usp=drive_web

Screenshot of gameplay
Screenshot of gameplay
Winner's screen!
Winner’s screen!

 

Players compete to shake and dance the most. A video input captures motion and determines the changes from one screen to another, thus detecting the motion of each player.

Progress to Infinity

For my final project, I made another game. There were about 4 factors that went into this game idea.

  1. Unlike my other games, I wanted to challenge myself and not be reliant on PImages for the actual graphics and physically draw shapes and lines myself.
  2. Since we  so recently wrote our blog post on Computational Media, I think I was still very oriented towards that meditation on computers and their capabilities, etc.
  3. I wanted to incorporate the first part of our class that focused on the Arduino because it was so integral to our course with its scope on the physical world of computing.

So with all those factors in mind, I thought about the circle and how basically it is a polygon with an infinite amount of sides. This topic of “infinity” reminded me of David Deutsch’s Beginning of Infinity , where  he claims that progress is boundless – or infinite. So by somehow weirdly merging the those thoughts in my mind, the game where the goal was to become a circle (that represented infinity with its “infinite” number of sides) became its own ” progress towards infinity”.

This is my first multiplayer game (woohooo, double the fun!) and the movement of the players are controlled by the  ASDW and the arrow keys. In each level, there are foreign words floating around that mean the number of the next shape the player must become. To advance, the player must retrieve all of his respective words. For example, for a square to become a polygon, the player must collect all 5 words that mean “5”. The words are also the same color as the player so it makes the game easier than just a mere translation exercise and actually possible to finish within a more reasonable amount of time.

Here is the tutorial demo video: https://www.youtube.com/watch?v=4atf-fCveh0

 

Twist the dials to change your color!
Twist the dials to change your color!

 

I did throw in one last surprise so unfortunately the file needs to be in the data folder along with the fonts in order for the code to run correctly.

So to download everything: https://drive.google.com/folderview?id=0B9jm5VZVC2_oNFZtVUdCZ1hXMVk&usp=sharing

And here is the scary code. The switch statements got very repetitive but I wasn’t able to modularize the actual level changing function because then it ended up being that I would have to return an array and somehow pass it back into the same function (all because I have so many switches within each level).

import processing.serial.*;
Serial myPort;

int pot1;
int pot2;
int pot3;
int converted1;
int converted2;
int converted3;

Shape player_1;
Shape player_2;
Text opening;

ArrayList<Text>player1text;
ArrayList<Text>player2text;

//OPENING:

PFont type;

//Beginning: 
boolean start;

boolean player_1_choice;
boolean player_2_choice;
int final_red1;
int final_green1;
int final_blue1;
int final_red2;
int final_green2;
int final_blue2;

//GAME
boolean game_on;
int level_count1= 1; 
int level_count2 = 1;
boolean level_start;
boolean level_start2;

//WINNING:
boolean finish;
Boolean win1;
Boolean win2;
boolean win_keys;
boolean win_reset1;
boolean win_reset2;
boolean win_text1;
boolean win_text2;

void setup() {
  size(1000, 700,P2D);

  //Beginning:
  start = true;
  game_on = false; 
  type = loadFont("Cochin-48.vlw");
  boolean player_1_choice = true;
  boolean player_2_choice = false;
  int final_red1=0;
  int final_green1=0;
  int final_blue1=0;
  int final_red2=0;
  int final_green2=0;
  int final_blue2=0;

  //Gameplay:
  level_start = false;
  level_start2 = false;
  player_1 = new Shape();
  player_1.set_Position(200,300,4);
  player_2 = new Shape();
  player_2.set_Position(800,300,-4);

  player1text = new ArrayList<Text>();
  player2text = new ArrayList<Text>();
  //Only for level 1:
  for (int i = 0;i<2;i++) {
    player1text.add(new Text((int)random(1, 11), 1));
    player1text.get(i).set_text_color(final_red1, final_green1, final_blue1);
    player2text.add(new Text((int)random(1, 11), 1));
    player2text.get(i).set_text_color(final_red2, final_green2, final_blue2);
  }
  //Winning:
  finish = false;
  win1 = false;
  win2=  false;
  win_keys = false;
  win_reset1 = false;
  win_reset2 = false;
  win_text1 = false;
  win_text2 = false;

 //ARDUINO --> PROCESSING: 
  println(Serial.list());
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
 myPort.readStringUntil('\n');
}//End of setup

void draw() {
  background(255);

  if (start) {
    background(255);
    fill(0);
    textFont(type, 36);
    text("Welcome to the beginning of infinity.\n Please press the '1' to begin color selection for player 1.\n Mouse click to re-orient the dial inputs.", width/10, height /4);

    if (player_1_choice) {
      background(255);
      text("Player 1: please select color by turning each dial. \n Press '2' to save your selection and let Player 2 choose.", width/9, height/4);
      final_red1 = converted1;
      final_green1 = converted3;
      final_blue1 = converted2;
      fill(final_red1, final_green1, final_blue1);
      rect(100, height/2, 200, 200);
    }
    else if (player_2_choice) {
      background(255);
      text("Player 2: please select your color by turning the dial.\n To begin the game press 'ENTER'", width/8, height/4);
      final_red2 = converted1;
      final_green2 = converted3;
      final_blue2 = converted2;
      fill(final_red2,final_green2, final_blue2);
      rect(700, height/2, 200, 200);
    }
  }

  //game start
  if (game_on) {

    //PLAYER 1 SWITCH
    switch (level_count1) {
    case 1:
      player_1.drawShape1();
       ;
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //TEXT
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).set_text_color(final_red1, final_green1, final_blue1);
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;

          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }

      break;
    case 2:
      player_1.drawLine();
       ;
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<3;i++) {
          player1text.add(new Text((int)random(1, 11), 2));
        }
        for (int i = 0; i<3; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;

          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 3:
      player_1.drawShape2(3);
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<4;i++) {
          player1text.add(new Text((int)random(1, 11), 3));
        }
        for (int i = 0; i<4; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }

      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;
          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 4:
      player_1.drawShape2(4);
       ;
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<5;i++) {
          player1text.add(new Text((int)random(1, 11), 4));
        }
        for (int i = 0; i<5; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;

          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 5:
      player_1.drawShape2(5);
       ;
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<6;i++) {
          player1text.add(new Text((int)random(1, 11), 5));
        }
        for (int i = 0; i<6; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;

          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 6:
      player_1.drawShape2(6);
       ;
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<7;i++) {
          player1text.add(new Text((int)random(1, 11), 6));
        }
        for (int i = 0; i<7; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;

          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 7:
      player_1.drawShape2(7);
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<8;i++) {
          player1text.add(new Text((int)random(1, 11), 7));
        }
        for (int i = 0; i<8; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;
          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 8:
      player_1.drawShape2(8);
       ;
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<9;i++) {
          player1text.add(new Text((int)random(1, 11), 8));
        }
        for (int i = 0; i<9; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;
          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 9:
      player_1.drawShape2(9);
      player_1.moveShape();
      if (player_1.checkBoundaries()) {
        player_1.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<10;i++) {
          player1text.add(new Text((int)random(1, 11), 9));
        }
        for (int i = 0; i<10; i++) {
          player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
      }
        level_start = false;
      }
      for (int i = 0;i<player1text.size();i++) {
        player1text.get(i).drawText();
        player1text.get(i).moveText();
        if (player1text.get(i).checkBoundaries()) {
          player1text.get(i).move_back_text();
        }
        if (player1text.get(i).check_Collision(player_1)) {
          player1text.remove(i);
          i--;
          if (player1text.isEmpty()) {
            level_count1++;
            level_start = true;
          }
        }
      }
      break;
    case 10:
      game_on = false;
      finish = true;
      win1 = true;
      win_keys = true;
      win_reset1 = true;
      break;
    } // End of player 1

    //PLAYER 2
    switch (level_count2) {
    case 1:
      player_2.drawShape1();
      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //TEXT
      for (int i = 0;i<player2text.size();i++) {
       player2text.get(i).set_text_color(final_red2, final_green2, final_blue2);
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;
          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 2:
      player_2.drawLine();
      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //Text
      if (level_start2) {
        for (int i = 0;i<3;i++) {
          player2text.add(new Text((int)random(1, 11), 2));
        }
        for (int i = 0; i<3; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;

          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 3:
      player_2.drawShape2(3);

      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //Text
      if (level_start2) {
        for (int i = 0;i<4;i++) {
          player2text.add(new Text((int)random(1, 11), 3));
        }
        for (int i = 0; i<4; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;
          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 4:
      player_2.drawShape2(4);

      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //TEXT
      //Text
      if (level_start2) {
        for (int i = 0;i<5;i++) {
          player2text.add(new Text((int)random(1, 11), 4));
        }
        for (int i = 0; i<5; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;

          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 5:
      player_2.drawShape2(5);
      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //TEXT
      //Text
      if (level_start2) {
        for (int i = 0;i<6;i++) {
          player2text.add(new Text((int)random(1, 11), 5));
        }
        for (int i = 0; i<6; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;

          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 6:
      player_2.drawShape2(6);

      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<7;i++) {
          player2text.add(new Text((int)random(1, 11), 6));
        }
        for (int i = 0; i<7; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;
          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 7:
      player_2.drawShape2(7);
      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //Text
      if (level_start) {
        for (int i = 0;i<8;i++) {
          player2text.add(new Text((int)random(1, 11), 7));
        }
        for (int i = 0; i<8; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;
          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 8:
      player_2.drawShape2(8);
      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //Text
      if (level_start2) {
        for (int i = 0;i<9;i++) {
          player2text.add(new Text((int)random(1, 11), 8));
        }
        for (int i = 0; i<9; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;
          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 9:
      player_2.drawShape2(9);
      player_2.moveShape();
      if (player_2.checkBoundaries()) {
        player_2.move_back();
      }
      //Text
      if (level_start2) {
        for (int i = 0;i<10;i++) {
          player2text.add(new Text((int)random(1, 11), 9));
        }
        for (int i = 0; i<10; i++) {
          player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
      }
        level_start2 = false;
      }
      for (int i = 0;i<player2text.size();i++) {
        player2text.get(i).drawText();
        player2text.get(i).moveText();
        if (player2text.get(i).checkBoundaries()) {
          player2text.get(i).move_back_text();
        }
        if (player2text.get(i).check_Collision(player_2)) {
          player2text.remove(i);
          i--;
          if (player2text.isEmpty()) {
            level_count2++;
            level_start2 = true;
          }
        }
      }
      break;
    case 10:
      game_on = false;
      finish = true;
      win2 = true;
      win_keys = true;
      win_reset2 = true;
      break;
    } // End of player 2
  }// End of game_on

  //WINNERS: 
  else if (finish) {
    //player 1 win:
    if (win1) {
      if (win_reset1) {
        player_1.win_reset();
        win_reset1 = false;
      }
      player_1.win_animation();
      if (player_1.get_win()) {
        textFont(type, 32);
        fill(255);
        text("To Infinity and Beyond", 500, 300);
      }
    }//play1win

    //player 2 win:

    if (win2) {
      if (win_reset2) {
        player_2.win_reset();
        win_reset1 = false;
      }
      player_2.win_animation();
      if (player_2.get_win()) {
        textFont(type, 32);
        fill(255);
        text("To Infinity and Beyond", 500, 300);
      }
    }//play1win
  }//end of finish
}// End of Draw

void keyPressed() {
  if (start) {
    if (key=='1'){
      player_1_choice = true;
    }
    if (key=='2') {
      player_1.set_color(final_red1, final_green1, final_blue1);
      player_1_choice = false;
      player_2_choice = true;
    }
    else if (key==ENTER && player_2_choice == true) {
      player_2.set_color(final_red2, final_green2, final_blue2);
      start = false;
      game_on = true;
    }
  }//end of if start

  if (win_keys==false) {
    if (key==CODED) {
      if (keyCode == UP) {
        player_1.set_y_velocity(-2);
        player_1.set_x_velocity(0);
      }
      else if (keyCode == DOWN) {
        player_1.set_y_velocity(2);
        player_1.set_x_velocity(0);
      }
      else if (keyCode == RIGHT) {
        player_1.set_x_velocity(2);
        player_1.set_y_velocity(0);
      }
      else if (keyCode == LEFT) {
        player_1.set_x_velocity(-2);
        player_1.set_y_velocity(0);
      }
    } //end of key code

    else if (key=='a') {
      player_2.set_x_velocity(-2);
      player_2.set_y_velocity(0);
    }
    else if (key=='d') {
      player_2.set_x_velocity(2);
      player_2.set_y_velocity(0);
    }
    else if (key=='w') {
      player_2.set_y_velocity(-2);
      player_2.set_x_velocity(0);
    }
    else if (key=='s') {
      player_2.set_y_velocity(2);
      player_2.set_x_velocity(0);
    }
  }
}//End of keyPress

void serialEvent(Serial _port) { //this is a callback function
  if (myPort == null) return;  //this is a hack to cover a bug where the port does not get set up in time.  
  ///this says if the port is not set up yet, bail (for now.) 
  String input = myPort.readStringUntil('\n');
  if (input != null) {  //if a '\n' character has in fact now arrived
    String [] dual_inputs = split(input, ",");
   input = input.trim();  //Take off the '\n' character
    pot1 = int(dual_inputs[0]);  //Turn it into number
    pot2 = int(dual_inputs[1]); 
    pot3 = int(dual_inputs[2]);
    converted1 = (int)map(pot1, 0, 1024, 0, 256);
   converted2 = (int)map(pot2, 0, 1024, 0, 256);
    converted3= (int)map(pot3, 0, 1024, 0, 256);
    println(converted1, converted3, converted2);
    myPort.write("Go");
  }
}//END OF KEY EVENT

void mousePressed(){
myPort.write("A");
}

Here is the SHAPE class:

class Shape {
  float x;
  float y;
  int velocity_x;
  int velocity_y;
  int radius; 
  float new_x;
  float new_y;
  int magnitude = 2;
  int shape_height = radius * 2;
  int shape_width = radius * 2; 
  int red;
  int green;
  int blue;

  PShape player;

  //Stages of Win:
  boolean win = false;

  //Constructor: 
  Shape() {
    velocity_x = 4;
    velocity_y=0;
    radius = 60;
    shape_height = radius * 2;
    shape_width = radius * 2;
  }
  void drawShape1() {
    fill(red,green,blue);
    ellipse(x, y, 8, 8);
  }
void drawLine(){
  fill(red, green, blue);
  line(x-(radius/2), y,x+(2 * radius),y);
  ellipse(x,y, 2,2);
}
  void drawShape2(int number_of_sides) {
    FloatList new_x_coords;
    new_x_coords = new FloatList();
    FloatList new_y_coords;
    new_y_coords = new FloatList();
    float angle = 0;

//FILL ARRAY WITH VERTEXES 
    for (int i=0;i<number_of_sides; i++) {
      new_x = cos(angle) * radius;
      new_y = sin(angle) * radius;
      new_x_coords.append(new_x);
      new_y_coords.append(new_y);
      angle+= ((2*PI)/(number_of_sides));
    }

    player = createShape();
    player.beginShape();
    for (int i = 0; i<new_x_coords.size(); i++){
    player.vertex(new_x_coords.get(i), new_y_coords.get(i));}
    player.endShape();
    player.setStroke(color(0));
    player.setFill((color(red,green,blue)));
    shapeMode(CENTER);
    shape(player, x, y);

  } //End of drawing shape again

  //Set velocity:
  void set_x_velocity(int direction ) {
    velocity_x = direction * magnitude;
  }

  void set_y_velocity(int direction ) {
    velocity_y = direction * magnitude;
  }

  void set_magnitude(int potentiometer_input) {
    magnitude = potentiometer_input;
  }

  //MOVEMENT
  void moveShape() {
    x+= velocity_x;
    y+= velocity_y;
  }

  boolean checkBoundaries() {
    boolean out_of_bounds = false;
    //Out of screen (horizontal)
    if (x>(width+(shape_width))|| x < (0 - (shape_width))) {
      out_of_bounds = true;
    }

    //Out of screen (vertical)
    if (y>(height+(shape_height)) || y < (0 - (shape_height))) {
      out_of_bounds = true;
    }
    return out_of_bounds;
  }

  void move_back() {
    if (y < 0 - shape_height) {
      y = height;
    }
    else if (y > height+shape_height) {
      y = 0;
    }

    else if (x < 0 - shape_width) {
      x = width;
    }
    else if (x>width+shape_width) {
      x = 0;
    }
  }

  //Getters:
  float get_X_Position() {
    return x;
  }
  float get_Y_Position() {
    return y;
  }
  int get_shape_Height() {
    return shape_height;
  }

  int get_shape_Width() {
    return shape_width;
  }

  boolean get_win() {
    return win;
  }

  //Setters: 
  void set_Position(float x, float y, int x_v) {
    this.x  = x;
    this.y =y;
    this.velocity_x = x_v;
  }

void set_color(int _red, int _green, int _blue){
  red = _red;
  green = _green;
  blue = _blue;
}
  //WINNER:
  void win_reset() {
    x = 0+shape_width;
    y = height / 2;
    velocity_x = 2;
    velocity_y = 0;
  }

  void drawCircle() {
    fill(red, green, blue);
    ellipse(x, y, radius*2, radius*2);
  }

  void win_animation() {
    if (x < width / 5) {
      this.drawShape2(10);
      this.moveShape();
    }
    else if (x < (2*(width / 5)) && x >= (width / 5)) {
      this.drawShape2(14);
      this.moveShape();
    }

    else if (x < (3*(width / 5)) && x >= (2*width) / 5) {
      this.drawShape2(18);
      this.moveShape();
    }

    else if (x < (4*(width / 5)) && x >= (3*width) / 5) {
      this.drawShape2(22);
      this.moveShape();
    }
    else if (x < width && x >=  (4*(width / 5))) {
      this.drawCircle();
      this.moveShape();
      if (x == width - 60) {
        velocity_x=0;
        if (radius< 1010) {
          radius+=10;
        }
        else if (radius == 1010) {
          win = true;
        }//end of win sign
      }//side of screen
    }//end of last else-if
  }//win animation
}//End of class

Here is the TEXT class:

class Text {

  //Different languages arrays that must be converted into stringsList
  String [] EnglishArray = {
    "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"
  };
  StringList English = new StringList(EnglishArray);
  String [] GermanArray = {
    "eins", "zwei", "drei", "vier", "funf", "sechs", "sieben", "acht", "neun", "zehn"
  };
  StringList German = new StringList(GermanArray);
  String [] FrenchArray = {
    "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix"
  };
  StringList French = new StringList(FrenchArray);
  String [] HindiArray = {
    "ek", "do", "teen", "char", "panch", "chech", "saat", "aath", "nau", "das"
  };
  StringList Hindi = new StringList(HindiArray);
  String [] JapaneseArray = {
    "ichi", "ni", "san", "yon", "go", "roku", "nana", "hatchi", "kyu", "ju"
  };
  StringList Japanese = new StringList(JapaneseArray);
  String [] MandarinArray= {
    "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu", "shi"
  };
  StringList Mandarin = new StringList(MandarinArray);
  String [] ItalianArray = {
    "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove", "dieci"
  };
  StringList Italian = new StringList(ItalianArray);
  String [] SpanishArray = {
    "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez"
  };
  StringList Spanish = new StringList(SpanishArray);
  String [] RegularNumbersArray = {
    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
  };
  StringList RegularNumbers = new StringList(RegularNumbersArray);
  String [] BinaryArray = {
    "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010"
  };
  StringList Binary = new StringList(BinaryArray);
  String [] SwahiliArray = {
    "moja", "mbili", "tatu", "nne", "tano", "sita", "saba", "nane", "tisa", "kumi"
  };

  PFont basic_font = loadFont("ACaslonPro-Regular-32.vlw");

  float x;
  float y;
  float velocity_x;
  float velocity_y;
  float text_height;
  float text_width;
  String words;
  int which_language;
  int index;
  int red;
  int green;
  int blue;

  //Constructor for load screen/win
  Text(String input, int fontsize) {
    x = width / 2;
    y = height / 4;
    textFont(basic_font);
    words = input;
    textSize(fontsize);
    textAlign(LEFT);
  }

  //Constructor for regular game:
  Text(int language, int number) {
    x = random(0,width+1 - text_width);
    y = random(0, height+1 - text_height);
    velocity_x = random(0,6);
    velocity_y = random(1,6);

    which_language = language;
    index = number;
  }

  void drawText() {
    fill(red, green, blue);
    String word="";
    switch(which_language) {
    case 1: 
      word = EnglishArray[index];
      break;
    case 2:
      word = GermanArray[index];
      break;
    case 3:
      word = FrenchArray[index];
      break;
    case 4:
      word = HindiArray[index];
      break;
    case 5:
      word = MandarinArray[index];
      break;
    case 6:
      word = ItalianArray[index];
      break;
    case 7:
      word = SpanishArray[index];
      break;
    case 8:
      word = RegularNumbersArray[index];
      break;
    case 9:
      word = BinaryArray[index];
      break;
    case 10:
      word = SwahiliArray[index];
      break;
    }
    text(word, x,y);
    textFont(basic_font);
    textSize(25);
    textAlign(LEFT);
    text_height = textDescent() + textAscent();
    text_width = textWidth(word);

  } // End of Draw

  void moveText() {
    x+= velocity_x;
    y+= velocity_y;
  }

  //MOVEMENT 
  boolean checkBoundaries() {
    boolean out_of_bounds = false;
    //Out of screen (horizontal)
    if (x>(width+(text_width))|| x < (0 - (text_width))) {
      out_of_bounds = true;
    }

    //Out of screen (vertical)
    if (y>(height+(text_height)) || y < (0 - (text_height))) {
      out_of_bounds = true;
    }
    return out_of_bounds;
  }

  void move_back_text() {
    //Will randomize the side from which the text returns
    //Will maintain the same magnitude of velocity
    /* Must figure out magnitude from original x and y velocities
     Then use magnitude to determine the new velocities 
     Use random to find new angle of reentry (radians)
     magintude SIN (theta) = y
     magnitude COS (theta) = X
     */

    float theta = random(.1*PI, .9*PI);
    float magnitude = 0;
    magnitude = sqrt((float)(Math.pow(velocity_x, 2))+(float)(Math.pow(velocity_y, 2)));
    int side = (int) random(1, 5);
    switch (side) {
      //Top
    case 1: 
      y = 0 - text_height; 
      x = width/2;
      velocity_y = (magnitude * sin(theta)); 
      velocity_x = (magnitude * cos(theta)); 
      break;

      //Bottom
    case 2:
      y = height + text_height;
      x = 450;
      velocity_y = -(magnitude * sin(theta)); 
      velocity_x = (magnitude * cos(theta));
      break;

      //Left
    case 3:
      y = 375;
      x = 0 - text_width;
      velocity_y = (magnitude * sin(theta)); 
      velocity_x = (magnitude * cos(theta));
      break;
      //Right
    case 4:
      y = 375;
      x = width + text_width;
      velocity_y = (magnitude * sin(theta)); 
      velocity_x = -(magnitude * cos(theta));
      break;
    }
  }

  //COLLISION

boolean check_Collision(Shape temporary) {
    boolean signal = false;

    int temp_shape_height = temporary.get_shape_Height();
    int temp_shape_width = temporary.get_shape_Width();
    float temp_x = temporary.get_X_Position()-temp_shape_width/2;
    float temp_y = temporary.get_Y_Position()-temp_shape_height/2;
    if (x < (temp_x + temp_shape_width) && (x+text_width) > (temp_x )) {   
      if (y < (temp_y + temp_shape_height) && (y+text_height) > (temp_y )) {
        signal = true;
      }
      else {
        signal = false;
      }
    }
    return signal;
  }

void set_text_color(int _red, int _green, int _blue){
  red = _red; 
  green = _green;
  blue = _blue;
}

}//End of text

To improve on this game, and to progress on this further, I’d maybe think about venturing into the third dimension for the shapes, making it more interactive (with the video tracking) so it can be a team game, and making obstacles!

I had a really fun time this class  and I had a lotta fun with you all, through bugs of frustration to sheer hilarity to coding successes! : )

 

Final Project: Visualizing Sound

 

Guysssss I’m gonna miss y’all. Thank you for a great semester. Friend me on the ‘book, and/or check out ma website to stay in touch!

tuneshot-1338

 

 

Step 1 of ‘the dream’ is complete. The program plays a select set of songs and visualizes the music using a forward fourier transformation (fft). The “.getBand()” command is used to quantify the various aspects of the audio waves.

 

tuneshot-876

As a user, hold down the ‘P’ key to view the menu. It will tell you that you have the option to change the song (with the number keys), the colors of the visualization (keys a through g). You can increase and decrease the intensity of the visualization with the ‘=’ and ‘-‘ keys, respectively. Finally, you can pause the movie with the ‘I’ key and take a screenshot by clicking on the screen. downloadable .zip file – including .mp3’s – here.

 

Worldstar-quality video clip of the in-class demo

 

Code:

import ddf.minim.*;
import ddf.minim.ugens.*; 
import ddf.minim.analysis.*;
import processing.video.*;

int spectrum;
float videoHeight;
float videoWidth;
float threshold;
float multiplier;
PFont font;
StringList playlist;
color ringColor;
color antiColor;

Minim       minim;
AudioPlayer jingle;
FFT         fft;
Capture     video;

void setup()
{
  //video presets
  font = loadFont("MicrosoftTaiLe-Bold-12.vlw");
  video = new Capture(this, 640, 480);
  video.start();
  threshold = 20;

  //sound presets
  minim = new Minim(this);
  jingle = minim.loadFile("bt.mp3", 1024);
  fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
  spectrum = fft.specSize();
  multiplier = 20;

  //sound-dependant video settings
  videoHeight = ((spectrum - 480)/2)+1;
  videoWidth = ((spectrum - 640)/2)+1;

  //sound-dependant background
  size(spectrum, spectrum);
}

void draw() {

//tracklist, key commands
if (keyPressed) {
  if (key == 'p') {background(255);
                  fill(0);
                  textFont(font, 12);
                  textAlign(CENTER);
                  text("Press 'P' at any time for playlist options, press number keys to change the song:",(width/2), 25);
                  text("1: One Direction - Happily",(width/2),50);
                  text("2: No Doubt - Sunday Morning",(width/2),75);
                  text("3: Beethoven - 5th Symphony",(width/2),100);
                  text("4: Andrew McMahon - Catching Cold",(width/2),125);
                  text("5: DJ Naim - Crispy Hunneds",(width/2),150);
                  text("6: Arctic Monkeys - Do I Wanna Know?",(width/2),175);
                  text("7: Grimes - Oblivion",(width/2),200);
                  text("8: Frank Ocean (feat. Diplo) - Hero",(width/2),225);
                  text("9: Srkrillex - Bangarang",(width/2),250);
                  text("0: Grimes - Genesis",(width/2),275);
                  text("Press a, b, c, d, e, f, and g keys to change the color profile", (width/2), 325);
                  text("Press = to increase the intensity, - to decrease the intensity of the waves", (width/2), 350);
                  text("Press 'I' to pause the current song", (width/2), 375);
                  text("Click on the screen to snap a screenshot!", (width/2), 400);
                  }

//color changer
  if (key == 'a') {ringColor = color(255,0,0); antiColor = color(0,255,0);};
  if (key == 'b') {ringColor = color(0,255,0); antiColor = color(255,0,0);};
  if (key == 'c') {ringColor = color(255,0,0); antiColor = color(0,0,255);};  
  if (key == 'd') {ringColor = color(0,255,0); antiColor = color(0,0,255);};
  if (key == 'e') {ringColor = color(0,0,255); antiColor = color(255,0,0);};
  if (key == 'f') {ringColor = color(0,0,255); antiColor = color(0,255,0);};
  if (key == 'g') {ringColor = color(random(255),random(255),random(255)); antiColor = color(random(255),random(255),random(255));};

//intensity changer
  if (key == '=') {multiplier = multiplier + 5;};
  if (key == '-') {multiplier = multiplier - 5;};

//pause button
  if (key == 'i') {jingle.pause();};

//track changer
  if (key == '1') {jingle.pause();jingle = minim.loadFile("oned.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '2') {jingle.pause();jingle = minim.loadFile("nod.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '3') {jingle.pause();jingle = minim.loadFile("bt.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '4') {jingle.pause();jingle = minim.loadFile("catch.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '5') {jingle.pause();jingle = minim.loadFile("crispy.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '6') {jingle.pause();jingle = minim.loadFile("doi.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '7') {jingle.pause();jingle = minim.loadFile("oblivion.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '8') {jingle.pause();jingle = minim.loadFile("hero.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '9') {jingle.pause();jingle = minim.loadFile("skr.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '0') {jingle.pause();jingle = minim.loadFile("genesis.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
    }

else {
  //launch music analysis
  fft.forward(jingle.mix);

  //video section
  if (video.available()) {
   video.read();
  }
    video.loadPixels();
    image(video,videoWidth, videoHeight);

  //for loop for EQs
  for (int i=0; i < fft.specSize(); i++) {
    float level = fft.getBand(i)*multiplier;
    noFill();
    strokeWeight(3);
    stroke(ringColor,random(100,200));
    //top left & right
    line(i, 0, i, level);line(width - i, 0, width - i, level);
    //bottom left & right
    line( i, height, i, height - level);line( width - i, height, width - i, height - level);
    stroke(antiColor,random(100,200));
    //left top & bottom
    line(0, i, level, i);line(0, height - i, level, height - i);
    //right top & bottom
    line(width, i, width - level, i);line(width, height - i, width - level, height - i);
    //oval
    if (level > 350 && level < 450) {
       strokeWeight(15);
       stroke(ringColor,random(0,150));
       ellipse((width/2),(height/2), level, level*1.2);
    }
  }
  };};

//  screenshot
void mousePressed() {
    saveFrame("tuneshot-###.png");
  }

ASL Learning Game (attempt)

So I decided to build upon my ASL project that I did earlier this semester. Originally I was going to do a search engine using the ASL alphabet. But during the user testing session Anna had an idea to make it into an ASL learning game. What would happen is at the top of the screen images of the handshapes of letters would appear. The object is that you would copy the handshapes which would input a query (which you wouldn’t see) and then it would pull up the first google image of what you signed. Then you would type into an answer box what you think the word you signed meant. If you got it correct you’d move onto the next level, if not you’d have to guess again. When I started putting all of these ideas together I ran into many road blocks though. When I first started I was trying to use a LeapMotion but it wasn’t working with Processing at all. Then when I started with the flex sensors I couldn’t get the Arduino to work (turns out I had the wrong numbers put into the program, but whatever). Then I got the Arduino to work but I had to find all the various numbers for the letters again, and it wasn’t working as nicely as the last time I ran the ASL program. Next I just tried to get Processing to print the letter on the screen, but I couldn’t figure out how to get readings from the Arduino to be slower and I couldn’t figure out how to make the letters into one string. Because I couldn’t figure out this I wasn’t able to think of a way to get the letters into a query. So I don’t have the entire project all together… But I do have all the pieces of the code (except for the ASL Handshape pictures).

Here is the Processing code for getting the letters to show up on the screen.

import processing.serial.*;

Serial myPort;
int flexFore;
int flexNumbers;

void setup() {
  size(640, 480);
  // List all the available serial ports:
  printArray(Serial.list());
  // Print out the list and look for port your Microcontroller is on
  // Finding the Arduino is not easy because they have weird names on the mac and on the pc they look like COM1.
  // Ardnino is almost always the first one so you can usually use Serial.list()[0].
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
  myPort.readStringUntil('\n');  //clean out anything already in the buffer
  size(640, 480);
}

void draw() {
  background(255);
  textSize(40);
  fill(0);
  if (flexNumbers >= 90 && flexNumbers <=100) {
    if (flexFore >= 25 && flexFore < 40) {
      text("A",width/2,height/2);
      delay(1000);
    }
  }

  if (flexNumbers >= 90&& flexNumbers <110) {
    if (flexFore >=100 && flexFore <115) {
      text("H",width/2,height/2);
      delay(1000);
    }
  }
  if (flexNumbers >=80 && flexNumbers <90) {
    if (flexFore >= 100 && flexFore <115) {
      text("G",width/2,height/2);
      delay(1000);
    }
  }
   if (flexNumbers >=60 && flexNumbers <70) {
    if (flexFore >=50 && flexFore <65) {
text("C",width/2,height/2);
      delay(1000);
    }
  }
  if (flexNumbers >=50 && flexNumbers <60) {
    if (flexFore >=40 && flexFore <55) {
text("F",width/2,height/2);
      delay(1000);
    }
  }
  if (flexNumbers >=70&& flexNumbers <78) {
    if (flexFore >= 90 && flexFore <=115) {
text("D",width/2,height/2);
      delay(1000);
    }
  }
  else if (flexNumbers >=30 && flexNumbers <65) {
    if (flexFore >=90 && flexFore <=115) {
text("B",width/2,height/2);
      delay(1000);
    }
  }
  if (flexNumbers <50) {
    if (flexFore >= 0 && flexFore < 50) {
text("E",width/2,height/2);
      delay(1000);
    }
  }
  else if (flexNumbers > 110) {
    if (flexFore >115 ) {
text(" ",width/2,height/2);
      delay(1000);
    }
  }
}

void serialEvent(Serial _port) { //this is a callback function
  if (myPort == null) return;
  String input = myPort.readStringUntil('\n');
  if (input != null) {  //if a '\n' character has in fact now arrived
    String[] parts = split(input, ",");  //split the message into parts based on the comma
    //the parts got put into a special kind of variable called an array that has slots
    String var1 = parts[0]; //they came in the order they were sent 
    String var2 = parts[1]; //so the force will be in the first slot of the array fire in second
    flexFore = int(var1);  //Turn it into a number for eelipse in draw
    flexNumbers = int(var2);  //Turn it into a number for fill in draw
    println(flexFore, flexNumbers);
  }
}

And here is the query code:

import java.io.*;
String query = "";

void setup() {
  size(640, 480);
  background(0);
  fill(255, 255, 255);
}

void draw() {
}

void queryGoogleImages(String _q) {
  String url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + _q;
  //this is a special url for getting google image results as json
  println(url);
  JSONObject result =  loadJSONObject(url);
  //make a network call and get the results back into a json object
  println(result);
  //make sure the results are not an error
  int status = result.getInt("responseStatus");
  if (status != 200) {
    String reason = result.getString("responseDetails");
    //this sucks that google does not allow you to do very many of these in a row
    println("You are too fast, take a break." + reason);
    return;
  }
  //go one level down in to the results to just get the data not the info about the package
  JSONObject response = result.getJSONObject("responseData");
  println(response);
  //get the arrary of stuff you got back
  JSONArray values = response.getJSONArray("results");
  int vOffset = 20;
  for (int i = 0; i < min(values.size(),3); i++) {
    //pick out the picture information for each picture
    JSONObject thisGuy = values.getJSONObject(i); 
    String r = thisGuy.getString("url");
    int w = thisGuy.getInt("tbWidth");
    int h = thisGuy.getInt("tbHeight");
    println(r + " "+ w + " " + h );
    PImage thisImage = loadImage(r);
    if (thisImage != null) {
      image(thisImage, 0, 0, 640, 460);
    }
    else {
      text((i + 1) + " key", 10, vOffset+13);
      rect(10, vOffset, w, h);
    }
    //make vOffset bigger so we will draw the next picture below this one.
    vOffset  += h;

  }//end for
}

void keyPressed() {
  if (keyCode == RETURN || keyCode == ENTER) {
    background(0);
    queryGoogleImages(query); //execute the query
    query = "";  //clear the query
  } 
  else if (key > 48 && key < 123) {  //letters and numbers only
    query = query + key;
    text(query, 320, 470);  //draw the query so far on the scrren
  }
}

 

Rainy Day Flower Video

For my final, I went back and forth between a few ideas, but ultimately, I decided to address the two elements that I had wanted to try but hadn’t figured out yet: movie and minim. The video is simple because my intention was to include more opportunities for user interaction, but at least it is functional. I had to make the choice between something that works and something that is complex…and I chose something that works. I used some random video that I shot the other day on an iPhone (this is not the video I had originally planned to use) because it is pretty and simple. I chose Youth Lagoon’s “17” to accompany it, as both the movie and the music are nice and calm. To add a bit more complexity, I added a mouseClick function, which simultaneously pauses the movie and the music. This turns the project into a music video player! When the mouse is released, both the movie and music return. I have a key pressed function that evaluates the brightness of the video and then colors the sky blue while the key is pressed to give the appearance of a rainy day/thunderstorm. For this part, I hoped to manipulate the video by tracking someone’s movement with the webcam, but ultimately, I couldn’t figure out how to do this. Thought that I could change the pitch and/or volume of the music that way, or the playback speed of the video. Since I struggled to translate the data from the computer’s vision into manipulation of the video on screen, I replaced that portion with a keyPressed function so when the spacebar is pressed, the video speeds up and the sky turns blue like a rainstorm! I know this looks like a pretty simple project, but for my final,  I really wanted to make something entirely functional and visually appealing.

import processing.video.*;
Movie myMovie;

import ddf.minim.*;
Minim minim;
AudioPlayer song;

void setup() {
  size(960, 540);
  myMovie = new Movie(this, "TreeCircle.mov");
  myMovie.loop();

  minim=new Minim(this);
  song= minim.loadFile("17.mp3");
  song.play();

}

void draw() {
  image(myMovie, 0,0, 960, 540);

    }

void mousePressed() {
  myMovie.pause();
  song.pause();
}

void mouseReleased() {
  myMovie.play();
  song.play();

}

void keyPressed() {
 for (int i = 0; i < myMovie.pixels.length;  i++) {  //you can ask the pixel array how long it is
    if ( brightness(myMovie.pixels[i]) > 200) {  //check the brightness of the pixel
      myMovie.pixels[i] = color(0, 200, 250,150);  //
      myMovie.speed(4.0);
    }}}

// Called every time a new frame is available to read
void movieEvent(Movie m) {
  m.read();

}

void stop() {
  song.close();
  minim.stop();

  super.stop();
}

 

The city that everyone loves to write about

I was juggling several ideas for this final project like painting with light or building an interactive playlist you can share with people, but boy, did I have trouble figuring that out. I haven’t let go of those dreams completely, just momentarily, and decided to work with a concept I call “poetic data” — subverting how data is being used these days to target you with ads or to spy on you. Recently, the NY Times selected and published submissions of haikus about New York livin’. I created a generator that would mashup some of the published haikus I liked often nonsensically but still capture the human heart behind each carefully crafted line.

Instructions: Hold down and move around your mouse to see the haikus (fast-paced on purpose, but maybe too fast so I’m going to work on a variable for the speed). Release to see an animation of your mouse path (I explored PGraphics a bit).

-Ashley

int frames = 20;
PGraphics pg[] = new PGraphics[frames]; //a PGraphics collection of 20 frames
PFont myFont;
int y = 300;

//Haikus consist of three lines of 5, 7, then 5 syllables.
String[] firstLn = {
  "Beware the puddle", 
  "Our eyes avoid but", 
  "Mistrust grips the heart", 
  "If build and destroy",
  "Coffee by myself",
  "The New Yorker is",
  "The City's timeless",
  "We can spend the night",
  "Hidden among the",
  "As we leave for work",
  "I know you, don't I"
};

String[] secondLn = {
  "of indeterminate depth", 
  "If we looked we would see that", 
  "Thought we travel in large packs",
  "Are music notes, our island",
  "The wind whispers names of friends",
  "Not kind, they say. I say, he",
  "But still, don't let it fool you",
  "together, but I expect",
  "Sleepwalking, caffeine zombies",
  "Youngsters head home from parties",
  "You were me five years ago"
};

String[] thirdLn = {
  "that swallows boots whole", 
  "We might just be friends", 
  "we are still alone",
  "Is a symphony",
  "Yet alone I sit",
  "Just left it at home",
  "It's never the same",
  "bagels in morning",
  "A morning person",
  "Eras intersect",
  "Dreaming of New York"
};

void setup(){
  size(600,600);
  background(0,0,100);
  for(int i = 0; i < frames; i++) {
    pg[i] = createGraphics(width,height);
    pg[i].beginDraw();
    pg[i].background(0,0,100);
    pg[i].stroke(255,150); //color of stroke
    pg[i].strokeWeight(5); //thickeness of stroke
    pg[i].endDraw();
  }

  noStroke();
  fill(#ffffff);
  myFont = createFont("Futura", 16, true);
  newHaiku();
}

void writeLine(String[] words) {
  int n = int(random(words.length));
  text(words[n], 300, y);
  y = y + 40;
}

void newHaiku() {
  background(0,0,100);

  y = 50;

  writeLine(firstLn);
  writeLine(secondLn);
  writeLine(thirdLn);

  textFont(myFont);
}

void draw(){  
  //background(0,0,100);

  int currentFrame = frameCount % frames; //0-19 frames
  if(mousePressed){
    pg[currentFrame].beginDraw();
    pg[currentFrame].line(mouseX,mouseY,pmouseX,pmouseY);
    pg[currentFrame].endDraw();
  }
  image(pg[currentFrame],0,0);

  if (mousePressed){
    newHaiku();
  }
}

Painting with Computer Vision

photo 2

For my final project, I knew that I wanted to incorporate everything we had learned for both the Arduino and Processing. I knew that I waned it to be playful, tactile and interactive. After a few sketches and a few suggestions from my lovely peers, I came up with a glove that can be used to digitally draw on a computer screen or projection. The glove works by sending out an infrared signal from the tip of the pointer finger, which the camera on the computer reads and converts to a colorful pen. The size of the pen is regulated by a flex sensor that runs along the bottom of the same finger. The color of the pen is regulated using a potentiometer. The code lends itself to easy changes of the pen colors, but as I have set them now, they run through red, orange, yellow, green, blue and purple, in that order. Pressing any key will clear the drawing surface. Also, to give credit where credit is due, I have to thank Imogen Heap for her inspiring musical gloves (and Jeremiah for sharing the video) and Golan Levin’s brightness tracking code, which helped me to work out a bunch of kinks and led me to abandon the color tracking that I used in the earlier versions of this glove. The song I use is The Fifth Hand by Parra for Cuva.

photo 1

Here is a video of the glove in action, I apologize for the slight lag, the pen gets a little jumpy when quicktime is also recording:

Click Me!!!

And finally, here is my code:

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

float flip;
float penSize; 
int penColor;
import processing.video.*;
import processing.serial.*;

Capture cam;
Serial flexPort;

Minim minim;
AudioPlayer sou;

void setup() {
  size(640, 480); 
  cam = new Capture(this, width, height, 30);
  cam.start(); 
  background (0);
  noStroke();
  smooth();
  printArray(Serial.list());
  String portName = Serial.list()[7];
  flexPort = new Serial(this, portName, 9600);
  flexPort.readStringUntil('\n');
  minim = new Minim(this);
  sou = minim.loadFile("The Fifth Hand.mp3");
  sou.loop();
}

void draw() {
  if (cam.available()) {
    cam.read();
    int brightestX = 0; 
    int brightestY = 0; 
    float brightestValue = 0; 
    cam.loadPixels();
    int index = 0;
    for (int y = 0; y < cam.height; y++) {
      for (int x = 0; x < cam.width; x++) {
        // Get the color stored in the pixel
        int pixelValue = cam.pixels[index];
        // Determine the brightness of the pixel
        float pixelBrightness = brightness(pixelValue);
        if (pixelBrightness > brightestValue) {
          brightestValue = pixelBrightness;
          brightestY = y;
          brightestX = x;
        }
        index++;
      }
      //mirror screen
      flip = (cam.width - brightestX);
    }
     if ((penColor >= 0) && (penColor <=170)) {
      fill (#DC143C); //Red
    }
    else if ((penColor >= 171) && (penColor <=341)) {
      fill (#FF8C00); //Orange
    }
    else if ((penColor >= 342) && (penColor <=512)) {
      fill (#FFFF66); //Yellow
    }
    else if ((penColor >= 513) && (penColor <=683)) {
      fill (#90EE90); //Green
    }
    else if ((penColor >= 684) && (penColor <=853)) {
      fill (#4682B4); //Blue
    }
    else if ((penColor >= 854) && (penColor <=1023)) {
      fill (#9400D3); //Purple
    }

    ellipse(flip, brightestY, 10+(penSize-10), 10+(penSize-10));
  }
}

void serialEvent(Serial _port) { 
  if (flexPort == null) return;
  String input = flexPort.readStringUntil('\n');
  if (input != null) {  
    String[] parts = split(input, ","); 
    String var1 = parts[0]; 
    String var2 = parts[1]; 
    penSize = int(var1);  
    penColor = int(var2);  
  }

}

void keyPressed(){
 background (0);

 }

and Arduino side:

void setup(){
  Serial.begin(9600);
  pinMode (A0, INPUT);
  pinMode (4, OUTPUT);
  pinMode (A1, INPUT);

}

void loop(){
  digitalWrite (4, HIGH);
  int flex = analogRead(A0);
  // Serial.println (flex);
  int trueFlex = map (flex, 30, 200, 100, 0);
  int twist = analogRead (A1);
  Serial.print (trueFlex); 
  Serial.print(",");
  Serial.print (twist); 
  Serial.print(",");
  Serial.print("\n");
}

 

Catch One Direction

welcome

For my final project, I went further with the One Direction game I have been working on for the past few weeks! It now has four levels, a start screen, and end screen. The game can also be restarted by pressing ‘r’ and if the user chooses to leave the game before they reach the end, they can press ‘q’ and it will bring them to the quit screen (different from the winning end screen). For music, I made a mashup of a handful of their songs. The user doesn’t have to just listen to one song! I was going to add some sort of sound effect for when the crazy fan catches one of the boys… but as the number of them grew.. I could feel my brain wanted to explode from that number of repetitive sounds.

Also, if you are curious about which songs I used for the background track:
“One Way or Another (Teenage Kicks)”
What Makes You Beautiful
Kiss You
Live While We’re Young
Best Song Ever
Happily (one of my personal favorites)

Level One:
Catch1D_L1
This is the slowest level with only one of each member. I also decided to put the instructions on this page with a transparent background with mildly opaque instructions (made in Photoshop).

Level Two:
Catch1D_L2

Level Three:
Catch1D_L3

Level Four // Bonus Level:
Catch1D_L4

Quit Screen (in case you don’t have the patience to get to the end!):

quit

 

End Screen:
end

 

 

————CODE ————

import ddf.minim.*;
import processing.serial.*;

Minim minim;
AudioPlayer player;
Serial myPort;

PImage welcomePic;
PImage endPic;
PImage logo;
PImage quit;
PImage instructions;

Member [] oneDirection = new Member[5];
Member [] levelTwo = new Member[10];
Member [] levelThree = new Member[25];
Member [] bonusLevel = new Member[50];

float xPos, yPos;
float xSpeed, ySpeed;
PImage harry, liam, louis, niall, zayn; 
PImage [] boy = {harry, liam, louis, niall, zayn };
boolean overBoy;

CrazyFan fan;
PImage crazyFan;
float fanXpos, fanYpos;
float fanXspeed, fanYSpeed;

boolean start, level_One,level_Two, level_Three, bonus_Level, endGame;
boolean quitGame;

void setup(){
  size(800,600); 

  welcomePic = loadImage("welcome.png");
  endPic = loadImage("end.png");
  logo = loadImage("logo.png");
  quit = loadImage("quit.png");
  instructions = loadImage("instructions.png");

  start=false;

  level_One=true;
  level_Two=true;
  level_Three=true;
  bonus_Level=true;

  endGame=false;
  quitGame=false;

  crazyFan = loadImage("crazyFan.png");
  harry = loadImage("Harry_Styles.png");
  liam = loadImage("Liam_Payne.png");
  louis = loadImage("Louis_Tomlinson.png");
  niall = loadImage("Niall_Horan.png");
  zayn = loadImage("Zayn_Malik.png");

  fan = new CrazyFan(crazyFan, fanXpos, fanYpos);
  oneDirection[0] = new Member(harry, xPos, yPos);
  oneDirection[1] = new Member(liam, xPos, yPos);
  oneDirection[2] = new Member(louis, xPos, yPos);
  oneDirection[3] = new Member(niall, xPos, yPos);
  oneDirection[4] = new Member(zayn, xPos, yPos);

 minim = new Minim(this);
 player = minim.loadFile("mashup.mp3");
 player.play();

  //fill levelTwo
  for(int i=0; i<2; i++){
      levelTwo[5*i] = new Member(harry, xPos, yPos);
      levelTwo[5*i+1] = new Member(liam, xPos, yPos);
      levelTwo[5*i+2] = new Member(louis, xPos, yPos);
      levelTwo[5*i+3] = new Member(niall, xPos, yPos);
      levelTwo[5*i+4] = new Member(zayn, xPos, yPos);
    }

   //fill levelThree   
   for(int i=0; i<5; i++){
      levelThree[5*i] = new Member(harry, xPos, yPos);
      levelThree[5*i+1] = new Member(liam, xPos, yPos);
      levelThree[5*i+2] = new Member(louis, xPos, yPos);
      levelThree[5*i+3] = new Member(niall, xPos, yPos);
      levelThree[5*i+4] = new Member(zayn, xPos, yPos);
    }

   //fill bonusLevel  
   for(int i=0; i<10; i++){
      bonusLevel[5*i] = new Member(harry, xPos, yPos);
      bonusLevel[5*i+1] = new Member(liam, xPos, yPos);
      bonusLevel[5*i+2] = new Member(louis, xPos, yPos);
      bonusLevel[5*i+3] = new Member(niall, xPos, yPos);
      bonusLevel[5*i+4] = new Member(zayn, xPos, yPos);
    }

}

void draw(){ 

  if(endGame==false){
     background(255);
      if(start==false){
            image(welcomePic,0,0);
          }
     image(logo,0,0);
  }  

  levelUp();

  if (endGame==true){
    background(255);
    image(endPic,0,0);
    if (quitGame==true){
      image(quit,0,0);
    }
  }

}  

//----------------LEVEL ONE----------------
void levelOneFreeze(){
  for (int i = 0; i < 5; i ++){
     if (dist(oneDirection[i].xPos, oneDirection[i].yPos, fan.fanXpos, fan.fanYpos)<= 75){
       oneDirection[i].overBoy=true;
       oneDirection[i].freezeBoy();
      }//dist
   }
}
void levelOne(){
  for (int i = 0; i < 5; i ++){
      oneDirection[i].positionChange();
      oneDirection[i].levelOneDrawLad();
  }
  fan.positionChange();
  fan.levelOneDrawFan();
}

//----------------LEVEL TWO----------------
void levelTwoFreeze(){
  for (int i = 0; i < 10; i ++){
     if (dist(levelTwo[i].xPos, levelTwo[i].yPos, fan.fanXpos, fan.fanYpos)<= 50){
       levelTwo[i].overBoy=true;
       levelTwo[i].freezeBoyTwo();
      }//dist
   }
}
void levelTwo(){
  for (int i = 0; i < 10; i ++){
      levelTwo[i].positionChangeTwo();
      levelTwo[i].levelTwoDrawLad();
  }
  fan.positionChange();
  fan.levelTwoDrawFan();
}

//----------------LEVEL THREE----------------
void levelThreeFreeze(){
  for (int i = 0; i < 25; i ++){
     if (dist(levelThree[i].xPos, levelThree[i].yPos, fan.fanXpos, fan.fanYpos)<= 40){
       levelThree[i].overBoy=true;
       levelThree[i].freezeBoyThree();
      }//dist
   }
}
void levelThree(){
  for (int i = 0; i < 25; i ++){
      levelThree[i].positionChange();
      levelThree[i].levelThreeDrawLad();
  }
  fan.positionChange();
  fan.levelThreeDrawFan();
}

//----------------BONUS LEVEL----------------
void bonusLevelFreeze(){
  for (int i = 0; i < 50; i ++){
     if (dist(bonusLevel[i].xPos, bonusLevel[i].yPos, fan.fanXpos, fan.fanYpos)<= 40){
       bonusLevel[i].overBoy=true;
       bonusLevel[i].freezeBoyBonus();
      }//dist
   }
}
void bonusLevel(){
  for (int i = 0; i < 50; i ++){
      bonusLevel[i].positionChangeBonus();
      bonusLevel[i].bonusLevelDrawLad();
  }
  fan.positionChange();
  fan.bonusLevelDrawFan();
}

//--------------------------------------------

void levelUp(){
  if(start==true){
    if (level_One){
      levelOne();
      image(instructions,0,0);
      if (oneDirection[0].overBoy==true && oneDirection[1].overBoy==true && oneDirection[2].overBoy==true && oneDirection[3].overBoy==true && oneDirection[4].overBoy==true){
        level_One = false;
      }
    }
    else if(level_Two){
      levelTwo();
      if (levelTwo[0].overBoy==true && levelTwo[1].overBoy==true && levelTwo[2].overBoy==true && levelTwo[3].overBoy==true && levelTwo[4].overBoy==true && levelTwo[5].overBoy==true && levelTwo[6].overBoy==true && levelTwo[7].overBoy==true && levelTwo[8].overBoy==true && levelTwo[9].overBoy==true){
            level_Two = false;
          }
       }
    else if(level_Three){  
       levelThree();
       if (levelThree[0].overBoy==true && levelThree[1].overBoy==true && levelThree[2].overBoy==true && levelThree[3].overBoy==true && levelThree[4].overBoy==true && levelThree[5].overBoy==true && levelThree[6].overBoy==true && levelThree[7].overBoy==true && levelThree[8].overBoy==true && levelThree[9].overBoy==true && levelThree[10].overBoy==true && levelThree[11].overBoy==true && levelThree[12].overBoy==true && levelThree[13].overBoy==true && levelThree[14].overBoy==true && levelThree[15].overBoy==true && levelThree[16].overBoy==true && levelThree[17].overBoy==true && levelThree[18].overBoy==true && levelThree[19].overBoy==true && levelThree[20].overBoy==true && levelThree[21].overBoy==true && levelThree[22].overBoy==true && levelThree[23].overBoy==true && levelThree[24].overBoy==true){
           level_Three = false;
        }
    }
   else if(bonus_Level){
      bonusLevel();
       if (bonusLevel[0].overBoy==true && bonusLevel[1].overBoy==true && bonusLevel[2].overBoy==true && bonusLevel[3].overBoy==true && bonusLevel[4].overBoy==true && bonusLevel[5].overBoy==true && bonusLevel[6].overBoy==true && bonusLevel[7].overBoy==true && bonusLevel[8].overBoy==true && bonusLevel[9].overBoy==true && bonusLevel[10].overBoy==true && bonusLevel[11].overBoy==true && bonusLevel[12].overBoy==true && bonusLevel[13].overBoy==true && bonusLevel[14].overBoy==true && bonusLevel[15].overBoy==true && bonusLevel[16].overBoy==true && bonusLevel[17].overBoy==true && bonusLevel[18].overBoy==true && bonusLevel[19].overBoy==true && bonusLevel[20].overBoy==true && bonusLevel[21].overBoy==true && bonusLevel[22].overBoy==true && bonusLevel[23].overBoy==true && bonusLevel[24].overBoy==true && bonusLevel[25].overBoy==true && bonusLevel[26].overBoy==true && bonusLevel[27].overBoy==true && bonusLevel[28].overBoy==true && bonusLevel[29].overBoy==true && bonusLevel[30].overBoy==true && bonusLevel[31].overBoy==true && bonusLevel[32].overBoy==true && bonusLevel[33].overBoy==true && bonusLevel[34].overBoy==true && bonusLevel[35].overBoy==true && bonusLevel[36].overBoy==true && bonusLevel[37].overBoy==true && bonusLevel[38].overBoy==true && bonusLevel[39].overBoy==true && bonusLevel[40].overBoy==true && bonusLevel[41].overBoy==true && bonusLevel[42].overBoy==true && bonusLevel[43].overBoy==true && bonusLevel[44].overBoy==true && bonusLevel[45].overBoy==true && bonusLevel[46].overBoy==true && bonusLevel[47].overBoy==true && bonusLevel[48].overBoy==true && bonusLevel[49].overBoy==true){
          bonus_Level = false;
          endGame = true; 
      }
    }
  }
}//end levelUp

void keyPressed(){
  if (key == 'c'){
    levelOneFreeze();
    if(level_One==false && level_Two==true && level_Three==true && bonus_Level==true){
      levelTwoFreeze();
    }
    if(level_One==false && level_Two==false && level_Three==true && bonus_Level==true){
      levelThreeFreeze();
    }
    if(level_One==false && level_Two==false && level_Three==false && bonus_Level==true){
      bonusLevelFreeze();
    }

  }
  //back to the beginning
  if (key == 'r'){   
    start=false;
    level_One=true;
    level_Two=true;
    level_Three=true;
    bonus_Level=true;
    endGame=false;
    quitGame=false;
  }
  //skip to levelTwo
  if (key == 'd'){ 
    start = true;
    level_One=false;
    level_Two=true;
    level_Three=true;
    bonus_Level=true;
    endGame=false;
  }
  //skip to levelThree
  if (key == 'f'){ 
    start = true;
    level_One=false;
    level_Two=false;
    level_Three=true;
    bonus_Level=true;
    endGame=false;
  }
  //skip to bonusLevel
  if (key == 'g'){ 
    start = true;
    level_One=false;
    level_Two=false;
    level_Three=false;
    bonus_Level=true;
    endGame=false;
  }
  if (key == 'm'){
    start = true;
  }
  if (key == 'q'){
    level_One=false;
    level_Two=false;
    level_Three=false;
    bonus_Level=false;
    endGame=true;
    quitGame=true;
  }
}

Member Class:

class Member{
    float xPos = random(0,width-75);
    float yPos = random(0,height-150);
    float xSpeed, ySpeed;
    PImage [] boy = {harry, liam, louis, niall, zayn };
    PImage lad, _lad;
    boolean overBoy;
    float _xSpeed, _ySpeed;

    //Constructor
    Member(PImage lad, float xPos, float yPos){
       xSpeed = random (3.0, 5.0);
       ySpeed = random (3.0, 5.0);
       _lad = lad;
       overBoy = false;
    }//constructor  

//----------------LEVEL ONE----------------    
    void positionChange(){
      if(xPos >= width - 75|| xPos <0){
        xSpeed *= -1;
      }
      if(yPos >= height - 150 || yPos<0){
        ySpeed *= -1;
      } 
      xPos += xSpeed;
      yPos += ySpeed;
    }
    void levelOneDrawLad(){
        xPos += xSpeed;
        yPos += ySpeed; 
        _lad.resize(0,150);
        image(_lad, xPos, yPos);
    }//draw the boys
    void freezeBoy(){
      if (overBoy){
         xSpeed = 0;
         ySpeed = 0; 
       }
     }//freezeBoy

//----------------LEVEL TWO----------------    
   void positionChangeTwo(){
      if(xPos >= width - 75|| xPos <0){
        xSpeed *= -1;
      }
      if(yPos >= height - 100 || yPos<0){
        ySpeed *= -1;
      } 
      xPos += xSpeed;
      yPos += ySpeed;
    }
    void freezeBoyTwo(){
      if (overBoy){
         xSpeed = 0;
         ySpeed = 0; 
       }
     }//freezeBoy
    void levelTwoDrawLad(){
        xPos += 1.25*xSpeed;
        yPos += 1.25*ySpeed; 
        _lad.resize(0,120);
        image(_lad, xPos, yPos);
    }//draw the boys

//----------------LEVEL THREE----------------
   void positionChangeThree(){
      if(xPos >= width - 50|| xPos <0){
        xSpeed *= -1;
      }
      if(yPos >= height - 25 || yPos<0){
        ySpeed *= -1;
      } 
      xPos += xSpeed;
      yPos += ySpeed;
    } 
    void freezeBoyThree(){
      if (overBoy){
         xSpeed = 0;
         ySpeed = 0; 
       }
     }//freezeBoy
    void levelThreeDrawLad(){
        xPos += 1.5*xSpeed;
        yPos += 1.5*ySpeed; 
        _lad.resize(0,100);
        image(_lad, xPos, yPos);
    }//draw the boys

//----------------BONUS LEVEL----------------

   void positionChangeBonus(){
      if(xPos >= width - 45 || xPos <0){
        xSpeed *= -1;
      }
      if(yPos >= height - 30 || yPos<0){
        ySpeed *= -1;
      } 
      xPos += 1.75*xSpeed;
      yPos += 1.75*ySpeed;
    }
    void freezeBoyBonus(){
      if (overBoy){
         xSpeed = 0;
         ySpeed = 0; 
       }
     }//freezeBoy
   void bonusLevelDrawLad(){
        xPos += _xSpeed;
        yPos += _ySpeed; 
        _lad.resize(0,75);
        image(_lad, xPos, yPos);
    }//draw the boys

}//member

CrazyFan Class:

class CrazyFan{
    PImage crazyFan, _crazyFan;
    float fanXpos = random(0,width-75);
    float fanYpos = random(0,height-150);
    float fanXspeed = random(3.0, 5.0);
    float fanYspeed = random(3.0, 5.0); 

//Constructor
    CrazyFan(PImage crazyFan, float fanXpos, float fanYpos){
       _crazyFan = crazyFan;
    }//constructor  

   void positionChange(){
        if(fanXpos >= width - 75|| fanXpos<0){
          fanXspeed *= -1;
        }
        if(fanYpos >= height - 140 || fanYpos<0){
          fanYspeed *= -1;
        } 

        fanXpos += fanXspeed;
        fanYpos += fanYspeed;
     }

//----------------LEVEL ONE----------------  

     void levelOneDrawFan(){
      fanXpos += xSpeed;
      fanYpos += ySpeed; 
      _crazyFan.resize(0,150);
      image(_crazyFan, fanXpos, fanYpos);
    }//draw the fan

//----------------LEVEL TWO----------------     

    void levelTwoDrawFan(){
      fanXpos += xSpeed;
      fanYpos += ySpeed; 
      _crazyFan.resize(0,140);
      image(_crazyFan, fanXpos, fanYpos);
    }//draw the fan

//----------------LEVEL TWO----------------     

    void levelThreeDrawFan(){
      fanXpos += xSpeed;
      fanYpos += ySpeed; 
      _crazyFan.resize(0,130);
      image(_crazyFan, fanXpos, fanYpos);
    }//draw the fan

//----------------LEVEL TWO----------------     

    void bonusLevelDrawFan(){
      fanXpos += xSpeed;
      fanYpos += ySpeed; 
      _crazyFan.resize(0,130);
      image(_crazyFan, fanXpos, fanYpos);
    }//draw the fan 

}

 

Final Project Progress: visualizing sound

Inspired by Alice. The process of turning sound into numbers and numbers into visuals is tough for exactly the reasons we talked about in class. Sound has many aspects to it that change from instant to instant. Fourier Tranformations? Spectrum size? Changes in waves over time? If you think I remember ANY trigonometry, you’re wrong.

Recall my dream for Amon Tobin-style crowdsourced music visuals, tersly summarized below:

badassdj

This weeks I got the basics of minim down. I am exploring use of the getBand(), noise(), and averaging sound bytes over time. The code below plays the very basic “jingle” file so we can observe how changes in the sound of the music are reflected across the soundbytes. Different axis feature different equalizer-type representations of the sound.

Notice how the equalizers tend to have a long-tail situation in terms of frequency.

Screen Shot 2014-04-21 at 12.48.59 PM

My inner economist thinks the next step is to take the (inverse?) natural log of this equalizer value and see how it looks.

import ddf.minim.analysis.*;
import ddf.minim.*;
import processing.serial.*;
import ddf.minim.ugens.*; 

float x; 
float y;
float l1; 
float l2; 
float l3; 
float l4;  
float l5;
float level; 
float average;

Minim       minim;
AudioPlayer jingle;
FFT         fft;

void setup()
{
  float y = 0;
  float x = 0;
  l1 = 0; 
  l2 = 0; 
  l3 = 0; 
  l4 = 0; 
  l5 = 0;
  size(600, 600, P3D);
  smooth();
  colorMode(RGB);

  minim = new Minim(this);

  // specify that we want the audio buffers of the AudioPlayer
  // to be 1024 samples long because our FFT needs to have 
  // a power-of-two buffer size and this is a good size.
  jingle = minim.loadFile("jingle.mp3", 1024);

  // loop the file indefinitely

  jingle.loop();

  // create an FFT object that has a time-domain buffer 
  // the same size as jingle's sample buffer
  // note that this needs to be a power of two 
  // and that it means the size of the spectrum will be half as large.
  fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
}

void draw()
{
  background(0);

  // perform a forward FFT on the samples in jingle's mix buffer,
  // which contains the mix of both the left and right channels of the file
  fft.forward( jingle.mix );

  for (int i = 0; i < fft.specSize(); i++)
  {
    // draw the line for frequency band i, scaling it up a bit so we can see it
    x+=0.02;
    y+=1;
    float n = noise(x);
    float t = noise(y);
    float p = noise(i);
    pushMatrix();
    stroke(70, 3, 11, 20);
    translate(310, -140);
    rotate(.5);
    fill (60, 170, 205, t*4);
    rect(t*(width)-50, n*height+10, 50*n, height/2*n);
    rect( n*height+10, t*(width)-50, height/2*n, 50*n);
    popMatrix();
    stroke(255);
    level = fft.getBand(i);
    l1 = level;
    //float logLevel = -1/log(level);
    print(average + " | "+ p + "\n");

    if (average <= 12) {
      stroke(50, 160, 200);
      rect(40, 30, 150, 450);
    };

    if (average <= 18 && average >= 12) {
      stroke(50, 160, 200);
      rect(60, 130, 180, 450);
    };

    if (average <= 40 && average >= 18) {
      stroke(50, 160, 200);
      rect(400, 30, 550, 450);
    };

    if (average > 40) {
      stroke(50, 160, 200);
      rect(350, 130, 120, 600);
    };

    line( i, height, i, height - level*50);
    stroke(255);
    line(i, 0, i, p*100);
    line(average, i, 0, i);
    l5 = l4;
    l4 = l3;
    l3 = l2;
    l2 = l1;
    average = 100*((l1 + l2 + l3 +l4 + l5)/5);
  };
}