网格不返回处理3的价值3
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中的类/对象和全局变量有些混乱。
我说这是出于一些原因:
boolean mousepress = false;
是一个全局变量,可以在整个草图中访问,但是您正在访问它并从每个tile> tile
实例,重置其值:您可能是要使用Mousepress
属性 tile 。这样,每个瓷砖都具有Mousepress
而不会彼此干扰。tileclicked()
中,您正在通过 all tiles the tiles,tile
,这意味着每次都不必要地进行循环:每个瓷砖都可以检查它是自己的坐标和位置,以了解是否正在单击(不需要循环)Mousex&lt; Tilesize
和Mousey&lt; Tilesize
只能检查左上图:您可能是指Mousex&lt; x + tilesize
和mousey&lt; y + tilesize
这是您的草图的一个版本,并应用了上述注释
(以及可选的调试文本渲染以仔细检查针对瓷砖的打印值):
我了解
Mousepress
变量的意图,但我想mousepressed()
您可以使用这些功能避免de-bouncing/de-bouncing/deborce de bounconing/repenter thit此值并简化代码。这是上述草图的一个版本,使用
Mousepressed()
(并且将变量重命名为 java命名约定):虽然开头很好:继续前进!
代码的其余部分很好,恭喜使用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 eachTile
instance, resetting it's value: you probably meant to use amousePress
property forTile
. This way, each Tile has it'smousePress
without interfering with one another.tileClicked()
you're itereratting through all the tiles, fromTile
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)mouseX < tileSize
andmouseY < tileSize
will only check for the top left tile: you probably meantmouseX < x + tileSize
andmouseY < 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):
I understand the intent of the
mousePress
variable, but I'd like tomousePressed()
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):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 !
我认为您做得很好,命名功能和课程,什么不做。从这一点开始,我建议您将代码分解,以便您可以在允许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.