# Snake Game problem: having trouble figuring out how to make it reset

**URL:** <https://discourse.processing.org/t/snake-game-problem-having-trouble-figuring-out-how-to-make-it-reset/6005>\
**Category:** Coding Questions\
**Created:** [November 27, 2018, 6:54pm UTC](https://discourse.processing.org/t/snake-game-problem-having-trouble-figuring-out-how-to-make-it-reset/6005 "2018-11-27T18:54:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blankbleak](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@blankbleak](https://discourse.processing.org/u/blankbleak)\
**Post date:** [November 27, 2018, 6:54pm UTC](https://discourse.processing.org/t/snake-game-problem-having-trouble-figuring-out-how-to-make-it-reset/6005/1 "2018-11-27T18:54:38Z")

</div>

Hello, I have just started a snake game project for my class, however I’m a little confused with partial arrays in the sense that I am unsure about how to make it start in the top middle of the canvas, I am just confused as to how to make the array start in that place. Here is my code, I would really appreciate some help 🙂

```auto
final int ROWS=20, COLS=15, SQ_SIZE=40, DIAMETER = 20;
final int MAX_SIZE = 70;
/*state variables go here*/
int currentLength = 0;
//if debugging is needed, switch the place of this variable.
int startingLength;
/*global array variables go here*/
int [] currentHeadX = new int[MAX_SIZE];
int [] currentHeadY = new int[MAX_SIZE];
//will store up to the maximum circles that can fit in the canvas circles.
//int [] currentYPosition = new int[MAX_SIZE];
//Also known as the "size" variable.

void setup(){
  //20*40, 15*40
 size(800,600);
 //controls the length of the snake at the start of each level, if debugging is needed, switch the place of this
 
}
void draw(){
 resetSnake();
  
}
/*draws the circles that form the snake*/
void drawCircles(int[] x, int[] y, int n, int colour){
for(int i=0; i<n; i++){
  fill(colour);
  ellipse(x[i],y[i],DIAMETER,DIAMETER);
 } 
}
/*fills the array with data*/
void fillArray(int[] a, int n, int start, int delta){
  for(int i=0; i<n; i++){
    a[i] = start+(delta*i);
  }
}
/*resets the snake in the proper position*/
void resetSnake(){
  startingLength=5;
  //starting length is 5
   currentLength = startingLength;
   //sets the starting length to the current length.
  for(int i=0; i<currentLength;i++){
 fillArray(currentHeadX,currentLength*i,startingLength,5);
 //where im confused, need the snake to start in the middle of the canvas, and go down vertically.
 fillArray(currentHeadY,currentLength*i,startingLength,5);
 drawCircles(currentHeadX,currentHeadY,5,#FFFF00);
 //draws the snake.
  }
}

```

---

<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:** [November 27, 2018, 11:21pm UTC](https://discourse.processing.org/t/snake-game-problem-having-trouble-figuring-out-how-to-make-it-reset/6005/2 "2018-11-27T23:21:58Z")

</div>

> [@blankbleak](#):
>
> ```auto
> startingLength=5;
> //starting length is 5
> 
> ```

Pet peeve: Don’t do this. Code that is self-explanatory doesn’t need useless comments.

* * *

Your code has some real problems. For a start, what are you doing inside `draw()`?  
Normally, people making a snake game would clear the background in `draw()`, and then draw the snake. But you aren’t doing either of these things! Instead, your `draw()` function is… resetting the snake?!?

This isn’t the sort of things we can help fix. It’s a glaring logical issue. Did you develop this code step by step?

* * *

I recently did snake. I suggest you go have a look over here:

> [@Problems with understanding language](https://discourse.processing.org/t/problems-with-understanding-language/3016/4):
>
> So why doesn’t the tail grow now? Well, it never did, did it? The problem is that append() returns an array - it doesn’t assign that array to anything. Basically, whoever wrote this didn’t use append() properly. There are several other problems with this code as well. But I think the biggest problem, really, is that you ALREADY HAD this code. The best thing to do would be to take this code as an example and write a BETTER VERSION yourself, from scratch. So let’s do that. Start with an empty s…

---

<div class="post-metadata">

**Author:** ![blankbleak](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@blankbleak](https://discourse.processing.org/u/blankbleak)\
**Post date:** [November 27, 2018, 11:48pm UTC](https://discourse.processing.org/t/snake-game-problem-having-trouble-figuring-out-how-to-make-it-reset/6005/3 "2018-11-27T23:48:44Z")

</div>

I don’t think you understand the question that I was asking. I have not finished the game yet, this is only the first question, resetting the snake to it’s original point. I am just wondering as to how to make the snake start at the points asked, as I am confused with how to do so with array objects. Thank you for your help.

---

<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:** [November 28, 2018, 12:05am UTC](https://discourse.processing.org/t/snake-game-problem-having-trouble-figuring-out-how-to-make-it-reset/6005/4 "2018-11-28T00:05:52Z")

</div>

Well, the top middle of the canvas is the point `(width/2, 0)`. So if you want your snake to start there, then its initial position should be set with statements like `x = width/2;` and `y = 0;`

If your snake is made up of many body parts, they can all start there. You would loop over each part, and set all of their positions to this location.
