如何使用mousePressed与2D数组

时间:2018-12-18 13:42:30

标签: processing

一般来说,我是处理和编程的新手,但在使mousePressed函数具有所需效果方面遇到问题。我希望获得一些帮助或建议,以使其按预期工作。

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;
}

基本上,我想单击一个正方形以检查条件并更改正方形的颜色,类似于“战舰”游戏的形式。感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:2)

mouseX / mouseY是鼠标的像素X / y位置。要确定它在哪个正方形上,必须将像素位置除以正方形的宽度/高度。将此用作2D数组的参数以找到正确的正方形。然后,您必须检查/将更改的值分配给该正方形。

类似这样的东西:

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??)
  } 
}

在下一帧中,正方形将具有新的颜色。