2022年10月31日 星期一

( σ՞ਊ ՞)σ的互動技術筆記 week08

為什麼我換手機就可以登GOOGLE了?????????


step1-1

 先下載音樂,並播放


import processing.sound.*;

SoundFile sound1, sound2, sound3;


void setup() {

  size(400, 300);

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

  sound1.play();

}

void draw() {

}

------------------------------------------------------------------------------

step1-2

把音樂下載好,放在資料夾裡面,點滑鼠可以切換音樂

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, "Monkey 1.mp3");

  sound1.play();

}

int stage=1;

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=2;

    sound1.stop();

    sound2.play();

  }

}


------------------------------------------------------------------------------

step1-3

簡化剛剛的程式

void setup() {
  size(400, 300);
}
int stage=1;
void draw() {
  background(255,255,0);
  fill(255,0,0);
  textSize(80);
  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;
  else if(stage==2) stage=1;
}

------------------------------------------------------------------------------

step2-1
物體沒有重力會漂走

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


------------------------------------------------------------------------------

step2-2
給物體重力

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

------------------------------------------------------------------------------

step2-3

修改一下


//目標: class物件: 每個水果都可以用物件做出來(值、函式)
class Fruit{
  float x,y,vx,vy;
  boolean flying;
  PApplet sketch;//為了讓random可以用,修改一下
  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);//為了讓random可以用,修改一下
}
void draw(){
  background(255,255,0);
  ellipse(fruit.x, fruit.y, 50, 50);
  fruit.update();
}
void keyPressed(){
  fruit.reset();
}

------------------------------------------------------------------------------

step2-4-1
水果的程式

//目標: class物件: 每個水果都可以用物件做出來(值、函式)
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;
  }
}


step2-4-2
主程式

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

沒有留言:

張貼留言