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

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 :slight_smile:

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.
  }
}
1 Like

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:

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.

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.