2016年11月10日 星期四

60508013E_林子婷,Week09

105/11/10

1.檔案讀取/寫檔
   1)永遠存同一個檔名(會蓋掉上一版)
 *存檔程式碼


*畫圖


*開啟資料夾


*資寮夾內有剛剛的檔案(now.png)

    2)每次存不同檔名



*檔名會每次編號都+1

      3) saveFrame



     4) 文字檔




2.接金幣
    1)滑鼠接金幣



*加上life機制


---程式碼---
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);
}



     2) Leap Motion 接金幣



*白點為手的位置

---程式碼---
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);
}




沒有留言:

張貼留言