2022年11月1日 星期二

大拇指的啦

 放音樂、切場景

設定變數

SoundFile sound1,sound2;

讀音檔

sound1= new SoundFile(this,"檔名.mp3");

撥放音樂

sound1.play();

設定切換場景變數

int stage=1;

用mousePressed()內+if else來判斷切換場景與放音樂、暫停音樂

void mousePressed(){

  if( stage==1) stage=2;

  else if (stage==2) stage=1;

}

設定兩個舞台 舞台1顯示stage1 舞台2顯示stage2

if (stage==1){

     text("stage 1",100,100); 

  }else if(stage==2){

     text("stage 2",100,100); 

  } 

整體

void setup(){

  size(400,300);

}

int stage=1;

void draw(){

  background(255,255,0);

  fill(255,0,0);

  if (stage==1){

     text("stage 1",100,100); 

  }else if(stage==2){

     text("stage 2",100,100); 

  } 

}

void mousePressed(){

  if( stage==1) stage=2;

  else if (stage==2) stage=1;

}

切水果

設水果變數

float fruitX=200 , fruitY=300

速度變數

fruitVX=1、fruitVY=-1

控制水果是否要往上飛

boolean flying = true;

畫水果

ellipse(fruitX,fruitY,50,50);

利用按鍵鍵盤控制 按下時停止

void keyPressed(){

    flying = false;

}

將fruitY改成300 讓他從底下開始飛 

初始速度

fruitVX=2 (負的左 正的右) 

fruitVY=-13 

設定重力

fruitVY+=0.98/3

設定一個按下按鍵會有新水果飛出來的函式

void fruitReset(){

   fruitX=random(100,300);

   fruitY=300;

   fruitVX=random(-2,+2);

   fruitVY=-13;

   flying=true;

}

用class分類來修改切水果

先設定Class Fruit 裡面設定變數

float x,y,vx,vy;

先呼叫PApplet sketch 這個函式庫 在class裡呼叫自己

Fruit(PApplet_sketch)

///PApplet sketch是為了後面的random所需 ,random需改成sketch.random()

設定reset和update函式 

主程式的地方改成用Fruit的Class

class Fruit{
   float x,y, vx, vy;
   boolean flying;
   PApplet sketch;///在class呼叫PApplet sketch的class為了Random可以使用
   Fruit(PApplet _sketch){//呼叫自己,來產生另外一顆水果
      sketch = _sketch;//設立sketch。
      reset(); 
   }
   void reset(){
     x=sketch.random(100,300);
     y=300;
     vx=sketch.random(-2,2);
     vy=-13;
     flying=true;
   }
   void update(){
     x+=vx;
     y+=vy;
     vy+=0.98/3;
   }
}
Fruit fruit; //fruit是Fruit的東西
void setup(){
  size(400,300);
  fruit = new Fruit(this);   
}
void draw(){
  background(#FFF798);
  ellipse(fruit.x,fruit.y,50,50);//用class裡的x,y
  fruit.update();//用class裡的update
}
void keyPressed(){
  fruit.reset(); //用class裡的reset
}
Class新分頁
取名為Fruit 並將原本在Class裡的程式碼貼過去 把原本的刪除

class Fruit{
  float x,y,vx,vy;
  boolean flying;
  char c;
  PApplet sketch;
  Fruit(PApplet _sketch){
     sketch = _sketch;
     reset();
  }
  void reset(){
     x=sketch.random(100.0 , 300.0);
     y=300;
     vx=sketch.random(-2,+2);
     vy=-13;
     flying = true;
     int i = int(random(26));
     c = line.charAt(i);
  }
  void update(){
     x += vx;
     y += vy;
     vy += 0.98/3;
  }
}
水果上的字
設定一個字串陣列放入A~Z 在建立一個char字串
String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

利用變數去對到陣列裡的字母
int i = int(random(26));
c = line.charAt(i);

主程式
宣告fruit改成陣列
Fruit [] fruits;
fruits = new Fruit[3];//放setup裡面

用for迴圈去偵測飛起來的fruit 
///friut要改成fruit[i] 放在setup() 、draw()、keyPressed()裡

設定字體大小 、顏色、置中、顯示
textSize(30);//大小
textAlign(CENTER ,CENTER);///置中
fill(0); ///顏色
text(fruits[i].c, fruits[i].x , fruits[i].y);///顯示字

在keyPressed()裡 使用keyCode去偵測檢盤的字是否跟水果的字一樣
如果一樣就reset()
void keyPressed(){
   for (int i=0; i<3;i++){
      if ( keyCode == fruits[i].c){
         fruits[i].reset();
      }
   }
}

最後成品
主程式
Fruit [] fruits;
void setup(){
   size(400,300);
   fruits = new Fruit[3];
   for (int i=0;i<3;i++){
      fruits[i]= new Fruit(this); 
   }
}
void draw(){
   background(255,255,0);
   for (int i=0;i<3;i++){
      fill(255);
      ellipse(fruits[i].x ,fruits[i].y , 50,50);
      textSize(30);
      textAlign(CENTER ,CENTER);
      fill(0); 
      text(fruits[i].c, fruits[i].x , fruits[i].y);
      fruits[i].update();
   }
}
void keyPressed(){
   for (int i=0; i<3;i++){
      if ( keyCode == fruits[i].c){
         fruits[i].reset();
      }
   }
}
副程式
String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Fruit{
  float x,y,vx,vy;
  boolean flying;
  char c;
  PApplet sketch;
  Fruit(PApplet _sketch){
     sketch = _sketch;
     reset();
  }
  void reset(){
     x=sketch.random(100.0 , 300.0);
     y=300;
     vx=sketch.random(-2,+2);
     vy=-13;
     flying = true;
     int i = int(random(26));
     c = line.charAt(i);
  }
  void update(){
     x += vx;
     y += vy;
     vy += 0.98/3;
  }
}

沒有留言:

張貼留言