2024年11月25日 星期一

week12 不知道晚餐要吃啥

 1.第一節課

到https://processing.org/reference/libraries processing 官網


進到processing 的函式庫下載Sound

到老師德github下載音樂https://github.com/jsyeh/2024interaction/tree/main

將音樂拉到程式碼裡面


//week12_1_libraries_sound_SoundFile_sound_isPlaying
//官網-Documentation-libraries函式庫-sound的部分
//https://processing.org/reference/libraries/sound/index.html
//我們教過的play()stop()pause(),今天要式isPlaying()是不是在撥放
import processing.sound.*;
SoundFile sound1,sound2;
int playing = 1;
void setup(){
  size(640,360);
  background(255);
  sound1 = new SoundFile(this,"Intro Song_Final.mp3");
  sound2 = new SoundFile(this,"In Game Music.mp3");
}
void draw(){
  if(sound1.isPlaying()){//音樂1有沒有在撥放
    playing=1;
  }else if(sound2.isPlaying()){//音樂2有沒有在撥放
    playing=2;
  }else{//沒有任何音樂在撥放
    if(playing==1)sound2.play();//換另外一首
    else sound1.play();
  }
}


按右鍵,找bezier()函式的網站文件



//week12_2_bezier_sample
//按右鍵,找bezier()函式的網站文件
size(500,500);
noFill();//以下,是從文件裡copy來的
stroke(255, 102, 0);
line(340, 80, 40, 40);
line(360, 360, 60, 320);
stroke(0, 0, 0);
bezier(340, 80, 40, 40, 360, 360, 60, 320);

2.第二節上課

//week12_3_bezier_1_3_3_1_equation
//按右鍵,找bezier()函式的網站文件
void setup(){
  size(500,500);
}
PVector p0 = new PVector(340,80);
PVector p1 = new PVector(40,40);
PVector p2 = new PVector(360,360);
PVector p3 = new PVector(60,320);
float t = 0.0;
void draw(){
  background(255);
  noFill();//以下,是從文件裡copy來的
  stroke(255, 102, 0);
  line(340, 80, 40, 40);
  line(360, 360, 60, 320);
  stroke(0, 0, 0);
  bezier(340, 80, 40, 40, 360, 360, 60, 320);
  float x = p0.x*(1-t)*(1-t)*(1-t) + 3*p1.x*t*(1-t)*(1-t) + 3*p2.x*t*t*(1-t) + p3.x*t*t*t; 
  float y = p0.y*(1-t)*(1-t)*(1-t) + 3*p1.y*t*(1-t)*(1-t) + 3*p2.y*t*t*(1-t) + p3.y*t*t*t;
  ellipse(x,y,10,10);
}
void mouseDragged(){
  t += 0.01 * (mouseX-pmouseX);
}

利用滑鼠拖曳可以控制小球在線上往哪個方向走

//week12_4_bezier_for_t_t0_0_many_t
//按右鍵,找bezier()函式的網站文件
void setup(){
  size(400,400);
}
PVector p0 = new PVector(120,80);
PVector p1 = new PVector(320,20);
PVector p2 = new PVector(320,300);
PVector p3 = new PVector(120,300);
float t0 = 0.0;
void draw(){
  background(255);
  noFill();//以下,是從文件裡copy來的
  stroke(255, 102, 0);
  line(120, 80, 320, 20);
  line(320, 300, 120, 300);
  stroke(0, 0, 0);
  bezier(120, 80, 320, 20, 320, 300, 120, 300);
  for(float t = t0; t>=0; t-=0.02){
    float x = p0.x*(1-t)*(1-t)*(1-t) + 3*p1.x*t*(1-t)*(1-t) + 3*p2.x*t*t*(1-t) + p3.x*t*t*t; 
    float y = p0.y*(1-t)*(1-t)*(1-t) + 3*p1.y*t*(1-t)*(1-t) + 3*p2.y*t*t*(1-t) + p3.y*t*t*t;
    ellipse(x,y,10,10);
  }
}
void mouseDragged(){
  t0 += 0.01 * (mouseX-pmouseX);
}


