Kinect Game

This is a two person Pong game run by Kinect and Processing. Kinect works as a motion sensor, sensing where the players' hands are. A...


This is a two person Pong game run by Kinect and Processing. Kinect works as a motion sensor, sensing where the players' hands are. And Processing runs the game.
Kinect detects where the player's hand and let players control the paddle to bounce a small ball.



// import the fingertracker library
// and the SimpleOpenNI library for Kinect access
// and bc music 
import fingertracker.*;
import SimpleOpenNI.*;
import ddf.minim.*;

// declare FignerTracker and SimpleOpenNI objects
FingerTracker fingers;
SimpleOpenNI kinect;

// declare music player
AudioPlayer backgroundPlayer, bloopPlayer = null;
Minim minim;//audio context

// set a default threshold distance:
// 625 corresponds to about 2-3 feet from the Kinect
int threshold = 625;

ArrayList  dpositionsl = new ArrayList ();
ArrayList  dpositionsr = new ArrayList ();

int[] depthMap = null;
int[] objectMap = null;


/////////////////////////////////////////////////////
float x = 300;
float y = 300;
float dx = 3;
float dy = -5;
int t;
int count=0;
int points=0;
float heightbounce=600;
// beginners level
boolean[][] inside=new boolean[1][11];
PImage img = null;

int mouseXr;
int mouseXl;
int mouseYl;
int mouseYr;

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

  frameRate(60);
  PFont font;
  font = loadFont("AgencyFB-Reg-48.vlw"); 
  textFont(font);

  // initialize your SimpleOpenNI object
  // and set it up to access the depth image
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
  // mirror the depth image so that it is more natural
  kinect.setMirror(true);
  
  minim = new Minim(this);
  startbc();
}

void draw() {
  println("draw");
  // get new depth data from the kinect
  kinect.update();
  println("kinect updated");
  // get a depth image and display it
  PImage depthImage = kinect.depthImage();
  //  image(depthImage, 0, 0);

  println("kinect got depthimage");

  // access the "depth map" from the Kinect
  // this is an array of ints with the full-resolution
  // depth data (i.e. 500-2047 instead of 0-255)
  // pass that data to our FingerTracker
  depthMap = kinect.depthMap();

  println("got depthmap");
  objectMap = new int[depthMap.length];
  //---------------------------------------------
  for (int i=0; i400 && depthMap[i]<700) {
      //      println("in range!");
      objectMap[i]=1;
    } else {
      objectMap[i]=0;
    }
  }
  
  img = createImage(kinect.depthWidth(), objectMap.length / kinect.depthWidth(), RGB);
  img.loadPixels();
  for (int i = 0; i < objectMap.length; i++) {
    if(objectMap[i]==1){
      img.pixels[i] = color(0); 
    }else{
      img.pixels[i] = color(255);
    }
  }
  
  //println("small=", small, "large=", large);
  checkObject();
  
  game();
  
  img.updatePixels();
  image(img, 0, height-100, 200, 200);
}

//sound 
void stop()
{
  backgroundPlayer.close();
  minim.stop();
  super.stop();
}

void startbc(){
  backgroundPlayer = minim.loadFile("bc.mp3",2048);
  backgroundPlayer.play();
}

