0

我是这个编程领域的新手,正在研究视觉节拍器。每个字母都有一个分配给它的鼓模式。

稍后我将努力为其添加点击声音,但我想先弄清楚视觉效果。除非你们中的一些人可以帮助我在每次字母变化时添加一个声音,那太好了!

不过,关于视觉部分,我想区分单个字母上的文本颜色,这样左边的会更突出,而右边的会更暗。

你们中的一些人能帮助我吗?

最好的,SHB

String[] words = {"A    ", "B    ", "C    ", "D    ", "E    ", "F    "}; 
int newIndex = 0;
int oldIndex = -1;
PFont SansSerif;

void setup() {
  size(700, 500);
  background(0);
  SansSerif = createFont("SansSerif", 162);
  textFont(SansSerif);
}

void draw() {
  frameRate(.6); 
  background(0);
  // Get a random element from array
  newIndex = int(random(words.length));
  
  if (oldIndex > -1) {
    fill(150);
    text(words[oldIndex]  + words[newIndex], 150, 300);
    println("old =", words[oldIndex] + " : " +  "new =", words[newIndex] ); 
  } else {
    fill(150);
    text(words[newIndex],  150, 300);
    println("new =", words[newIndex]);
  }
  oldIndex = newIndex;
}
4

1 回答 1

0

通过将您选择的字母更改为字符串,然后遍历字符,您可以使用 charAt() 将每个字符更改为不同的颜色。

String[] words = {"A    ", "B    ", "C    ", "D    ", "E    ", "F    "}; 
int newIndex = 0;
int oldIndex = -1;
PFont SansSerif;
String beat = "";
int x = 0;

void setup() {
  size(700, 500);
  background(255); // Changed to lighter background
  SansSerif = createFont("SansSerif", 162);
  textFont(SansSerif);
}

void draw() {
  frameRate(.6); 
  background(255); // Changed to lighter background
  // Get a random element from array
  newIndex = int(random(words.length));  
  if (oldIndex > -1) {
    beat = words[oldIndex] + words[newIndex];
    int x = 150;
    for (int i = 0; i < beat.length(); i++){
      if(i == 0){
        fill(0);
      } else {
        fill(150);
      }
      text(beat.charAt(i), x, 300);
      x += 60;
    }
    println("old =", words[oldIndex] + " : " +  "new =", words[newIndex] ); 
  } else {
    fill(0);
    text(words[newIndex],  150, 300);
    println("new =", words[newIndex]);
  }
  oldIndex = newIndex;
}

于 2021-12-30T13:28:17.277 回答