2016年4月15日 星期五

Week08其中作業Demo

Week08 其中作業Demo
本作業以疊疊樂為基本概念,並加入Leap Motion的手部互動遊戲,遊戲方式為偵測食指,若要放下方塊則伸出食指而縮起食指則為暫停放置方塊,分數會跟著方塊的數量累加,若方塊沒疊到位子上則結束遊戲!
遊戲程式碼:
import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector myFinger1 = new PVector();
PVector myHand = new PVector();
float dis=0;
PVector []pos = new PVector[1000];
int now = 1;
int frame=1;
boolean bStartDown=false, bStartNext=false, bGameOver=false;
float down =0 ;
void setup() {
  size(600, 400, P3D);
  for (int i=0; i<1000; i++)pos[i]=new PVector(0, 50-i*50, 0);
  leap = new LeapMotion(this);
}
void draw() {

  int fps = leap.getFrameRate();
  for (Hand hand : leap.getHands ()) {
    myHand=hand.getPosition();
 
 
    for (Finger finger : hand.getFingers ()) {
      switch(finger.getType()) {
      case 0:
        // System.out.println("thumb");
        break;
      case 1:
        myFinger1 = finger.getPosition();
        // System.out.println("index");
        break;
      case 2:
        // System.out.println("middle");
        break;
      case 3:
        // System.out.println("ring");
        break;
      case 4:
        // System.out.println("pinky");
        break;
      }
    }
    dis=dist(myHand.x, myHand.y, myFinger1.x, myFinger1.y);
    println(dis);
  }
  if (bGameOver)background(255, 0, 0, 100);
  else background(0,0,0);
   translate(width/2, height/2);
  translate(0, down);
  for (int i=0; i<=now; i++) {
    pushMatrix();
    translate(pos[i].x, pos[i].y);
    box(50, 50, 50);
    popMatrix();
  }
  if (bStartNext) {
    for (int i=0; i<=now; i++)
    {
      pos[i].y+=50;
    }
 
    bStartNext=false;
 
  }
  if (bStartDown) {
    pos[now-1].y+=2;
 
    if(pos[now-1].y>0){
      bStartNext=true;
      bStartDown=false;  
    }
  }
  //
  if (dis>60/*pos[now].y>0 */&& frame%60==1) {
    bStartDown=true;
    now++;
 
 
    if (abs(pos[now].x-pos[now-1].x)>50)bGameOver=true;
  } else {
    pos[now].x=myFinger1.x-width/2;
    pos[now].y=myFinger1.y-height/2;
    println(pos[now].x + "   " +pos[now].y);
  }
 if(bGameOver!=true)
 { textSize(32);
   text("Score:" + now,100,100); }
  else {background(255, 0, 0, 100);
    fill(0,0,0);
    textSize(50);
    text("g a m e  o v e r",-200,0);}

 frame++;
}



void mousePressed() {
  bStartDown=true;
}

沒有留言:

張貼留言