2023年11月30日 星期四

Doing Nothing Losing Life Week12

 Week12

開始構想作業的目標,決定製作模擬鋼琴的遊戲

```
import processing.sound.*;
int [][] pos = {{1,269},{241,270},{480,269},{721,271},{0,405},{240,405},{481,404},{721,405}};
SoundFile Do,Re,Mi,Fa,Sol,La,Si;

void setup(){
  size(960,540);
  Do = new SoundFile(this,"Do.MP3");
  Re = new SoundFile(this,"Re.MP3");
  Mi = new SoundFile(this,"Mi.MP3");
  Fa = new SoundFile(this,"Fa.MP3");
  Sol = new SoundFile(this,"Sol.MP3");
  La = new SoundFile(this,"La.MP3");
  Si = new SoundFile(this,"Si.MP3");
}

void draw() {
  background(255);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      fill(255);
      rect(j * 240, i * 135, 240, 135);
      
      // 在每個方塊的中心寫入文字
      fill(0);  // 設定文字顏色為黑色
      textAlign(CENTER, CENTER);  // 文字置中對齊
      textSize(24);  // 設定文字大小
      
      fill(255,0,0, 128); //128是透明度
      rect(mouseX, mouseY, 240, 135, 5);
      fill(0,255,0,  128);
      

      // 使用 do re mi 作為文字內容
      String textContent = "";
      switch (i * 4 + j + 1) {
        case 1:
          textContent = "do";
          break;
        case 2:
          textContent = "re";
          break;
        case 3:
          textContent = "mi";
          break;
        case 4:
          textContent = "fa";
          break;
        case 5:
          textContent = "so";
          break;
        case 6:
          textContent = "la";
          break;
        case 7:
          textContent = "ti";
          break;
        case 8:
          textContent = "do";
          break;
      }
      text(textContent, j * 240 + 120, i * 135 + 67.5);
    }
  }
}
void keyPressed(){
  if(key== '1')Do.play();
  if(key== '2')Re.play();
  if(key== '3')Mi.play();
  if(key== '4')Fa.play();
  if(key== '5')Sol.play();
  if(key== '6')La.play();
  if(key== '7')Si.play();
}

```
一開始先將Sound加入程式庫,並將視窗分為16個區域,在每個方塊中間加入Do Re Mi
並且畫出一個紅色方塊找出每個方塊的座標。
按下對應的數字鍵波放出相對應的音效。








沒有留言:

張貼留言