网格不返回处理3的价值3

发布于 2025-01-27 19:47:56 字数 2177 浏览 1 评论 0原文

我正在尝试使用Processing的Java制作一个扫雷克隆。我设法创建了一个网格,每个瓷砖都有一个随机值分配给它,但是由于某种原因,只能单击左上图块,其余图块不会返回值。如果有人知道我的代码有什么问题,或者我可以尝试的事情会很棒,谢谢

这是我的Java代码:

int realWidth = 500;
int realHeight = 500;
int tilemount = 0;

boolean mousePress = false;

//calls class
Tile[] tile = new Tile[100];
float tileSize = realWidth/10;

//sets size of window
void settings() {
  size(realWidth, realHeight);
}

//Draws 100 tiles on the grid and assignemts each with a value from -1 - 9
void setup() {
  int x = 0;
  int y = 0;
  for (int i = 0; i < tile.length; i++) {
    tile[i] = new Tile(x, y, int(random(-1,9)), tilemount);
    x += tileSize;
    if (x > realWidth-tileSize) {
      x = 0;
      y += tileSize;
    }
    tilemount ++;
  }
  print("done");
}

//updates each tile in the list
void draw() {
  background(50);
  for (int i = 0; i < tile.length; i++) {
    tile[i].display();
    tile[i].tileClicked();
  }
  //checks if tiles are clicked
  clickCheck();
}



//sets up tile class
class Tile {
  int x;
  int y;
  int value;
  int tilemount;
  
  Tile(int x, int y, int value, int tilemount) {
    this.x = x;
    this.y = y;
    this.value = value;
    this.tilemount = tilemount;
  } 
  
  //positions the tile based off of display values
  void display() {
    rect(x, y, tileSize, tileSize);
  }
  
  //the problem:
  
  
  /*if tile is clicked, it should run through all 100 tiles in the list, 
  detect if the mouse is inside the x value assinged to each tile, and produce the 
  value of the tile the mouse is currently inside */
  
  void tileClicked() {
    if (mousePressed && mousePress == false) {
      mousePress = true;
      for (int i = 0; i < tile.length; i++) {
        //println(tile[i].x, tile[i].y);
        //println(tile[2].x, tile[2].y, mouseX, mouseY);
        if (mouseX > tile[i].x && mouseX < tileSize && mouseY > tile[i].y  && mouseY < tileSize) {
          println(i, tile[i].value);
          break;
        }
      }
    } 
  }
}

//Checks if mouse is clicked
void clickCheck() {
  if (!mousePressed) {
    mousePress = false;
  }
}

I'm trying to make a minesweeper clone using Processing's java. I have managed to create a grid and each tile has a randomized value assigned to it, but for some reason, only the top left tile can be clicked, the rest of the tiles do not return a value. If anyone knows whats wrong with my code or what I could try that would be great, thanks

Here's my Java code:

int realWidth = 500;
int realHeight = 500;
int tilemount = 0;

boolean mousePress = false;

//calls class
Tile[] tile = new Tile[100];
float tileSize = realWidth/10;

//sets size of window
void settings() {
  size(realWidth, realHeight);
}

//Draws 100 tiles on the grid and assignemts each with a value from -1 - 9
void setup() {
  int x = 0;
  int y = 0;
  for (int i = 0; i < tile.length; i++) {
    tile[i] = new Tile(x, y, int(random(-1,9)), tilemount);
    x += tileSize;
    if (x > realWidth-tileSize) {
      x = 0;
      y += tileSize;
    }
    tilemount ++;
  }
  print("done");
}

//updates each tile in the list
void draw() {
  background(50);
  for (int i = 0; i < tile.length; i++) {
    tile[i].display();
    tile[i].tileClicked();
  }
  //checks if tiles are clicked
  clickCheck();
}



//sets up tile class
class Tile {
  int x;
  int y;
  int value;
  int tilemount;
  
  Tile(int x, int y, int value, int tilemount) {
    this.x = x;
    this.y = y;
    this.value = value;
    this.tilemount = tilemount;
  } 
  
  //positions the tile based off of display values
  void display() {
    rect(x, y, tileSize, tileSize);
  }
  
  //the problem:
  
  
  /*if tile is clicked, it should run through all 100 tiles in the list, 
  detect if the mouse is inside the x value assinged to each tile, and produce the 
  value of the tile the mouse is currently inside */
  
