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.

Leave a Reply