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

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


问题似乎是 mousePressed 代码与数组中最后一次着色的任何元素相关联,但我无法弄清楚如何使其与数组中已着色的任何元素进行交互。甚至有可能吗?我考虑过更改数组,以便对数组本身中每个元素的位置进行打乱,但它创建的形状仍然排列在网格中,然后每次单击鼠标都会向后迭代。但是我似乎无法弄清楚如何在不进入我不熟悉的 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);

  }

}

“盒子”类:


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;

    }

  }

}


撒科打诨
浏览 99回答 1
1回答

交互式爱情

boolean light = false;您需要一个类的成员变量,而不是单个变量Box:class Box {&nbsp; &nbsp; boolean light = false;&nbsp; &nbsp; float x, y;&nbsp; &nbsp; color c;&nbsp; &nbsp; int size = 50;&nbsp; &nbsp; Box (int valX, int valY) {&nbsp; &nbsp; &nbsp; &nbsp; x = valX * size;&nbsp; &nbsp; &nbsp; &nbsp; y = valY * size;&nbsp; &nbsp; }&nbsp; &nbsp; .....}现在每个盒子对象都可以保存信息,无论它是否“点亮”。在draw函数中,您必须绘制所有框,每个框都依赖于其状态,为此boxes[i][j].light使用 2 个嵌套for循环:void draw() {&nbsp;&nbsp; &nbsp; for (int i = 0; i < cols; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < rows; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (boxes[i][j].light == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boxes[i][j].rollover(mouseX, mouseY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boxes[i][j].displayOn();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boxes[i][j].displayOff();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在mousePressed你必须检查所有Box带有 state 的对象boxes[i][j].light,是否mouseX和mouseY是:void mousePressed() {&nbsp; &nbsp; boolean hit = false;&nbsp; &nbsp; for (int i = 0; i < cols; i++) {&nbsp; &nbsp; &nbsp; for (int j = 0; j < rows; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (boxes[i][j].light == true && boxes[i][j].onPress(mouseX, mouseY)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boxes[i][j].light = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hit = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; println(hit ? "yes" : "no");}&nbsp;最后,您必须boxes[randI][randJ].light在函数中设置成员keyPressed,而不是变量light,后者不再存在:void keyPressed() {&nbsp; &nbsp; if (boxes[randI][randJ].keyRight()) {&nbsp; &nbsp; &nbsp; &nbsp; boxes[randI][randJ].light = true;&nbsp; &nbsp; &nbsp; &nbsp; randI = (int)random(0, cols);&nbsp; &nbsp; &nbsp; &nbsp; randJ = (int)random(0, rows);&nbsp; &nbsp; &nbsp; &nbsp; keyIndex = int(random(97, 120));&nbsp; &nbsp; &nbsp; &nbsp; println(keyIndex, char(keyIndex));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java