2016年11月11日 星期五

20161110 小楓兒筆記

1. Leap Motion
2. 檔案讀取、寫檔
3. 排名
4. ArrayList 與資料結構


小畫家





void setup(){
  size(600,600);
}
void draw(){
  if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
}
void keyPressed(){
  save("now.png");
}


           
按任意鍵存檔
sketch(速寫本)→Show Sketch Folder(打開程序目錄)→即可找到剛剛存的圖檔






避免存檔覆蓋

檔名會自動存成 now_XXX


void setup(){
  size(600,600);
}
void draw(){
  if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);  
}
int number=0;
void keyPressed(){
  //save("now.png");
  save("now_" + number + ".png");
  number++;
}






存檔(saveFrame)


檔名會以screen命名

void setup(){
  size(600,600);
}
void draw(){
  if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);  
}
//int number=0;
void keyPressed(){
  saveFrame();
  //save("now.png");
  //save("now_" + number + ".png");
  //number++;
}




遊戲玩完存檔(saveString)




String [] names={"AAA","BBB","CCC"};
saveStrings("myClassmateName.txt",names);


*ctrl+K 開檔案資料夾




可以存檔讀檔案或網址



                                             可以是檔名或網址
                                                         
String lines[]=loadStrings("myClassmateName.txt");
println("there are"+lines.length+"lines");
for(int i=0;i<lines.length;i++){
  println(lines[i]);
}




接金幣





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;
void draw(){
  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+=2;
    if(coins[i].y>530) 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,0,0);textSize(30);text("Score:"+score,200,200);
}





接金幣死掉畫面





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;
void draw(){
  if(life<=0){
    background(128);speed=0;
  }
  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(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);
}



接金幣顯示分數紀錄




state=1
































state=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);
    allScore=loadStrings("allScores.txt");
    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);
}




分數排名






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);
    allScore=loadStrings("allScores.txt");
    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;
       allScore=loadStrings("allScores.txt");
       String [] newScore=new String[allScore.length+1];
       for(int j=0;j<allScore.length;j++){
         newScore[j]=allScore[j];
       newScore[allScore.length]=new String(""+score);
       saveStrings("allScores.txt",newScore);
       }
    }
    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);
}
}



分數計分排名






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);
    allScore=loadStrings("allScores.txt");
    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;
       allScore=loadStrings("allScores.txt");
       String [] newScore=new String[allScore.length+1];
       int now=0;
       for(int j=0;j<allScore.length;j++){
         if(j==now&&score>Integer.valueOf(allScore[j])){
           newScore[now]=new String(""+score);
           now++;
          }
         newScore[now]=allScore[j];
         now++;
         }
         saveStrings("allScores.txt",newScore);
       }
    }
    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);
}







沒有留言:

張貼留言