Processing 如何在2D数组中使用鼠标按下
一般来说,我对处理和编程还不熟悉,我在让鼠标按下的函数达到预期效果方面遇到了一些问题。我希望能得到一些帮助或建议,让它按预期工作Processing 如何在2D数组中使用鼠标按下,processing,Processing,一般来说,我对处理和编程还不熟悉,我在让鼠标按下的函数达到预期效果方面遇到了一些问题。我希望能得到一些帮助或建议,让它按预期工作 int[][] cave; int caveSize = 50; int PILLAR = 1; int FIX = -1; int EMPTY = -2; int MINE = 2; color pillar = color(255, 255, 255); color mine = color(255, 0, 0); color fix = c
int[][] cave;
int caveSize = 50;
int PILLAR = 1;
int FIX = -1;
int EMPTY = -2;
int MINE = 2;
color pillar = color(255, 255, 255);
color mine = color(255, 0, 0);
color fix = color(0, 255, 0);
color empty = color(0, 0, 0);
boolean end = false;
void setup() {
size(602, 352);
stroke(100);
noSmooth();
cave = new int[width/caveSize][height/caveSize];
for(int x=0; x < width/caveSize; x++) {
for(int y=0; y< height/caveSize; y++) {
cave[x][y] = 0;
}
}
placePoints();
}
void draw() {
background(color(0, 0, 0));
for (int x=0; x < width/caveSize; x++) {
for (int y=0; y < height/caveSize; y++) {
if(cave[x][y] == PILLAR) {
fill(pillar);
}
if(cave[x][y] == MINE) {
fill(mine);
}
if (cave[x][y] == FIX) {
fill(fix);
}
if (cave[x][y] == EMPTY) {
fill(empty);
}
rect(x*caveSize, y*caveSize, caveSize, caveSize);
noFill();
}
}
if (end) {
textSize(50);
fill(255);
text("GAME OVER!", width/4, height/2);
noFill();
}
}
void mousePressed()
{
// int x = mouseX/8;
// int y = mouseY/8;
if(mouseX == PILLAR && mouseY == PILLAR)
{
fill(fix);
}
if(mouseX == MINE && mouseY == MINE)
{
fill(mine);
}
else
{
}
}
boolean game_over() {
for(int x=0; x < width/caveSize; x++) {
for(int y=0; y< height/caveSize; y++) {
if (cave[x][y] == PILLAR || cave[x][y] == MINE) {
return false;
}
}
}
end = true;
return true;
}
void end() {
if (game_over()) {
return;
}
}
void placePoints() {
cave[3][1] = MINE;
cave[7][2] = MINE;
cave[2][3] = MINE;
cave[9][4] = MINE;
cave[1][5] = MINE;
cave[6][4] = MINE;
cave[4][5] = MINE;
cave[6][3] = MINE;
cave[5][5] = MINE;
cave[7][6] = MINE;
cave[0][2] = MINE;
cave[1][0] = MINE;
cave[10][1] = MINE;
cave[11][3] = MINE;
cave[11][6] = MINE;
cave[6][4] = MINE;
cave[4][5] = MINE;
cave[6][3] = MINE;
cave[5][5] = MINE;
cave[7][6] = MINE;
cave[5][6] = PILLAR;
cave[6][6] = PILLAR;
cave[3][6] = PILLAR;
cave[4][6] = PILLAR;
cave[5][2] = PILLAR;
cave[5][3] = PILLAR;
cave[5][4] = PILLAR;
cave[0][0] = PILLAR;
cave[0][1] = PILLAR;
cave[11][2] = PILLAR;
cave[10][2] = PILLAR;
cave[9][2] = PILLAR;
}
int[][]洞穴;
int caveSize=50;
int支柱=1;
int FIX=-1;
int EMPTY=-2;
int MINE=2;
颜色柱=颜色(255、255、255);
颜色矿=颜色(255,0,0);
颜色固定=颜色(0,255,0);
颜色空=颜色(0,0,0);
布尔结束=假;
无效设置(){
规模(602352);
中风(100);
noSmooth();
cave=新整数[宽度/凹陷][高度/凹陷];
对于(int x=0;x
基本上,我想点击一个方块来检查条件并改变方块的颜色,类似于一种“战舰”游戏。感谢您的时间。mouseX/mouseY是鼠标的像素X/y位置。要确定它位于哪个正方形上,必须将像素位置除以正方形的宽度/高度。将其用作二维阵列的参数以查找正确的正方形。然后,您必须检查/将更改后的值分配给该正方形 大概是这样的:
void mousePressed()
{
int x = mouseX/caveSize;
int y = mouseY/caveSize;
if (cave[x][y] == PILLAR) //if the value is PILLAR (=1)
{
cave[x][y] = FIX; //set the value to FIX (=-1)
}
if (cave[x][y] == MINE) //if the value is MINE (=2)
{
cave[x][y] = MINE; //set the value to MINE (=2) (the same??)
}
}
在下一帧上,正方形将有新的颜色