# Creating a moving snake on a predefined grid

**URL:** https://discourse.processing.org/t/creating-a-moving-snake-on-a-predefined-grid/16854
**Category:** Coding Questions
**Created:** [January 4, 2020, 11:06am UTC](https://discourse.processing.org/t/creating-a-moving-snake-on-a-predefined-grid/16854 "2020-01-04T11:06:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Reynaert98](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reynaert98/32/7580_2.png) [@Reynaert98](https://discourse.processing.org/u/Reynaert98)
#### Post date: [January 4, 2020, 11:06am UTC](https://discourse.processing.org/t/creating-a-moving-snake-on-a-predefined-grid/16854/1 "2020-01-04T11:06:34Z")

</div>

I’m working on a game on which a snake/ centipede can move on a grid by mouseclicking on grid spaces next to it. Its body consists of 10 pieces which all follow the previous locations of its head. My question is, where do I start? I have several ideas but am quite clueless on where to start or if it will work at all. My idea was to run it through a for loop which generates the body parts then run it through an if loop that would check the clicked location and create a new rectangle on the specific grid. I assume there has to be another way of doing it with other functions available. I checked the API but couldn’t quite find something. Any help?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 4, 2020, 11:12am UTC](https://discourse.processing.org/t/creating-a-moving-snake-on-a-predefined-grid/16854/2 "2020-01-04T11:12:00Z")

</div>

> [@Reynaert98](#):
>
> I checked the API but couldn’t quite find something

? what you searched for ?

> **[Array / Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

or

> **[class / Reference](https://processing.org/reference/class.html)**
>
> Keyword used to indicate the declaration of a class. A class is a composite of fields (data) and methods (functions that are a part of the class) which may be instantiated as objects. The first lette…

* * *

please show us your code up to now

- grid logic
- mouseclick? and on NEXT logic
- create rect on that location

---

<div class="post-metadata">

### Author: ![Reynaert98](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reynaert98/32/7580_2.png) [@Reynaert98](https://discourse.processing.org/u/Reynaert98)
#### Post date: [January 4, 2020, 11:30am UTC](https://discourse.processing.org/t/creating-a-moving-snake-on-a-predefined-grid/16854/3 "2020-01-04T11:30:04Z")

</div>

As of now, a grid which also includes a mouseclicked function. There’s objects hidden on the grid which the snake has to collect for points.

```auto
int score = 0;
int[][] items = new int[30][30];

void placeFruitInit() {
  
  for ( int i = 0; i < 30; i++) {
    for ( int j = 0; j < 30; j++) {
      
      items[i][j] = int(random(-5, -1));
      
    }
  }
};

void placeFruit() {
  
  for ( int i = 0; i < 30; i++) {
    for ( int j = 0; j < 30; j++) {
      
      int t = items[i][j];
      
      if ( t < 0 ) {
        
        fill(128);
        stroke(0);
        rect(30*i, 30*j, 30, 30);
        
      } else if ( t == 2 ) {
        
        fill(255, 0, 0);
        stroke(0);
        rect(30*i+2, 30*j+2, 26, 26);
        
      } else if ( t == 3 ) {
        
        fill(255, 255, 0);
        stroke(0);
        rect(30*i+2, 30*j+2, 26, 26);
        
      } else if ( t == 4 ) {
        
        fill(0, 255, 0);
        stroke(0);
        rect(30*i+2, 30*j+2, 26, 26);
          
      }      
    }
  }
};

```

placeFruitInit() is called in setup. Rest in draw.

Method for mouseClicked()

```auto
void mouseClickGrid() {
  
  int x = int( mouseX / 30 );
  int y = int( mouseY / 30 );
  
  if ( x >= 0 && y >= 0 && x < 30 && y < 30 ) {
    
    items[x][y] = abs(items[x][y]);

  }
  
  println(score);

};

```
