2022年10月24日 星期一

互技概 Week08

 0.音檔按下去換stage

1.按下去換音樂

 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){

     text("stage1", 100, 100);

   }else if(stage==2){

     text("stage2", 100, 100);

   }

 }

 void mousePressed(){

   if(stage==1){

     stage=2;

     sound1.stop();

     sound2.play();

   }else if(stage==2){

     stage=1;

     sound2.stop();

     sound1.play();

   }

 }


2.換舞台簡單版(沒有音檔)

void setup(){
  size(400, 300);
}
int stage = 1;  //1:start, 2:playing
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;
}


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;
}


4.用class 物件導向觀念改3.

class Fruit{
  float x, y, vx, vy;
  boolean flying;
  PApplet sketch;  ///為了讓random可以在class裡用
  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();
}
5.分頁並完成能切水果
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();
    }
  }
}

沒有留言:

張貼留言