2022年10月24日 星期一

RH week8

 week08 水果忍者

1.先根據moodle上給的網址下載各種水果忍者的音效

2.打開程式函式庫,下載sound函式

3.輸入聲音函式,接著先存檔,然後把下載的瑛拉進來

```c
import processing.sound.*;
SoundFile sound1,sound2,sound3;
//存檔,把音樂直接拉進來
void setup(){
  size(400,300);
  sound1=new SoundFile(this,"In Game Music.mp3");
  sound1.play();
}
void draw(){
}
```

4.切換舞台

新增stage1、2,並加上mousePressed()函式,使一下滑鼠就切換舞台及切換音樂,接著用text印出該舞台是1還2
```c
import processing.sound.*;
SoundFile sound1,sound2,sound3;
//存檔,把音樂直接拉進來
void setup(){
  size(400,300);
  textSize(50);
  fill(255,0,0);
  sound1=new SoundFile(this,"In Game Music.mp3");
  sound2=new SoundFile(this,"Intro Song_Final.mp3");
  sound1.play();
}
int stage =1;  //1,2,3
void draw(){
  background(255);
  if(stage==1){//舞台1
    text("stage 1",100,100);
  }else if(stage==2){//舞台2
    text("stage 2",100,100);
  }
}
void mousePressed(){
  if(stage==1){
    stage=2;
    sound1.stop();
    sound2.play();
  }else if(stage==2){
    stage=1;
    sound2.stop();
    sound1.play();
  }
}
```


5.再寫一次只切背景的程式

```c
void setup(){
  size(400,300);
}
int stage=1;
void draw(){
  background(255,255,0);    //背景顏色
  fill(255,0,0);    //字顏色
  textSize(80);
  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;
}
```


6.讓水果飛起來,設定好初始位置和移動速度,讓水果由左下望右上飛

```c
void setup(){
  size(400,300);
}
float fruitX=200,fruitY=150;//水果的位置 X Y有小數點,精確
float fruitVX=1,fruitVY=-1;//水果的速度 VX VY
boolean flying=true;
void draw(){
  background(255,255,0);
  
  ellipse(fruitX,fruitY,50,50);
  if(flying){//如果在飛,水果位置會改變
    fruitX+=fruitVX;
    fruitY+=fruitVY;
  }
}
void keyPressed(){
  flying=false;
}
```

7.修改參數使水果到一定高度時落下,因此加上了重力加速度0.98

```c
void setup(){
  size(400,300);
}
float fruitX=200,fruitY=300;//水果的位置 X Y有小數點,精確
float fruitVX=2,fruitVY=-13;//水果的速度 VX VY
boolean flying=true;
void draw(){
  background(255,255,0);
  
  ellipse(fruitX,fruitY,50,50);
  if(flying){//如果在飛,水果位置會改變
    fruitX+=fruitVX;
    fruitY+=fruitVY;
    fruitVY+=0.98/3;//重力加速度
  }
}
void keyPressed(){
  flying=false;
  fruitReset();  //重新準備另一波水果

void fruitReset(){
  fruitX=random(100,300);
  fruitY=300;//固定高度
  fruitVX=random(-2,+2);
  fruitVY=-13;
  flying=true;
}
```


8.另一種寫法,為了使之後更多的水果出現不用打許多頻繁、重複的程式碼

```c
class Fruit{
  float x,y,vx,vy;
  boolean flying;
  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;
  }
  void update(){
    x += vx;
    y += vy;
    vy+=0.98/3;
  }
}
Fruit fruit;
void setup(){
  size(400,300);
  fruit = new Fruit(this);
}
void draw(){
  background(255,255,0);
  ellipse(fruit.x,fruit.y,50,50);
  fruit.update();
}
void keyPressed(){
  fruit.reset();
}
```

9.類似真正的水果忍者,會隨機跳出任意字母所代表的水果,必須按出對應的按鍵才可消除
主函式
```c
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();
    }
  }
}
```
Fruit外掛函式
```c
String line="ABCDEFGHIJKLMOPQRSTUVWXYZ";
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;
  }
}
```




沒有留言:

張貼留言