2015年4月27日 星期一

期中作業:手指跳舞機

影片位置:(建議將喇叭音量調小再點開)
https://www.youtube.com/watch?v=m1-bHjUTskg


簡介:
設計理念是會有音樂播放,然後會有箭頭落下。依落下的方向用手揮動來代表上下左右。




程式:
//import de.voidplus.leapmotion.*;
import de.voidplus.leapmotion.*;
//LeapMotion leap;
import ddf.minim.*;
PImage img, img1, img2,img3,img4, down, left, right, fly;
PImage img7;
PImage img10;
PImage img11;
PImage img12;
PImage img13;
PImage img14;
PImage img15;
PImage img16;
PImage img17;
PImage img18;
PImage img19;
LeapMotion leap;
AudioPlayer player;
Minim minim;
int [] ran=new int[50];
float [] pointY=new float[50];
//int [] pointY=new int[50];
int h1, t1, t2, start, control, score, health, ground, time;
float speed, centerx, centery;
int n1, n2, n3, n4, n5, nu1, nu2, nu3, nu4, nu5, keys, cx, cy,count;

void setup() {
  size(800, 600, P3D);
  for (int i=0; i<50; i++) {
    pointY[i]=-1;
    ran[i]=0;
  }
  img = loadImage("floor.jpg");
  fly = loadImage("fly.png");
  img1 = loadImage("tree.jpg");
  img2 = loadImage("floor1.jpg");
  img3 = loadImage("floor2.jpg");
  img4 = loadImage("floor3.jpg");
  down = loadImage("down.png");
  left = loadImage("left.png");
  right = loadImage("right.png");
  img7 = loadImage("protect.png");
  img10 = loadImage("0.png");
  img11 = loadImage("1.png");
  img12 = loadImage("2.png");
  img13 = loadImage("3.png");
  img14 = loadImage("4.png");
  img15 = loadImage("5.png");
  img16 = loadImage("6.png");
  img17 = loadImage("7.png");
  img18 = loadImage("8.png");
  img19 = loadImage("9.png");
  minim = new Minim(this);
  player = minim.loadFile("Lala.mp3", 2048);
  player.play();
  t1=0;
  t2=0;
  control=0;
  start=0;
  score=0;
  health=10;
  h1=0;
  n1=0;
  n2=0;
  n3=0;
  n4=0;
  n5=0;
  nu1=10;
  nu2=100;
  nu3=1000;
  nu4=10000;
  nu5=100000;
  keys=0;
  ground=0;
  time=1;
  speed=1.5;
  leap = new LeapMotion(this);
  leap = new LeapMotion(this).withGestures();
  centerx=400;
  centery=300;
  count=0;
}

