2022年10月24日 星期一

@W@ Week08

 

Week08

1.)

1-1.下載mp3檔 --> sketch / Library / Manager --> 安裝 sound

1-2.
播音樂
import processing.sound.*;
SoundFile sound1,sound2,sound3;
void setup(){
 size(400,300); 
 sound1= new SoundFile(this,"In Game Music.mps");
 sound1.play();
}
void draw(){
   
}



1-3.
加上場景(stage)切換
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;
void draw(){
   background(255);
   if(stage==1){
    text("stage 1",100,100);
   }else if(stage==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(); 
 }
}


2.)

2-1.
簡化版切換stage
void setup(){
 size(400,300);
}
int stage=1;
void draw(){
   background(255);
   textSize(80);
   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;
}
2-2.
會飛的水果
void setup(){
 size(400,300);
}
float fruitX=200,fruitY=150;
float fruitVX=1,fruitVY=-1;
boolean flying=true;
void draw(){
   background(255,255,0);
   ellipse(fruitX,fruitY,50,50);
   if(flying){
    fruitX+=fruitVX;
    fruitY+=fruitVY;
   }
}
void keyPressed(){
   flying=false; 
}


2-3.
讓球有重力加速度
void setup(){
 size(400,300);
}
float fruitX=200,fruitY=300;
float fruitVX=2,fruitVY=-13;
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;
}

3.)

3-1.
Fruit [] fruits;
void setup(){
 size(400,300);
 fruits=new Fruit[3];
 //fruit=new Fruit(this);//讓random可以用
 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
String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Fruit{
  float x,y,vx,vy;
  boolean flying;
  char c;//對應水果
  PApplet sketch;//讓random可以用
  Fruit(PApplet _sketch){
  sketch= _sketch;//讓random可以用
  reset();
 }
 void reset(){
  x=sketch.random(100.0,300.0);//讓random可以用
  y=300;
  vx=sketch.random(-2,+2);//讓random可以用
  vy=-13;
  flying = true;
  int i=int(random(26));
  c = line.charAt(i);
 }
 void update(){
  x+=vx;
  y+=vy;
  vy+=0.98/3;
 }
}





沒有留言:

張貼留言