2016年11月10日 星期四

week09 張曉瑀上課筆記

161110  week09


今日實作 processing


檔案讀取save
step1按任意鍵就可以將所畫的存檔,但此情況每次都會覆蓋掉新的檔案



step2 將每次新畫的圖都另存一個新檔案
** 所有變數設定前都要宣告 > int















***想了解save還有什麼功能可以使用,會出現save的各種功能










step4


void setup(){
  size(600,600);
}
void draw(){
  if(mousePressed) line(mouseX, mouseY, pmouseX, pmouseY);
}
void keyPressed(){
  saveFrame();   //加入safeFrame功能就可以直接儲存檔案
}


step5  save成文字













step6 接金幣遊戲(計分)















step7 加入生命機制(扣分)














step8 設定遊戲的機制

PVector [] coins=new PVector[10];
void setup() {
  size(500, 500);
  for (int i=0; i<10; i++) coins[i]=new PVector( random(500), -random(100));
}
int score=0, life=10, speed=2;
int state=0;   //state 0:playing, 1:gameOver, 2:showScoreBoard
String [] allScore;
void draw() {
  if (state==2) {
    background(255, 255, 0);
    for (int i=0; i<allScore.length; i++) {
      fill(255, 0, 0);
      textSize(30);
      text( allScore[i], 100, 200+i*50);
    }
    return ;
  } else if (state==1) {
    background(128);
    speed=0;
    allScore = loadStrings("allScores.txt");
    if (mousePressed) state=2;
  } else background(255);
  for (int i=0; i<10; i++) {
    fill(255, 255, 0);
    ellipse(coins[i].x, coins[i].y, 30, 30);
    coins[i].y+=speed;//!!!!!!
    if (coins[i].y>530) {
      coins[i].y=-random(100);
      life--;
      if (life<=0) state=1;
    }
    if (dist(mouseX, mouseY, coins[i].x, coins[i].y)<30) {
      score+=100;
      coins[i].y=-random(100);
    }
  }
  fill(255, 0, 0);
  textSize(30);
  text("Score:"+score +"Life:"+life, 200, 200);
}

step9 連結leapmotion 利用leapmotion功能接金幣

import de.voidplus.leapmotion.*;
LeapMotion leap;
float handX=0, handY=0;
PVector [] coins=new PVector[10];
void setup() {
  size(500, 500);
  leap = new LeapMotion(this);
  for (int i=0; i<10; i++) coins[i]=new PVector( random(500), -random(100));
}
int score=0, life=10, speed=2;
int state=0;//state 0:playing, 1:gameOver, 2:showScoreBoard
String [] allScore;
void draw() {
  for (Hand hand : leap.getHands()) {
    handX=hand.getPosition().x;
    handY=hand.getPosition().y;
  }
  if (state==2) {
    background(255, 255, 0);
    for (int i=0; i<allScore.length; i++) {
      fill(255, 0, 0);
      textSize(30);
      text( allScore[i], 100, 200+i*50);
    }
    return ;
  } else if (state==1) {
    background(128);
    speed=0;
    allScore = loadStrings("allScores.txt");
    if (mousePressed) state=2;
  } else background(255);
  for (int i=0; i<10; i++) {
    fill(255, 255, 0);
    ellipse(coins[i].x, coins[i].y, 30, 30);
    coins[i].y+=speed;//!!!!!!
    if (coins[i].y>530) {
      coins[i].y=-random(100);
      life--;
      if (life<=0) state=1;
    }
    if (dist(handX, handY, coins[i].x, coins[i].y)<30) {
      score+=100;
      coins[i].y=-random(100);
    }
    if (dist(mouseX, mouseY, coins[i].x, coins[i].y)<30) {
      score+=100;
      coins[i].y=-random(100);
    }
  }
  fill(255);
  ellipse(handX, handY, 30, 30);
  fill(255, 0, 0);
  textSize(30);
  text("Score:"+score +"Life:"+life, 200, 200);
}

**白色球為leapmotion偵測到的手



































沒有留言:

張貼留言