//week12_5_bezier_ArrayList_class
//按右鍵,找bezier()函式的網站文件
void setup(){
  size(400,400);
}
PVector p0 = new PVector(120,80);
PVector p1 = new PVector(320,20);
PVector p2 = new PVector(320,300);
PVector p3 = new PVector(120,300);
float t0 = 0.0;
ArrayList<Ball>balls = new ArrayList<Ball>();
void draw(){
  background(255);
  noFill();//以下,是從文件裡copy來的
  stroke(255, 102, 0);
  line(120, 80, 320, 20);
  line(320, 300, 120, 300);
  stroke(0, 0, 0);
  bezier(120, 80, 320, 20, 320, 300, 120, 300);
  for(Ball ball : balls){
    ball.draw();
  }
  if(frameCount%20==0) balls.add(new Ball(p0,p1,p2,p3));
  //for(float t = t0; t>=0; t-=0.02){
  //  float x = p0.x*(1-t)*(1-t)*(1-t) + 3*p1.x*t*(1-t)*(1-t) + 3*p2.x*t*t*(1-t) + p3.x*t*t*t; 
 //   float y = p0.y*(1-t)*(1-t)*(1-t) + 3*p1.y*t*(1-t)*(1-t) + 3*p2.y*t*t*(1-t) + p3.y*t*t*t;
  //  ellipse(x,y,10,10);
  //}
}
void mouseDragged(){
  t0 += 0.01 * (mouseX-pmouseX);
}
class Ball{
  PVector p0,p1,p2,p3;
  float t = 0.0;
  Ball(PVector _p0,PVector _p1, PVector _p2,PVector _p3){
    p0 = _p0;
    p1 = _p1;
    p2 = _p2;
    p3 = _p3;
  }
  void draw(){
    float x = p0.x*(1-t)*(1-t)*(1-t) + 3*p1.x*t*(1-t)*(1-t) + 3*p2.x*t*t*(1-t) + p3.x*t*t*t; 
    float y = p0.y*(1-t)*(1-t)*(1-t) + 3*p1.y*t*(1-t)*(1-t) + 3*p2.y*t*t*(1-t) + p3.y*t*t*t;
    ellipse(x,y,10,10);
    t += 0.001;
  }
}
讓球自動生成出來

3.第三節上課

//week12_6_bezier_ArrayList_balls_size_get_remove
//按右鍵,找bezier()函式的網站文件
void setup(){
  size(400,400);
}
PVector p0 = new PVector(120,80);
PVector p1 = new PVector(320,20);
PVector p2 = new PVector(320,300);
PVector p3 = new PVector(120,300);
float t0 = 0.0;
ArrayList<Ball>balls = new ArrayList<Ball>();
void draw(){
  background(255);
  noFill();//以下,是從文件裡copy來的
  stroke(255, 102, 0);
  line(120, 80, 320, 20);
  line(320, 300, 120, 300);
  stroke(0, 0, 0);
  bezier(120, 80, 320, 20, 320, 300, 120, 300);
  for(Ball ball : balls){
    ball.draw();
  }
  if(frameCount%20==0) {
    balls.add(new Ball(p0,p1,p2,p3));
    println(balls.size());
  }
  //for(float t = t0; t>=0; t-=0.02){
  //  float x = p0.x*(1-t)*(1-t)*(1-t) + 3*p1.x*t*(1-t)*(1-t) + 3*p2.x*t*t*(1-t) + p3.x*t*t*t; 
 //   float y = p0.y*(1-t)*(1-t)*(1-t) + 3*p1.y*t*(1-t)*(1-t) + 3*p2.y*t*t*(1-t) + p3.y*t*t*t;
  //  ellipse(x,y,10,10);
  //}
  if(balls.size()>0&&balls.get(0).t>1.0)balls.remove(0);
}
void mouseDragged(){
  t0 += 0.01 * (mouseX-pmouseX);
}
class Ball{
  PVector p0,p1,p2,p3;
  float t = 0.0;
  Ball(PVector _p0,PVector _p1, PVector _p2,PVector _p3){
    p0 = _p0;
    p1 = _p1;
    p2 = _p2;
    p3 = _p3;
  }
  void draw(){
    float x = p0.x*(1-t)*(1-t)*(1-t) + 3*p1.x*t*(1-t)*(1-t) + 3*p2.x*t*t*(1-t) + p3.x*t*t*t; 
    float y = p0.y*(1-t)*(1-t)*(1-t) + 3*p1.y*t*(1-t)*(1-t) + 3*p2.y*t*t*(1-t) + p3.y*t*t*t;
    ellipse(x,y,10,10);
    t += 0.001;
  }
}
讓小球生成和移動範圍不會超過整個圈
4.上傳github


沒有留言:

張貼留言