void checkObject() {
  
 
  int totalXl = 0; 
  int numXl = 0;
  int totalYl = 0; 
  int numYl = 0;
  
  int totalXr = 0; 
  int numXr = 0;
  int totalYr = 0; 
  int numYr = 0;
  
  int mapWidth = kinect.depthWidth();
  int mapHeight = objectMap.length/kinect.depthWidth();
  for (int x=0; x 0) {
    Drawposition lastl = dpositionsl.get(dpositionsl.size()-1);
    mouseXl = int(lastl.x);
    mouseYl = int(lastl.y);
    mousePressed = kPressed(int(lastl.x), int(lastl.y));
  }
  
  //right
  if (numXr!=0) {
    int averageXr = totalXr/numXr;
    int averageYr = totalYr/numYr;
    dpositionsr.add(new Drawposition(averageXr, averageYr));
    //    fingerLine();
    //    println("average position", averageX, averageY);
  }
  if (dpositionsr.size() > 0) {
    Drawposition lastr = dpositionsr.get(dpositionsr.size()-1);
    mouseXr = int(lastr.x);
    mouseYr = int(lastr.y);
  }
}
///////////////////////////////////////////
void game() {
  // t=0 to start new level
  if (t==0) {
    background(255);
    stroke(12);

    // Display Level the first 2 seconds
    if (count<120) {
      fill(12);
      textAlign(CENTER);
      text("LEVEL " + inside.length, 300, 500);
    }

    //draw moving point
    strokeWeight(10);
    point(x, y);
    strokeWeight(1);

    randomSeed(1);
    // box to be destroied
    for (int j=0; j=25. && inside[j][k]==false) {
          if(k%3 == 1){
            fill(83,28,112);
          }else{  
          //fill(25,255-k*10,200,150);
            fill(12);
          }
          ellipse(50+50*k, 50+50*j, 50, 50);
        }
        // check if moving point are inside circle and from which direction it comes 
        else {
          if(inside[j][k]==false){
            if (k%3==1){
              dx *= 2;
              dy *= 2;
            } else {
              dx = 3;
              dy = -5;
            }
            if (bloopPlayer != null) {
              bloopPlayer.close();
            }
            bloopPlayer = minim.loadFile("filepz.wav", 2048);
            bloopPlayer.play();
          }

          if (dx2<=(dis2*cos(PI/4))&& dy2<=-(dis2*sin(PI/4)) && inside[j][k]==false) {
            dy*=-1;
            inside[j][k]=true;
            points++;
          } else if (dx2<=(dis2*cos(PI/4)) && dy2>=(dis2*sin(PI/4)) && inside[j][k]==false) {
            dy*=-1;
            inside[j][k]=true;
            points++;
          } else if (dy2<=(dis2*sin(PI/4)) && dx2<=-(dis2*cos(PI/4)) && inside[j][k]==false) {
            dx*=-1;
            inside[j][k]=true;
            points++;
          } else if (dy2<=(dis2*sin(PI/4)) && dx2>=(dis2*cos(PI/4)) && inside[j][k]==false) {
            dx*=-1;
            inside[j][k]=true;            
            points++;
          }
        }
      }
   }
  
    //Bar1 to bounce ball on
      float barsize=75.;
      fill(12);
      strokeWeight(3);
      int heightbouncel=mouseYl;
      int heightbouncer=mouseYr;
      
      if (hPress(mouseXl, mouseYl)) {
        line(mouseXl-barsize, heightbouncel, mouseXl+barsize, heightbouncel);

      }   
      if (hPress(mouseXr, mouseYr)) {
        line(mouseXr-barsize, heightbouncer, mouseXr+barsize, heightbouncer);
      }      
         // change direction if moving point hits boundaries
      if (x>=width || x<=0) {
        dx*=-1;
      }
      if (y<=0) {
        dy*=-1;
      }
      
    // bounce ball at y position of mouse
      if(x > 0 && x < 300){
        // change direction if moving point hits bar depending on where the point came from
        if (y>=heightbouncel && y(mouseXl-barsize) && x< (mouseXl-(1./3)*barsize) && dx>0 && dy>0) {
            dy*=-0.95;
            dx*=-1.05;
          }
          //left part - right left - get more horisontal
          else if (x>(mouseXl-barsize) && x< (mouseXl-(1./3)*barsize) && dx<0 && dy>0) {
            dy*=-0.95;
            dx*=1.05;
          }
          //right part - left right - get more horisontal
          else if (x>(mouseXl+(1./3)*barsize) && x< (mouseXl+barsize) && dx>0 && dy>0) {
            dy*=-0.95;
            dx*=1.05;
          }
          //right part - right right - get more horisontal
          else if (x>(mouseXl+(1./3)*barsize) && x< (mouseXl+barsize) && dx<0 && dy>0) {
            dy*=-0.95;
            dx*=-1.05;
          }
          //middle part - just bounce - get more vertical
          else if (x>(mouseXl-(1./3)*barsize) && x< (mouseXl+(1./3)*barsize) && dy>0) {
            dy*=-1.05;
            dx*=0.95;
          }
        }
      }else{
  
          // change direction if moving point hits bar depending on where the point came from
        if (y>=heightbouncer && y(mouseXr-barsize) && x< (mouseXr-(1./3)*barsize) && dx>0 && dy>0) {
            dy*=-0.95;
            dx*=-1.05;
          }
          //left part - right left - get more horisontal
          else if (x>(mouseXr-barsize) && x< (mouseXr-(1./3)*barsize) && dx<0 && dy>0) {
            dy*=-0.95;
            dx*=1.05;
          }
          //right part - left right - get more horisontal
          else if (x>(mouseXr+(1./3)*barsize) && x< (mouseXr+barsize) && dx>0 && dy>0) {
            dy*=-0.95;
            dx*=1.05;
          }
          //right part - right right - get more horisontal
          else if (x>(mouseXr+(1./3)*barsize) && x< (mouseXr+barsize) && dx<0 && dy>0) {
            dy*=-0.95;
            dx*=-1.05;
          }
          //middle part - just bounce - get more vertical
          else if (x>(mouseXr-(1./3)*barsize) && x< (mouseXr+(1./3)*barsize) && dy>0) {
            dy*=-1.05;
            dx*=0.95;
          }
        }
    }
      


    //Print points
    textAlign(RIGHT);
    fill(12);
    text(points, 550, 550);

    //GAME OVER
    if (y>height) {
      t=1;
      fill(12);
      textAlign(CENTER);
      text("GAME OVER", 300, 300);
      text("CLICK TO RESTART GAME", 300, 400);
      text("PRESS SPACE TO RESTART LEVEL", 300, 500);
      minim.stop();
    }

    //Finished
    if (checkinside()) {
      t=2;
      fill(12);
      textAlign(CENTER);
      text("FINISHED", 300, 300);
      text("CLICK FOR NEXT LEVEL", 300, 400);
    }

    // incrementing moving point
    x=x+dx;
    y=y+dy;

    count++;
  }
  // end of t=0

  // if game over
  else if (t==1) {

    x = 300;
    y = 300;
    dx = 3;
    dy = -5;
    count=0;
    points=0;
   

    if (mousePressed==true) {
      t=0;
      inside=new boolean[inside.length][inside[0].length];  
      startbc();
    }
    if (keyPressed) {
      if (key == ' ') {
        t=0;
        inside=new boolean[1][11];
        startbc();
      }
    }
  }

  // if level compleated
  else {
    x = 300;
    y = 300;
    dx = 3;
    dy = -5;
    count=0;
    if (mousePressed==true) {
      t=0;
      inside=new boolean[inside.length+1][inside[0].length];
    }
  }
}

//_________________________
//void fingerLine() {
//
//  for (int i=0; i0){
//      Drawposition curr = dpositions.get(i);
//      println("here is x and y", curr.x, curr.y);
//      Drawposition prev = dpositions.get(i-1);       //we can access the previous
//      stroke(0);
//      strokeWeight(10);
//      fill(80, 90, 200);
//      line(curr.x, curr.y, prev.x, prev.y);  
//      ellipse(curr.x, curr.y, 30, 30);       //draw the line
//  
//      println("x=", curr.x, "y=", curr.y);  
//    }
//  }
//}


/////////////////////////////////
boolean checkinside() {
  for (int j=0; j440) {
    return true;
  } else {
    return false;
  }
}

  
class Drawposition{
  float x, y, r;
  color c;
  
  void copy(Drawposition copyFrom){
    x = copyFrom.x;
    y = copyFrom.y;
    r= 80;
    c = copyFrom.c;
  }
  
  Drawposition(float ax, float ay){
    x = ax;
    y = ay;
    c = color(255,255,255);
  }
  
  void display(){
    fill(c, 100);
    ellipse(x,y,r,r);
  }
}

    

You Might Also Like

0 评论

Flickr Images