2024年1月4日 星期四

Doing Nothing Losing Life Week17

 Week17

成功解決遇到空格時自動刪除空格,並且新增開始頁面及操作說明

```
import processing.sound.*;

int[][] pos = {
  {66, 532}, {205, 532}, {348, 532}, {492, 532}, {633, 532}, {773, 532}, {914, 532}
};

SoundFile Do, Re, Mi, Fa, Sol, La, Si;
PImage img, img2, logo, tuto;
PFont font;
String marqueeText1 = "533 422 1234 555 533 422 13553 2222 234 3333 345 533 422 13551";//小蜜蜂
String backup1 = "533 422 1234 555 533 422 13553 2222 234 3333 345 533 422 13551";
String marqueeText2 = "1155 665 4433 221 5544 332 5544 332 1155 665 4433 221";//小星星
String backup2 = "1155 665 4433 221 5544 332 5544 332 1155 665 4433 221";
String marqueeText3 = "1231 1231 345 345 565431 565431 151 151";//兩隻老虎
String backup3 = "1231 1231 345 345 565431 565431 151 151";
float marqueeX; // 初始化文字的起始位置
int marqueeColor = 255; // 初始文字顏色為白色
int marqueeTextI=0;
int ID = 0;
int select = 0;
void setup() {
  size(960, 540);
  img = loadImage("Piano.png");
  img2 = loadImage("Piano2.png");
  logo = loadImage("PianoLogo.png");
  tuto= loadImage("Tutorial.png");
  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");
  rectMode(CENTER);
}

void draw() {
  if (select == 0) {//開始場景
    background(#303030);
    image(tuto,750,180,212,170);
    image(logo, 280, 20, 400, 400);
    textSize(75);
    text("Click to Start", 280, 500);
    textSize(30);
    text("ver0.5", 880, 535);
  }

  if (select == 1) {
    background(#303030);
    textSize(35);
    text("Little Bee",10,40);
    image(img2, -10, 150, 1000, 540);
    fill(#616362);
    rect(480, 153, 980, 5);
    textSize(120);
    for (int i = 0; i < marqueeText1.length(); i++) {
      char currentChar = marqueeText1.charAt(i);
      fill(i == 0 ? marqueeColor : 255);//後方數字始終為白色
      text(currentChar, marqueeX + i * textWidth(currentChar), 135);
    }
  } else if (select == 2) {
    background(#303030);
    textSize(35);
    text("Little Star",10,40);
    image(img2, -10, 150, 1000, 540);
    fill(#616362);
    rect(480, 153, 980, 5);
    textSize(120);
    for (int i = 0; i < marqueeText2.length(); i++) {
      char currentChar = marqueeText2.charAt(i);
      fill(i == 0 ? marqueeColor : 255);//後方數字始終為白色
      text(currentChar, marqueeX + i * textWidth(currentChar), 135);
    }
  } else if (select == 3) {
    background(#303030);
    textSize(35);
    text("Two Tigers",10,40);
    image(img2, -10, 150, 1000, 540);
    fill(#616362);
    rect(480, 153, 980, 5);
    textSize(120);
    for (int i = 0; i < marqueeText3.length(); i++) {
      char currentChar = marqueeText3.charAt(i);
      fill(i == 0 ? marqueeColor : 255);//後方數字始終為白色
      text(currentChar, marqueeX + i * textWidth(currentChar), 135);
    }
  }

  /*if (marqueeX < -textWidth(marqueeText)) {
   marqueeX = width;
   marqueeColor = 255;
   }*/

  // 檢查是否有按鍵被按下,以及該按鍵是否與跑馬燈上的數字相同
  for (int i = 0; i < 7; i++) {
    if (pressed[i]) {
      fill(#7eeffd);
      rect(pos[i][0], pos[i][1], 130, 10, 5);
    }
  }
}

boolean[] pressed = new boolean[7];

void keyPressed() {
  if (select==1) {
    //if(keyCode==RIGHT) marqueeTextI=(marqueeTextI+1) % marqueeText.length();
    for (int i = 0; i < 7; i++) {
      if (key == str(i + 1).charAt(0)) {
        if (marqueeText1.length() > 0 && i + 1 == int(marqueeText1.charAt(0) - '0')) {
          marqueeColor = color(255, 255, 0);//按下(住)顏色變為黃色
          marqueeText1 = marqueeText1.substring(1);
          if (marqueeText1.length()>0 && marqueeText1.charAt(0)==' ' ) marqueeText1 = marqueeText1.substring(1);
          if (marqueeText1.length()<1) marqueeText1 += backup1;//在剩下一個音符時加上備用音譜
          //marqueeTextI=(marqueeTextI+1) % marqueeText.length();
          break;
        }
      }
    }
  } else if (select ==2) {
    for (int i = 0; i < 7; i++) {
      if (key == str(i + 1).charAt(0)) {
        if (marqueeText2.length() > 0 && i + 1 == int(marqueeText2.charAt(0) - '0')) {
          marqueeColor = color(255, 255, 0);//按下(住)顏色變為黃色
          marqueeText2 = marqueeText2.substring(1);
          if (marqueeText2.length()>0 && marqueeText2.charAt(0)==' ' ) marqueeText2 = marqueeText2.substring(1);
          if (marqueeText2.length()<1) marqueeText2 += backup2;//在剩下一個音符時加上備用音譜
          //marqueeTextI=(marqueeTextI+1) % marqueeText.length();
          break;
        }
      }
    }
  } else if (select == 3) {
    for (int i = 0; i < 7; i++) {
      if (key == str(i + 1).charAt(0)) {
        if (marqueeText3.length() > 0 && i + 1 == int(marqueeText3.charAt(0) - '0')) {
          marqueeColor = color(255, 255, 0);//按下(住)顏色變為黃色
          marqueeText3 = marqueeText3.substring(1);
          if (marqueeText3.length()>0 && marqueeText3.charAt(0)==' ' ) marqueeText3 = marqueeText3.substring(1);
          if (marqueeText3.length()<1) marqueeText3 += backup3;//在剩下一個音符時加上備用音譜
          //marqueeTextI=(marqueeTextI+1) % marqueeText.length();
          break;
        }
      }
    }
  }
  if (key == 'j') {
    select ++;
    if (select > 3) select = 1;
  }else if(key == 'f'){
    select --;
    if(select < 1)select = 3;
  }
  if(key == 9){
    select = 0;
  }
  
  if(key>='1'&& key<='7'){
    SoundFile[] file = {Do,Re,Mi,Fa,Sol,La,Si};
    pressed[key-'1'] = true;
    file[key-'1'].play();
  }
}

void mousePressed() {
  if (select == 0)select =1;
}

void keyReleased() {
  for (int i = 0; i < 7; i++) {
    if (key == str(i + 1).charAt(0)) {
      marqueeColor = color(255, 255, 255);//放開之後顏色變回白色
      pressed[i] = false;
      break;
    }
  }
}
```

if (marqueeText1.length()>0 && marqueeText1.charAt(0)==' ' ) marqueeText1 = marqueeText1.substring(1);
當遇到空格時自動刪除空格。


沒有留言:

張貼留言