2016年4月7日 星期四

WEEK07_02160136_陳威志

import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pos =null;
int gamescenes = 1;
int time = 0;
int time1 = 120;
int time2 = 120;
int button_width = 100;
int button_height = 50;
int hand_ballr = 15;
int ballr = 5;
int ball_quantity = 40;
float[] ballx = new float [ball_quantity];
float[] bally = new float [ball_quantity];
float speed = 2;
float[] speedx = new float [ball_quantity];
float[] speedy = new float [ball_quantity];
int[] xdirection = new int [ball_quantity];
int[] ydirection = new int [ball_quantity];
int redballr = 10;
float redballx;
float redbally;
float redspeedx;
float redspeedy;
int reddirectionx;
int reddirectiony;
float tempx;
float tempy;

void setup() {
  size(800, 600, P3D);
  ellipseMode(RADIUS);
  ballmake();
  redballmake();
  leap = new LeapMotion(this);
}

void draw() {
  background(73, 125, 178);
  for (Hand hand : leap.getHands ()) {
    if ( hand.isRight() ) {
      pos = hand.getIndexFinger().getPosition();
    }
  }
  if (pos!=null) {
    fill(254, 252, 150);
    noStroke();
    ellipse(pos.x, pos.y, hand_ballr, hand_ballr);
  }

  switch(gamescenes) {
  case 1:
    button_make();
    fill (0);
    textSize (34);
    textAlign(CENTER, BOTTOM);
    text ("Game Start", width/2, height/2+17);
    if (pos!=null) {
      if (pos.x<=width/2+button_width && pos.x>=width/2-button_width && pos.y<=height/2+button_height && pos.y>=height/2-button_height) {
        time1 --;
        button_click();
      } else time1 = 120;
      if (time1 == 0)
        gamescenes = 2;
    }
    break;
  case 2:
    if (pos.x>0&&pos.x<width&&pos.y>0&&pos.y<height) time++;
    score();
    redballmove();
    if (pos!=null) {
      float s = sqrt(sq(pos.x-redballx)+sq(pos.y-redbally));
      if (s < hand_ballr+redballr) {
        redballmake();
        time+=3000;
      }
    }
    for (int i = 0; i<ball_quantity; i++) {
      ballmove(i);
      if (pos!=null) {
        float z = sqrt(sq(pos.x-ballx[i])+sq(pos.y-bally[i]));
        if (z < hand_ballr+ballr) {
          for (int j = 0; j<ball_quantity; j++) {
            speedx[j] = 0;
            speedy[j] = 0;
          }
          tempx = pos.x;
          tempy = pos.y;
          gamescenes = 3;
        }
      }
    }
    break;
  case 3:
    for (int i = 0; i<ball_quantity; i++) {
      fill(0);
      noStroke();
      ellipse(ballx[i], bally[i], ballr, ballr);
    }
    fill(0, 255, 0);
    noStroke();
    ellipse(tempx, tempy, 15, 15);
    gameover();
    if (pos!=null) {
      if (pos.x<=width/2+button_width && pos.x>=width/2-button_width && pos.y<=height/2+button_height && pos.y>=height/2-button_height) {
        time1 --;
        button_click();
      } else time1 = 120;
      if (time1 == 0) {
        time = 0;
        ballmake();
        redballmake();
        gamescenes = 2;
      }

      if (pos.x<=width/2+button_width && pos.x>=width/2-button_width && pos.y<=height/2+button_height+120 && pos.y>=height/2-button_height+120) {
        time2 --;
        fill(255, 255, 255, 50);
        stroke(0);
        ellipse(width/2, height/2+120, button_width, button_height);
      } else time2 = 120;
      if (time2 == 0) {
        exit();
      }
    }
    break;
  }
}

void ballmake() {
  for (int i=0; i<ball_quantity; i++) {
    ballx[i] = random(ballr, width-ballr);
    bally[i] = random(ballr, height-ballr);
    while (ballx[i]<=width/2+button_width && ballx[i]>=width/2-button_width && bally[i]<=height/2+button_height && bally[i]>=height/2-button_height) {
      ballx[i] = random(ballr, width-ballr);
      bally[i] = random(ballr, height-ballr);
    }
    speedx[i] = random(-speed, speed);
    speedy[i] = random(-speed, speed);
    xdirection[i] = int(random(1));
    if (xdirection[i]==0)xdirection[i] = -1;
    ydirection[i] = int(random(1));
    if (ydirection[i]==0)ydirection[i] = -1;
  }
}

void ballmove(int i) {
  fill(0);
  noStroke();
  ellipse(ballx[i], bally[i], ballr, ballr);
  ballx[i] += speedx[i] * xdirection[i] ;
  bally[i] += speedy[i] * ydirection[i] ;

  if (ballx[i] > width-ballr || ballx[i] < ballr) {
    xdirection[i] *= -1;
  }
  if (bally[i] > height-ballr || bally[i] < ballr) {
    ydirection[i] *= -1;
  }
}

void gameover() {
  fill (255);
  textSize (66);
  textAlign(CENTER, BOTTOM);
  text ("Game Over", width/2, height/2-120);
  
  textSize (33);
  text ("Your Score:"+time/6, width/2, height/2-70);

  button_make();
  fill (0);
  textSize (34);
  textAlign(CENTER, BOTTOM);
  text ("Restart", width/2, height/2+17);

  fill(182, 130, 77);
  stroke(0);
  ellipse(width/2, height/2+120, button_width, button_height);
  fill (0);
  textSize (34);
  textAlign(CENTER, BOTTOM);
  text ("Exit", width/2, height/2+137);
}

void button_make() {
  fill(182, 130, 77);
  stroke(0);
  ellipse(width/2, height/2, button_width, button_height);
}

void button_click() {
  fill(255, 255, 255, 50);
  stroke(0);
  ellipse(width/2, height/2, button_width, button_height);
}

void redballmake() {
  redballx = random(redballr, width-redballr);
  redbally = random(redballr, height-redballr);
  redspeedx = random(3, 6);
  redspeedy = random(3, 6);
  reddirectionx = int(random(1));
  if (reddirectionx==0)reddirectionx = -1;
  reddirectiony = int(random(1));
  if (reddirectiony==0)reddirectiony = -1;
}

void redballmove() {
  fill(255, 0, 0);
  noStroke();
  ellipse(redballx, redbally, redballr, redballr);
  redballx += redspeedx * reddirectionx ;
  redbally += redspeedy * reddirectiony ;
  if (redballx > width-redballr || redballx < redballr) {
    reddirectionx *= -1;
  }
  if (redbally > height-redballr || redbally < redballr) {
    reddirectiony *= -1;
  }
}

void score() {
  fill (255);
  textSize (18);
  textAlign(LEFT, BOTTOM);
  text ("Score:"+time/6, 5, 20);
}

沒有留言:

張貼留言