  void tileClicked() {
    if (mousePressed && mousePress == false) {
      mousePress = true;
      for (int i = 0; i < tile.length; i++) {
        //println(tile[i].x, tile[i].y);
        //println(tile[2].x, tile[2].y, mouseX, mouseY);
        if (mouseX > tile[i].x && mouseX < tileSize && mouseY > tile[i].y  && mouseY < tileSize) {
          println(i, tile[i].value);
          break;
        }
      }
    } 
  }
}

//Checks if mouse is clicked
void clickCheck() {
  if (!mousePressed) {
    mousePress = false;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御弟哥哥 2025-02-03 19:47:56

您的代码中的类/对象和全局变量有些混乱。

我说这是出于一些原因:

  • boolean mousepress = false;是一个全局变量,可以在整个草图中访问,但是您正在访问它并从每个tile> tile实例,重置其值:您可能是要使用Mousepress属性 tile 。这样,每个瓷砖都具有Mousepress而不会彼此干扰。
  • tileclicked()中,您正在通过 all tiles the tiles,tile,这意味着每次都不必要地进行循环:每个瓷砖都可以检查它是自己的坐标和位置,以了解是否正在单击(不需要循环)
  • 检查界限,检查是否Mousex&lt; TilesizeMousey&lt; Tilesize只能检查左上图:您可能是指Mousex&lt; x + tilesizemousey&lt; y + tilesize

这是您的草图的一个版本,并应用了上述注释
(以及可选的调试文本渲染以仔细检查针对瓷砖的打印值):


    int realWidth = 500;
    int realHeight = 500;
    int tilemount = 0;
    
    //calls class
    Tile[] tile = new Tile[100];
    float tileSize = realWidth/10;
    
    //sets size of window
    void settings() {
      size(realWidth, realHeight);
    }
    
    //Draws 100 tiles on the grid and assignemts each with a value from -1 - 9
    void setup() {
      int x = 0;
      int y = 0;
      for (int i = 0; i < tile.length; i++) {
        tile[i] = new Tile(x, y, int(random(-1,9)), tilemount);
        x += tileSize;
        if (x > realWidth-tileSize) {
          x = 0;
          y += tileSize;
        }
        tilemount ++;
      }
      println("done");
    }
    
    //updates each tile in the list
    void draw() {
      background(50);
      for (int i = 0; i < tile.length; i++) {
        tile[i].display();
        tile[i].tileClicked();
        //checks if tiles are clicked
        tile[i].clickCheck();
      }
    }
    
    
    
    //sets up tile class
    class Tile {
      int x;
      int y;
      int value;
      int tilemount;
      
      boolean mousePress = false;
      
      Tile(int x, int y, int value, int tilemount) {
        this.x = x;
        this.y = y;
        this.value = value;
        this.tilemount = tilemount;
      } 
      
      //positions the tile based off of display values
      void display() {
        fill(255);
        rect(x, y, tileSize, tileSize);
        fill(0);
        text(value,x + tileSize * 0.5, y + tileSize * 0.5);
      }
      
      //the problem:
      
      
      /*if tile is clicked, it should run through all 100 tiles in the list, 
      detect if the mouse is inside the x value assinged to each tile, and produce the 
      value of the tile the mouse is currently inside */
      
      void tileClicked() {
        if (mousePressed && mousePress == false) {
          mousePress = true;
          //println(tile[2].x, tile[2].y, mouseX, mouseY);
          if (mouseX > x && mouseX < x + tileSize && mouseY > y  && mouseY < y + tileSize) {
            println(value);
          }
        } 
      }
      //Checks if mouse is clicked
      void clickCheck() {
        if (!mousePressed) {
          mousePress = false;
        }
      }
    }

我了解Mousepress变量的意图,但我想 mousepressed() 您可以使用这些功能避免de-bouncing/de-bouncing/deborce de bounconing/repenter thit此值并简化代码。

这是上述草图的一个版本,使用Mousepressed()(并且将变量重命名为 java命名约定):

int realWidth = 500;
int realHeight = 500;
int tileCount = 0;

//calls class
Tile[] tiles = new Tile[100];
float tileSize = realWidth/10;

//sets size of window
void settings() {
  size(realWidth, realHeight);
}

//Draws 100 tiles on the grid and assignemts each with a value from -1 - 9
void setup() {
  int x = 0;
  int y = 0;
  for (int i = 0; i < tiles.length; i++) {
    tiles[i] = new Tile(x, y, int(random(-1,9)), tileCount);
    x += tileSize;
    if (x > realWidth-tileSize) {
      x = 0;
      y += tileSize;
    }
    tileCount ++;
  }
  println("done");
}

//updates each tile in the list
void draw() {
  background(50);
  for (int i = 0; i < tiles.length; i++) {
    tiles[i].display();
  }
}

void mousePressed(){
  for (int i = 0; i < tiles.length; i++) {
    tiles[i].click();
  }
}



//sets up tile class
class Tile {
  int x;
  int y;
  int value;
  int index;
  
  Tile(int x, int y, int value, int tileCount) {
    this.x = x;
    this.y = y;
    this.value = value;
    this.index = tileCount;
  } 
  
  //positions the tile based off of display values
  void display() {
    fill(255);
    rect(x, y, tileSize, tileSize);
    // optional: display the tile value for debugging purposes
    fill(0);
    text(value, x + tileSize * 0.5, y + tileSize * 0.5);
  }
  
  //the problem:
  /*if tile is clicked, it should detect if the mouse is inside the
  value assinged to each tile, and produce the 
  value of the tile the mouse is currently inside */
  
  void click() {
    if (mouseX > x && mouseX < x + tileSize && mouseY > y  && mouseY < y + tileSize) {
      println("index", index, "value", value);
    }
  }
}

虽然开头很好:继续前进!
代码的其余部分很好,恭喜使用1D数组进行2D网格布局(以及用于重置每一行X位置的逻辑)。学习有趣!

There is a bit of confusion over classes/objects and global variables in your code.

I say that for a few reasons:

  • boolean mousePress = false; is a global variable, accessible throughout the sketch, however you are accessing it and changing it from each Tile instance, resetting it's value: you probably meant to use a mousePress property for Tile. This way, each Tile has it's mousePress without interfering with one another.
  • in tileClicked() you're itereratting through all the tiles, from Tile which means unecessarily doing the loop for each time: each tile can check it's own coordinates and position to know if it's being clicked or not (no need for the loop)
  • speaking of checking bounds, checking if mouseX < tileSize and mouseY < tileSize will only check for the top left tile: you probably meant mouseX < x + tileSize and mouseY < y + tileSize

Here's a version of your sketch with the above notes applied
(and optional debug text rendering to double check the printed value against the tile):


    int realWidth = 500;
    int realHeight = 500;
    int tilemount = 0;
    
    //calls class
    Tile[] tile = new Tile[100];
    float tileSize = realWidth/10;
    
    //sets size of window
    void settings() {
      size(realWidth, realHeight);
    }
    
    //Draws 100 tiles on the grid and assignemts each with a value from -1 - 9
    void setup() {
      int x = 0;
      int y = 0;
      for (int i = 0; i < tile.length; i++) {
        tile[i] = new Tile(x, y, int(random(-1,9)), tilemount);
        x += tileSize;
        if (x > realWidth-tileSize) {
          x = 0;
          y += tileSize;
        }
        tilemount ++;
      }
      println("done");
    }
    
    //updates each tile in the list
    void draw() {
      background(50);
      for (int i = 0; i < tile.length; i++) {
        tile[i].display();
        tile[i].tileClicked();
        //checks if tiles are clicked
        tile[i].clickCheck();
      }
    }
    
    
    
    //sets up tile class
    class Tile {
      int x;
      int y;
      int value;
      int tilemount;
      
      boolean mousePress = false;
      
      Tile(int x, int y, int value, int tilemount) {
        this.x = x;
        this.y = y;
        this.value = value;
        this.tilemount = tilemount;
      } 
      
      //positions the tile based off of display values
      void display() {
        fill(255);
        rect(x, y, tileSize, tileSize);
        fill(0);
        text(value,x + tileSize * 0.5, y + tileSize * 0.5);
      }
      
      //the problem:
      
      
      /*if tile is clicked, it should run through all 100 tiles in the list, 
      detect if the mouse is inside the x value assinged to each tile, and produce the 
      value of the tile the mouse is currently inside */
      
      void tileClicked() {
        if (mousePressed && mousePress == false) {
          mousePress = true;
          //println(tile[2].x, tile[2].y, mouseX, mouseY);
          if (mouseX > x && mouseX < x + tileSize && mouseY > y  && mouseY < y + tileSize) {
            println(value);
          }
        } 
      }
      //Checks if mouse is clicked
      void clickCheck() {
        if (!mousePressed) {
          mousePress = false;
        }
      }
    }

I understand the intent of the mousePress variable, but I'd like to mousePressed() function which you can use to avoid de-bouncing/resetting this value and simplify code a bit.

Here's a version of the above sketch using mousePressed() (and renaming variables a touch to be in line with Java naming conventions):

int realWidth = 500;
int realHeight = 500;
int tileCount = 0;

//calls class
Tile[] tiles = new Tile[100];
float tileSize = realWidth/10;

//sets size of window
void settings() {
  size(realWidth, realHeight);
}

//Draws 100 tiles on the grid and assignemts each with a value from -1 - 9
void setup() {
  int x = 0;
  int y = 0;
  for (int i = 0; i < tiles.length; i++) {
    tiles[i] = new Tile(x, y, int(random(-1,9)), tileCount);
    x += tileSize;
    if (x > realWidth-tileSize) {
      x = 0;
      y += tileSize;
    }
    tileCount ++;
  }
  println("done");
}

//updates each tile in the list
void draw() {
  background(50);
  for (int i = 0; i < tiles.length; i++) {
    tiles[i].display();
  }
}

void mousePressed(){
  for (int i = 0; i < tiles.length; i++) {
    tiles[i].click();
  }
}



//sets up tile class
class Tile {
  int x;
  int y;
  int value;
  int index;
  
  Tile(int x, int y, int value, int tileCount) {
    this.x = x;
    this.y = y;
    this.value = value;
    this.index = tileCount;
  } 
  
  //positions the tile based off of display values
  void display() {
    fill(255);
    rect(x, y, tileSize, tileSize);
    // optional: display the tile value for debugging purposes
    fill(0);
    text(value, x + tileSize * 0.5, y + tileSize * 0.5);
  }
  
  //the problem:
  /*if tile is clicked, it should detect if the mouse is inside the
  value assinged to each tile, and produce the 
  value of the tile the mouse is currently inside */
  
  void click() {
    if (mouseX > x && mouseX < x + tileSize && mouseY > y  && mouseY < y + tileSize) {
      println("index", index, "value", value);
    }
  }
}

Good start though: keep going!
The rest of the code is good, congrats on using a 1D array for a 2D grid layout (and the logic for resetting the x position on each row). Have fun learning !

无声静候 2025-02-03 19:47:56

我认为您做得很好,命名功能和课程,什么不做。从这一点开始,我建议您将代码分解,以便您可以在允许TileLegenth之前至少3个图块功能,在允许TileLength之前完全发挥作用(对于100英尺)。

因此,从我看到的内容来看:

+您在代码末尾有3个撇号,需要删除。

+您使用tile.length,但从未在类中定义长度变量,因此,当您的循环运行时,它没有用于确定其周期数的数字。

+请借助一个真相/逻辑表: https://en.wikipedia.org/wiki/truth_table 。我对此并不深刻地看待这一点,但这看起来像是一个误解可能会引发的领域。

尝试添加this.length = 3;在您的瓷砖类中,然后重新运行代码,以查看是否弹出更多瓷砖。
并且最好使用某个系统。只是一些基本调试。

祝你好运。

I think you did a good job naming functions and classes and what not. From this point, I would recommend breaking your code down so that you can make sure that at least 3 tiles function fully before allowing the tilelength to be something like 100 (for 100 tiles).

so from what I can see:

+ You have 3 apostrophes at the end of your code that need to be removed.

+ you use tile.length but never defined a length variable in your class, so when your for loop runs it has no number to use to determine it's number of cycles.

+ Please double check your logic in the tileClicked function with the help of a truth/logic table: https://en.wikipedia.org/wiki/Truth_table . I didn't look too deeply into this but this looks like an area where one misunderstanding could throw ;

Try adding this.length = 3; in your tile class and then re-run your code to see if more tiles pop up.
And it might be good to use some system.out.println calls to print conditional values out so that you can see the intermeediate results and compare them to what you expect to happen. Just some basic debugging.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文