# How to mouseclicked()

**URL:** <https://discourse.processing.org/t/how-to-mouseclicked/23316>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [August 17, 2020, 9:14pm UTC](https://discourse.processing.org/t/how-to-mouseclicked/23316 "2020-08-17T21:14:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greenknight71](https://avatars.discourse-cdn.com/v4/letter/g/3d9bf3/32.png) [@greenknight71](https://discourse.processing.org/u/greenknight71)\
**Post date:** [August 17, 2020, 9:14pm UTC](https://discourse.processing.org/t/how-to-mouseclicked/23316/1 "2020-08-17T21:14:03Z")

</div>

How does mouseClicked() work? Im trying to make it so that when a mouse is clicked, it ends.

```
void mouseClicked(){
   if(shot){
     end();
   }
   else{
     return;
   }
}
```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 18, 2020, 5:36pm UTC](https://discourse.processing.org/t/how-to-mouseclicked/23316/2 "2020-08-18T17:36:15Z")

</div>

Do you have to use 1D arrays? A 2D array would be a much better option!

---

<div class="post-metadata">

**Author:** ![Impossible\_code](https://avatars.discourse-cdn.com/v4/letter/i/7993a0/32.png) [@Impossible\_code](https://discourse.processing.org/u/Impossible_code)\
**Post date:** [August 19, 2020, 3:36am UTC](https://discourse.processing.org/t/how-to-mouseclicked/23316/3 "2020-08-19T03:36:50Z")

</div>

You were pretty close. Maybe try this…

```auto
int CELLSIZE = 50; //size each square

final int NUM_ROWS = 10; //rows * CELLSIZE = height 

final int NUM_COLS = 12; //columns * CELLSIZE = width

int[] shipX = new int[NUM_ROWS]; //ship rows

int[] shipY = new int[NUM_COLS]; //ship columns

int shipLength; //size ship

int[] shipHitX = new int[NUM_ROWS]; //ship hit rows

int[] shipHitY = new int[NUM_COLS]; //ship hit columns

void setup() {

  size(600, 500); //size MUST be (NUM_COLS*CELLSIZE) by (NUM_ROWS*CELLSIZE);

  setupGrid();

  randomShip(); // generate a random ship...once

  drawGrid();

}

void draw() {

}

void setupGrid(){

  for(int x=0; x<NUM_COLS; x++)

  {

    for(int y=0; y<NUM_ROWS; y++)

    {

        shipX[x] = -1;

        shipY[y] = -1;

    }

  }

}

void drawGrid(){

  for(int x=0; x<NUM_COLS; x++)

  {

    for(int y=0; y<NUM_ROWS; y++)

    {

        int shipRowX = x*CELLSIZE;

        int shipRowY = y*CELLSIZE;

        fill(0,0,255);

        if(shipX[x]>-1 && shipY[y]>-1){

        fill(127,0,0); //for debugging, shows the location of the ship...

        }

        rect(shipRowX,shipRowY,CELLSIZE,CELLSIZE); 

    }

  }

}

void randomShip(){

  int direction = (int)random(-10,10);  

  shipLength = (int)random (3, 8); //random ship size

 

   if(direction >= 0) //horizontal ship

  {

    startX = int(random(NUM_COLS-shipLength)); //random starting column

    startY = int(random(NUM_ROWS)); //random row

    for(int a = 0; a < shipLength; a++)

    {

      shipX[startX+a] = 1; 

      shipY[startY] = 1;

    }

  }

else //vertical

  {

    startY = int(random(NUM_ROWS-shipLength)); //random starting row

    startX = int(random(NUM_COLS)); //random column

    for(int a = 0; a < shipLength; a++)

    {

    shipX[startX] = 1; 

    shipY[startY+a] = 1;

    }

  }

}

void mouseClicked(){

  int mouseShipX = int(mouseX / CELLSIZE);

  int mouseShipY = int(mouseY / CELLSIZE);

 

  fill(127); //fill in grey if missed

  if (shipX[mouseShipX] > 0 && shipY[mouseShipY] > 0) {

  //HIT..

  fill(255,0,255);

}

  rect(mouseShipX*CELLSIZE,mouseShipY*CELLSIZE,CELLSIZE,CELLSIZE); 

    //shipHitX[x]=shipY[x];

    //shipHitY[y]=shipX[y];

}

```
