# Need help on beginner project (Snake)

**URL:** https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908
**Category:** Coding Questions
**Created:** [February 18, 2021, 2:36pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908 "2021-02-18T14:36:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![luckduck312](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/luckduck312/32/12135_2.png) [@luckduck312](https://discourse.processing.org/u/luckduck312)
#### Post date: [February 18, 2021, 2:36pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/1 "2021-02-18T14:36:14Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi i’m working on making snake with a 2d array. Right now i’m trying to move a single square but i can’t get it to work. When moving up or left everthing works fine but when moving the square down or to the right it dosn’t draw the differences in the squares position. Can someone please take a look at my code below and tell me what i’m doing wrong.

Here is the main tab code:

```auto
int rows= 15;
int columns= 15;
String direction = "none";

sqaure[][] sqaurematrix = new sqaure[rows][columns];

void setup() {
  size (375, 375);
  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      sqaurematrix[x][y]= new sqaure(x, y);
      println(sqaurematrix[x][y].xpos, sqaurematrix[x][y].ypos );
    }
  }
  sqaurematrix[7][7].snakehead=true;
}

void draw() {

  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {

      sqaurematrix[x][y].display();
    }
  }

  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      sqaurematrix[x][y].move();

    }
  }
}

void keyPressed() {
  if (key == 'w') {
    direction = "up";
  }
  if (key == 's') {
    direction = "down";
  }
  if (key == 'a') {
    direction = "left";
  }
  if (key == 'd') {
    direction = "right";
  }
}

```

Here is the square object code:

```auto
class sqaure {
  int xpos, ypos;
  color c = color(0, 255, 0);
  boolean snakehead = false;

  sqaure(int xpos, int ypos) {
    this.xpos = xpos;
    this.ypos = ypos;
  }

  void display() {
    if (snakehead) {
      fill(0, 0, 255);
    } else { 
      fill(c);
    }
    rect(xpos*25, ypos*25, 25, 25);
  }

  void move() {
    if (snakehead) {
      if (direction=="up" && ypos>0) {
        snakehead=false;
        sqaurematrix[xpos][ypos-1].snakehead=true;
      }
      if (direction=="down" && ypos<columns-1) {
        snakehead=false;
        sqaurematrix[xpos][ypos+1].snakehead=true;
      }
      if (direction=="right" && xpos<rows-1) {
        snakehead=false;
        sqaurematrix[xpos+1][ypos].snakehead=true;
      } 
      if (direction=="left" && xpos>0) {
        snakehead=false;
        sqaurematrix[xpos-1][ypos].snakehead=true;
      }
      delay(500);
      println(xpos,ypos);
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 18, 2021, 2:52pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/2 "2021-02-18T14:52:16Z")

</div>

> [@luckduck312](#):
>
> `direction=="left"`

better check String with equals please:

```auto
direction.equals("left") 

```

It’s `square with -ua-`

---

<div class="post-metadata">

### Author: ![luckduck312](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/luckduck312/32/12135_2.png) [@luckduck312](https://discourse.processing.org/u/luckduck312)
#### Post date: [February 18, 2021, 5:03pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/3 "2021-02-18T17:03:17Z")

</div>

Okay thanks for the help, i have chanced everything that you mentioned but my problem still occurs.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 18, 2021, 5:15pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/4 "2021-02-18T17:15:29Z")

</div>

Please post entire code again

---

<div class="post-metadata">

### Author: ![luckduck312](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/luckduck312/32/12135_2.png) [@luckduck312](https://discourse.processing.org/u/luckduck312)
#### Post date: [February 18, 2021, 5:28pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/5 "2021-02-18T17:28:03Z")

</div>

```auto
int rows= 15;
int columns= 15;
String direction = "none";

square[][] squarematrix = new square[rows][columns];

void setup() {
  size (375, 375);
  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      squarematrix[x][y]= new square(x, y);
      println(squarematrix[x][y].xpos, squarematrix[x][y].ypos );
    }
  }
  squarematrix[7][7].snakehead=true;
}

void draw() {
 println(direction);
  
  
  
  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      squarematrix[x][y].display();
    }
  }

  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      squarematrix[x][y].move();

    }
  }
}

void keyPressed() {
  if (key == 'w') {
    direction = "up";
  }
  if (key == 's') {
    direction = "down";
  }
  if (key == 'a') {
    direction = "left";
  }
  if (key == 'd') {
    direction = "right";
  }
}
class square {
  int xpos, ypos;
  color c = color(0, 255, 0);
  boolean snakehead = false;

  square(int xpos, int ypos) {
    this.xpos = xpos;
    this.ypos = ypos;
  }

