2023年12月14日 星期四

Doing Nothing Losing Life Week14

 Week14

嘗試修改上週期中展示的程式碼

```
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;
String marqueeText = "5334221234555";
float marqueeX; // 初始化文字的起始位置
int marqueeColor = 255; // 初始文字顏色為白色

void setup() {
  size(960, 540);
  img = loadImage("Piano.png");
  img2 = loadImage("Piano2.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);
  marqueeText = "5334221234555" + "5334221234555" + "5334221234555" + "5334221234555" + "5334221234555" +
    "5334221234555" + "5334221234555" + "5334221234555" + "5334221234555" + "5334221234555";
}

void draw() {
  background(#303030);
  image(img2, -10, 150, 1000, 540);
  fill(#616362);
  rect(480, 153, 980, 5);

  // 跑馬燈文字
  textSize(60);
  for (int i = 0; i < marqueeText.length(); i++) {
    char currentChar = marqueeText.charAt(i);
    fill(i == 0 ? marqueeColor : 255);
    text(currentChar, marqueeX + i * textWidth(currentChar), 100);
  }

  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() {
  for (int i = 0; i < 7; i++) {
    if (key == str(i + 1).charAt(0)) {
      if (marqueeText.length() > 0 && i + 1 == int(marqueeText.charAt(0) - '0')) {
        marqueeColor = color(255, 0, 0);
        marqueeText = marqueeText.substring(1);
        break;
      }
    }
  }

  if (key == '1') {
    pressed[0] = true;
    Do.play();
  } else if (key == '2') {
    pressed[1] = true;
    Re.play();
  } else if (key == '3') {
    pressed[2] = true;
    Mi.play();
  } else if (key == '4') {
    pressed[3] = true;
    Fa.play();
  } else if (key == '5') {
    pressed[4] = true;
    Sol.play();
  } else if (key == '6') {
    pressed[5] = true;
    La.play();
  } else if (key == '7') {
    pressed[6] = true;
    Si.play();
  }
}

void keyReleased() {
  for (int i = 0; i < 7; i++) {
    if (key == str(i + 1).charAt(0)) {
      pressed[i] = false;
      break;
    }
  }
}
```
目前我們還是直接在螢幕上面直接顯示字串,並且將要按下的數字改為紅色,思考要如何讓畫面看起來乾淨一些。


沒有留言:

張貼留言