2022年10月24日 星期一

week08_Yiting

鍵盤遊戲(水果忍者)

(1)下載外掛

詳情見上週筆記

(2)下載音樂

(3)播放音樂



程式碼:

SoundFile sound1,sound2,sound3;

void setup(){

  size(400,300);

  sound1=new SoundFile(this,"In Game Music.mp3");

  sound1.play();

}

void draw(){

  

}

(4)切換舞台及音樂

程式碼:

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");
  sound1=new SoundFile(this, "Intro Song_Final.mp3");
  sound1.play();
}
int stage=1;
void draw() {
  background(255);
  if (stage==1) {
    text("stage 1", 100, 100);///"文字",x,y
  } else if (stage==2) {
    text("stage 2", 100, 100);
  }
}
void mousePressed() {
  if (stage==1) {///如果在舞台2的話
    stage=2;
    sound1.stop();///暫停1的音樂
    sound2.play();///播放2的音樂
  } else if (stage==2) {///如果在舞台1的話
    stage=1;
    sound2.stop();///暫停2的音樂
    sound1.play();///播放1的音樂
  }
}

(5)舞台設置
程式碼比較簡單的舞台設定
程式碼:
void setup(){
  size(400,300);
}
int stage=1;
void draw(){
  background(238,149,250);
  fill(59,3,67);
  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)水果
設定一個目標(待消除物)且讓他可以移動
程式碼:
void setup(){
  size(400,300);
}
float fruitX=200,fruitY=150;///位置
float fruitVX=-1,fruitVY=-1;///移動的速度
boolean flying=true;
void draw(){
  background(238,149,250);
  ellipse(fruitX,fruitY,50,50);
  if(flying){///如果在移動,目標位置會改變
    fruitX += fruitVX;
    fruitY += fruitVY;
  }
}
void keyPressed(){
  flying=false;
}

讓目標可以像被拋上去一樣
程式碼:
void setup(){
  size(400,300);
}
float fruitX=200,fruitY=300;
float fruitVX=-2,fruitVY=-13;
boolean flying=true;
void draw(){
  background(238,149,250);
  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;
}

使用class,讓每個物件都可以用函式生出來
程式碼:

 class Fruit{
  float x,y,vx,vy;
  boolean flying;
  PApplet sketch;///讓random可以使用
  Fruit(PApplet _sketch){///建構子:一開始要做的事
    sketch = _sketch;///讓random可以使用
    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(238,149,250);
  ellipse(fruit.x,fruit.y,50,50);
  fruit.update();
}
void keyPressed(){
  fruit.reset();///按shift可以重製物件
}

(7)成果
程式碼1(主程式):
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(238, 149, 250);
  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();
    }
  }
}
程式碼2(水果):
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;
  }
}






沒有留言:

張貼留言