void draw() {
  if (health==0)ground=1;
 
  if (ground!=0)background(img2);
  //image(img1, 0, 510, 800, 100);
  if (ground==0) {


    int fps = leap.getFrameRate();
    for (Hand hand : leap.getHands ()) {
      //hand.draw();
      int     hand_id          = hand.getId();
      PVector hand_position    = hand.getPosition();

      int n=hand.countFingers();

      if (keys==0)
        if (hand_position.x>=300&&hand_position.x<=500&&hand_position.y>=225&&hand_position.y<=375)
        {
          keys=1;
        }
      if (hand_position.x>=300&&hand_position.x<=500&&hand_position.y<=375)
      {
        centerx=hand_position.x;
        centery=hand_position.y;
        image(fly, centerx, centery, 60, 60);
        background(img3);
        } else background(img4);
        if (keys==1)
        {
          if (hand_position.y>=375) {
            control=1;
            keys=0;
          }
          if (hand_position.x<=300) {
            control=2;
            keys=0;
          }
          if (hand_position.x>=500) {
            control=3;
            keys=0;
          }
        }
      println("x:" + hand_position.x + " Y:" + hand_position.y + "   +   " + control);
    }
    for (int i=1; i<=health; i++) {
      image(img7, 790-i*50, 10, 50, 50);
    }

    if (control>=1&&control<=3) {
      for (int i=0; i<50; i++)
      {
        if (control==1)
          if (pointY[i]>=470&&pointY[i]<=570&&ran[i]==1) {
            ran[i]=0;
            score+=speed*10;
            h1=1;
            control=0;
          }
        if (control==2)
          if (pointY[i]>=470&&pointY[i]<=570&&ran[i]==2) {
            ran[i]=0;
            score+=speed*10;
            h1=1;
            control=0;
          }
        if (control==3)
          if (pointY[i]>=470&&pointY[i]<=570&&ran[i]==3) {
            ran[i]=0;
            score+=speed*10;
            h1=1;
            control=0;
          }
      }

      if (h1==0)health--;
      control=0;
      h1=0;
    }


    t1=0;
    for (int i=0; i<50; i++)
      if (pointY[i]<=100&&pointY[i]>=0)t1=1;

    if (t1==1)t2=1;
    else t2=0;

    if (t2==0)
    {
      if(start==50)start=0;
      ran[start]=int(random(1, 5));
      pointY[start]=0;
      start++;
    }
    for (int i=0; i<50; i++)
    {
      if (ran[i]==1)image(down, 350, pointY[i], 80, 80);
      if (ran[i]==2)image(left, 200, pointY[i], 80, 80);
      if (ran[i]==3)image(right, 500, pointY[i], 80, 80);
    }

    for (int i=0; i<50; i++)
      if (ran[i]>=1&&ran[i]<=5)pointY[i]+=speed;
  }
if(score>=9999)background(img);

  for (int i=0; i<50; i++)
    if (ran[i]>=1&&ran[i]<=3&&pointY[i]==600) {
      ran[i]=0;
      health--;
    }
  n1=score%nu1;
  n2=score%nu2/10;
  n3=score%nu3/100;
  n4=score%nu4/1000;

  if (n1==0)image(img10, 140, 10, 40, 40);
  if (n1==1)image(img11, 140, 10, 40, 40);
  if (n1==2)image(img12, 140, 10, 40, 40);
  if (n1==3)image(img13, 140, 10, 40, 40);
  if (n1==4)image(img14, 140, 10, 40, 40);
  if (n1==5)image(img15, 140, 10, 40, 40);
  if (n1==6)image(img16, 140, 10, 40, 40);
  if (n1==7)image(img17, 140, 10, 40, 40);
  if (n1==8)image(img18, 140, 10, 40, 40);
  if (n1==9)image(img19, 140, 10, 40, 40);


  if (n2==0)image(img10, 100, 10, 40, 40);
  if (n2==1)image(img11, 100, 10, 40, 40);
  if (n2==2)image(img12, 100, 10, 40, 40);
  if (n2==3)image(img13, 100, 10, 40, 40);
  if (n2==4)image(img14, 100, 10, 40, 40);
  if (n2==5)image(img15, 100, 10, 40, 40);
  if (n2==6)image(img16, 100, 10, 40, 40);
  if (n2==7)image(img17, 100, 10, 40, 40);
  if (n2==8)image(img18, 100, 10, 40, 40);
  if (n2==9)image(img19, 100, 10, 40, 40);



  if (n3==0)image(img10, 60, 10, 40, 40);
  if (n3==1)image(img11, 60, 10, 40, 40);
  if (n3==2)image(img12, 60, 10, 40, 40);
  if (n3==3)image(img13, 60, 10, 40, 40);
  if (n3==4)image(img14, 60, 10, 40, 40);
  if (n3==5)image(img15, 60, 10, 40, 40);
  if (n3==6)image(img16, 60, 10, 40, 40);
  if (n3==7)image(img17, 60, 10, 40, 40);
  if (n3==8)image(img18, 60, 10, 40, 40);
  if (n3==9)image(img19, 60, 10, 40, 40);


  if (n4==0)image(img10, 20, 10, 40, 40);
  if (n4==1)image(img11, 20, 10, 40, 40);
  if (n4==2)image(img12, 20, 10, 40, 40);
  if (n4==3)image(img13, 20, 10, 40, 40);
  if (n4==4)image(img14, 20, 10, 40, 40);
  if (n4==5)image(img15, 20, 10, 40, 40);
  if (n4==6)image(img16, 20, 10, 40, 40);
  if (n4==7)image(img17, 20, 10, 40, 40);
  if (n4==8)image(img18, 20, 10, 40, 40);
  if (n4==9)image(img19, 20, 10, 40, 40);
  //println(speed);
  time++;
  if (time%500==0)speed+=0.5;
  //println("x:" + hand_position.x + " Y:" + hand_position.y);
}
//490 560

//void keyPressed() {
/*if (key=='s') control=1;
 if (key=='a') control=2;
 if (key=='d') control=3;
 */
// if (key=='s') keys=0;
// else keys=1;
//}

/*
void leapOnSwipeGesture(SwipeGesture g, int state) {
 int     id               = g.getId();
 Finger  finger           = g.getFinger();
 PVector position         = g.getPosition();
 PVector position_start   = g.getStartPosition();
 PVector direction        = g.getDirection();
 float   aspeed            = g.getSpeed();
 long    duration         = g.getDuration();
 float   duration_seconds = g.getDurationInSeconds();


 switch(state) {
 case 1:  // Start
 break;
 case 2: // Update
 break;
 case 3: // Stop
 println("SwipeGesture: "+id);
 // if (keys==0){  
 if (position.y-position_start.y<position.x-position_start.x) {  
 if (position.x>position_start.x) {
 control = 3;
 println("right");
 } else if (position.x<position_start.x)
 {
 control = 1;
 println("left");
 }
 } else if (position.y>position_start.y) {
 control = 2;
 println("down");
 }
 //  }
 }
 }
 */

沒有留言:

張貼留言