与数组中的特定元素交互;加工3

Interacting with specific elements in an array; Processing 3

我正在尝试创建一个正方形网格,当键入正确的键时 "turn on",当用鼠标单击它们时 "turn off"。在我的程序中,会生成一个随机索引号,它对应于特定键的 Unicode 值,当您按下该键时,网格上的随机方块会变成绿色。重新着色后,会为不同的键生成新的索引号,依此类推。鼠标单击最后一个彩色方块可以“取消颜色”(变为黑色),但不能取消任何其他先前彩色方块。

问题似乎是 mousePressed 代码与数组中最后着色的元素相关联,但我不知道如何让它与 any 交互数组中已着色的元素的数量。有可能吗?我考虑过更改数组,以便每个元素在数组中的位置本身都被打乱,但它创建的形状仍然排列在网格中,然后每次单击鼠标都会向后迭代。但是我似乎无法弄清楚如何在不进入我不熟悉的 java 的情况下对数组进行洗牌。这是一个可以解决的问题还是我应该以某种方式调整我的代码?这是我目前所拥有的:

主要脚本:

int cols = 16;
int rows = 10;
boolean light = false;

Box[][] boxes = new Box[cols][rows];

int keyIndex = int(random(97, 122));
int randI = (int)random(0, cols);
int randJ = (int)random(0, rows);

void setup() {
  size (800, 600);
  background (0);
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      boxes[i][j] = new Box(i, j);
    }
  }
  println(keyIndex);
}


void draw() { 
  if (light == true) {
    boxes[randI][randJ].rollover(mouseX, mouseY);
    boxes[randI][randJ].displayOn();
  } else {
    boxes[randI][randJ].displayOff();
  }
}

void mousePressed() {
  if (boxes[randI][randJ].onPress(mouseX, mouseY)) {
    println("yes");
    light = false;
  } else {
    println("no");
  }
}

void keyPressed() {
  if (boxes[randI][randJ].keyRight()) {
    light = true;
    randI = (int)random(0, cols);
    randJ = (int)random(0, rows);
    keyIndex = int(random(97, 120));
    println(keyIndex);
  }
}

"Box" class:

class Box {

  float x, y;
  color c;
  int size = 50;

  Box (int valX, int valY) {
    x = valX * size;
    y = (int) random(0, valY) * size;
  }

  void displayOn() {
    fill(c);
    rect(x, y, size, size);
    c = #b1f64d;
  }

  void displayOff() {
    fill(c);
    rect(x, y, size, size);
    c = #000000;
  }

  void rollover(float mx, float my) {
    if (mx > x && mx < x + size && my > y && my < y + size) {
      c = 126;
    }
  }

  boolean onPress(float mx, float my) {
    if (mx > x && mx < x + size && my > y && my < y + size) {
      return true;
    } else {
      return false;
    }
  }

  boolean keyRight() {
    if (key == keyIndex) {
      return true;
    } else {
      return false;
    }
  }
}

您需要 class Box:

的成员变量,而不是单个变量 boolean light = false;

<s>boolean light = false;</s>

class Box {

    boolean light = false;
    float x, y;
    color c;
    int size = 50;

    Box (int valX, int valY) {
        x = valX * size;
        y = valY * size;
    }

    .....
}

现在每个盒子对象都可以保存信息,无论是否"lighted"。

draw 函数中,您必须绘制所有框,每个框都取决于其状态
boxes[i][j].light 为此使用 2 个嵌套 for 循环:

void draw() { 

    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {

            if (boxes[i][j].light == true) {
                 boxes[i][j].rollover(mouseX, mouseY);
                 boxes[i][j].displayOn();
            } else {
                 boxes[i][j].displayOff();
            }
        }
    }
}

mousePressed 中,您必须检查状态为 boxes[i][j].light 的所有 Box 个对象,是否 mouseXmouseY 处于打开状态:

void mousePressed() {
    boolean hit = false;
    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {

        if (boxes[i][j].light == true && boxes[i][j].onPress(mouseX, mouseY)) {
            boxes[i][j].light = false;
            hit = true;
        }
      }
    }
    println(hit ? "yes" : "no");
} 

最后你必须在函数 keyPressed 中设置成员 boxes[randI][randJ].light,而不是变量 light,它不再存在:

void keyPressed() {
    if (boxes[randI][randJ].keyRight()) {
        boxes[randI][randJ].light = true;
        randI = (int)random(0, cols);
        randJ = (int)random(0, rows);
        keyIndex = int(random(97, 120));
        println(keyIndex, char(keyIndex));
    }
}