2016年6月2日 星期四

WEEK15_期末進度














import SimpleOpenNI.*;
SimpleOpenNI openni;
PImage imgBG, imgHuman, imgMS, imgMS2, imgwa;
float count = 0;
int h=0, score=0, iGood=0, j=0;
void setup() {
  size(800, 600);
  openni = new SimpleOpenNI(this);
  openni.enableRGB();
  openni.enableDepth();
  openni.enableUser();
  imgBG = loadImage("bg.jpg");
  imgMS = loadImage("mouse.png");
  imgMS.resize(150, 150);
  imgMS2 = loadImage("mouse2.png");
  imgMS2.resize(150, 150);
  imgwa = loadImage("11.png");
  //imgwa.resize(800, 600);
  imgHuman = createImage(640, 480, ARGB);
}

void draw() {
  openni.update();
  background(255);
  image(imgBG, 0, 0, 800, 600);
  copyImage();
  int[]userList = openni.getUsers();
  PVector pos = new PVector(0, 0, 0), hand1 = new PVector(0, 0, 0), hand2 = new PVector(0, 0, 0), head = new PVector(0, 0, 0);
  for (int userID : userList) {
    openni.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_HEAD, pos);
    openni.convertRealWorldToProjective(pos, pos);
    fill(0, 0, 255);
    ellipse(pos.x*1.25, pos.y-20, 50, 50);
    head.set(pos);
    head.z=0;
    fill(0, 0, 255);
    ellipse(pos.x/10, pos.y/10, 5, 5);
    openni.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_HAND, pos);
    openni.convertRealWorldToProjective(pos, pos);
    fill(0, 0, 255);
    ellipse(pos.x*1.25, pos.y*1.25, 50, 50);
    hand1.set(pos);
    hand1.z=0;
    fill(0, 0, 255);
    ellipse(pos.x/10, pos.y/10, 5, 5);
    openni.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_HAND, pos);
    openni.convertRealWorldToProjective(pos, pos);
    fill(255, 0, 0);
    ellipse(pos.x/10, pos.y/10, 5, 5);
    fill(255, 0, 0);
    ellipse(pos.x*1.25, pos.y*1.25, 50, 50);
    hand2.set(pos);
    hand2.z=0;
    //openni.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_HEAD, pos);
    //openni.convertRealWorldToProjective(pos, pos);
    //fill(255,255,0);
    //ellipse(pos.x, pos.y, 50, 50);
  }
  image(imgHuman, 0, 0, 800, 600);
  /*PVector mouse1 = new PVector(mouseX, mouseY, 0);
   if (PVector.dist(mouse1, hand1)<100 || PVector.dist(mouse1, hand2)<100) {
   ellipse(mouseX, mouseY, 200, 200);
   //fill(255,255,0);
   }*/



  if (h==1)
    image(imgMS, 50, 80);
  if (h==2)
    image(imgMS, 50, 300);
  if (h==3)
    image(imgMS2, 530, 80);
  if (h==4)
    image(imgMS2, 530, 300);
  PVector mouse1 = new PVector(50, 80, 0);
  PVector mouse2 = new PVector(50, 300, 0);
  PVector mouse3 = new PVector(530, 80, 0);
  PVector mouse4 = new PVector(530, 300, 0);

  if (count%1500<15)
    println(score);
 
  for (int i=1; i<=4; i++) {
    if (j==0) {
      h = ((int)random(1, 4));
      j=50;
    }
  }
  if (h==1&&(PVector.dist(mouse1, hand1)<100 || PVector.dist(mouse1, hand2)<100)) {
    score+=1;
    j=0;
  }
  if (h==2&&(PVector.dist(mouse2, hand1)<100 || PVector.dist(mouse2, hand2)<100)) {
    score+=1;
    j=0;
  }
  if (h==3&&(PVector.dist(mouse3, hand1)<100 || PVector.dist(mouse3, hand2)<100)) {
    score+=1;
    j=0;
  }
  if (h==4&&(PVector.dist(mouse4, hand1)<100 || PVector.dist(mouse4, hand2)<100)) {
    score+=1;
    j=0;
  }

  /*if (h==1 && (pos.x*0.78+150>=50 && pos.x*0.78+150<=200) && (pos.y*1.04+50>=80 && pos.y*1.04+50<=230))
   score++;
   if (h==2 && (pos.x>=50 && pos.x<=200) && (pos.y>=300 && pos.y<=450))
   score++;
   if (h==3 && (pos.x>=530 && pos.x<=680) && (pos.y>=80 && pos.y<=230))
   score++;
   if (h==4 && (pos.x>=530 && pos.x<=680) && (pos.y>=300 && pos.y<=450))
   score++;*/

  image(imgwa, 0, 0, 1060, 600);
  image(openni.userImage(), 0, 300, 80, 60);
  textSize(30);
  text("score:"+score, 325, 450);
}
void copyImage() {
  PImage now = openni.userImage();
  now.loadPixels();
  for (int i=0; i<640*480; i++) {
    if (now.pixels[i]==color(255, 0, 0) ||
      now.pixels[i]==color(0, 255, 0) ||
      now.pixels[i]==color(0, 0, 255) ||
      now.pixels[i]==color(255, 255, 0)) {
      imgHuman.pixels[i]=color(255, 0, 0, 120);
      //println(i);
    } else {
      imgHuman.pixels[i]=color(0, 0);
    }
    imgHuman.updatePixels();
  }
}
void onNewUser(SimpleOpenNI curContext, int userId) {
  openni.startTrackingSkeleton(userId);
}

沒有留言:

張貼留言