  void display() {
    if (snakehead) {
      fill(0, 0, 255);
    } else { 
      fill(c);
    }
    rect(xpos*25, ypos*25, 25, 25);
  }

  void move() {
    if (snakehead) {
      if (direction.equals("up") && ypos>0) {
        snakehead=false;
        squarematrix[xpos][ypos-1].snakehead=true;
      }
      if (direction.equals("down") && ypos<columns-1) {
        snakehead=false;
        squarematrix[xpos][ypos+1].snakehead=true;
      }
      if (direction.equals("right") && xpos<rows-1) {
        snakehead=false;
        squarematrix[xpos+1][ypos].snakehead=true;
      } 
      if (direction.equals("left") && xpos>0) {
        snakehead=false;
        squarematrix[xpos-1][ypos].snakehead=true;
      }
      delay(500);
      println(xpos,ypos);
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 18, 2021, 7:28pm UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/6 "2021-02-18T19:28:29Z")

</div>

```auto

int rows= 15;
int columns= 15;
String direction = "none";

Square[][] squarematrix = new Square[rows][columns];

void setup() {
  size (375, 375);
  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      squarematrix[x][y]= new Square(x, y);
      println(squarematrix[x][y].xpos, squarematrix[x][y].ypos );
    }
  }
  squarematrix[7][7].snakehead=true;

  frameRate(9);
}

void draw() {
  println(direction);

  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {

      squarematrix[x][y].display();
    }
  }

  for (int y=0; y<columns; y++) {
    for (int x=0; x<rows; x++) {
      if (squarematrix[x][y].move())return;
    }
  }
}

void keyPressed() {
  if (key == 'w') {
    direction = "up";
  }
  if (key == 's') {
    direction = "down";
  }
  if (key == 'a') {
    direction = "left";
  }
  if (key == 'd') {
    direction = "right";
  }
}

void keyReleased() {
  //direction = "";
}

// =================================================================

class Square {
  int xpos, ypos;
  color c = color(0, 255, 0);
  boolean snakehead = false;

  Square(int xpos, int ypos) {
    this.xpos = xpos;
    this.ypos = ypos;
  }//constr

  void display() {
    if (snakehead) {
      fill(0, 0, 255);
    } else { 
      fill(c);
    }
    rect(xpos*25, ypos*25, 25, 25);
    // direction = "";
  }

  boolean move() {
    if (snakehead) {
      if (direction.equals("up") && ypos>0) {
        snakehead=false;
        squarematrix[xpos][ypos-1].snakehead=true;
      } else if (direction.equals("down") && ypos<columns-1) {
        snakehead=false;
        squarematrix[xpos][ypos+1].snakehead=true;
      } else if (direction.equals("right") && xpos<rows-1) {
        snakehead=false;
        squarematrix[xpos+1][ypos].snakehead=true;
      } else if (direction.equals("left") && xpos>0) {
        snakehead=false;
        squarematrix[xpos-1][ypos].snakehead=true;
      }
      delay(5);
      println(xpos, ypos);
      // direction = "";
      return true;
    }
    return false;
  }//method
} //class
//

```

---

<div class="post-metadata">

### Author: ![luckduck312](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/luckduck312/32/12135_2.png) [@luckduck312](https://discourse.processing.org/u/luckduck312)
#### Post date: [February 19, 2021, 12:02am UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/7 "2021-02-19T00:02:14Z")

</div>

Can you be so kind and explain to me why my code didn’t work and why it behaved different when moving up compared to moving down.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 19, 2021, 8:13am UTC](https://discourse.processing.org/t/need-help-on-beginner-project-snake/27908/8 "2021-02-19T08:13:12Z")

</div>

Good question.

It’s a bit tricky.

As you can see, the function `move()` is called again and again for every cell, searching the cell with the snake head and moving it. But when we moved the snake head, we don’t store the information that we are done.

Instead the nested for-loop goes on. What happens now (when we clicked down or right) is that it finds the next cell with (the newly placed) `snakehead` being true in `if (snakehead) {`. So the snake head is moved again. And then again. So it falls down (or to the right).  
Because of the search direction is right and down in the nested for loop, this happens.

BUT when the movement was up or left, the snake head is moved once but because the search direction is down, the moved snake head is not found nor moved again.

**My solution**

I told the function `move() ` to return true once it found something and otherwise false.

And in draw() in the nested for-loop I said

```auto
if (squarematrix[x][y].move())return;

```

which means it leaves the nested for-loop (and the function). Hence, we don’t move the snake head again.

Warm regards,

Chrisir
