# For Loop in Processing (snake game)

**URL:** <https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059>\
**Category:** Beginners\
**Created:** [July 23, 2022, 2:50am UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059 "2022-07-23T02:50:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ikay](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ikay](https://discourse.processing.org/u/ikay)\
**Post date:** [July 23, 2022, 2:50am UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/1 "2022-07-23T02:50:30Z")

</div>

hey i have a code and i’m trying to create a loop to draw 3 ellipses

int SNAKE\_SIZE =20,SNAKE\_LENGTH=4;  
void setup(){  
size(500,500);  
}  
//ellipse(250,250,SNAKE\_SIZE,SNAKE\_SIZE);  
//ellipse(250,270,SNAKE\_SIZE,SNAKE\_SIZE);  
//ellipse(250,290,SNAKE\_SIZE,SNAKE\_SIZE);  
void draw(){  
for(int i=0;i\<SNAKE\_LENGTH;i++){

}  
this is my code

---

<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:** [July 23, 2022, 3:57am UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/2 "2022-07-23T03:57:26Z")

</div>

Your three ellipses are drawn at (250,250), (250,270), and (250,290). Notice that only the Y position changes. It starts at 250, and increases by 20 each time.

Your looping variable, i starts at 0, and then is 1, and then 2.

Consider what the value of (150 + 20 \* i) is for each of these values for i.

Now try drawing an ellipse inside your loop. What is the X position? What is the Y position?!?

---

<div class="post-metadata">

**Author:** ![ikay](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ikay](https://discourse.processing.org/u/ikay)\
**Post date:** [July 26, 2022, 4:10pm UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/3 "2022-07-26T16:10:41Z")

</div>

thank you so much for helping me  
but now I’m trying to move the snake continuously around but it’s giving me syntax error  
this is my code

int x = 250;  
int y= 250;

final int SNAKE\_SIZE=20;  
int SNAKE\_LENGTH=3;  
final int X\_SPEED = 1;  
final int Y\_SPEED = 1;  
void setup(){  
size(500,500);

}

//ellipse(250,290,SNAKE\_SIZE,SNAKE\_SIZE);  
void draw(){  
if(x \>= 500 || x \<= 0) {  
background(0);  
}  
if(y \>= 500 || y \<= 0){  
background(0);  
}  
}

drawSnake();

}  
void drawSnake(){

for(int i=0;i\<SNAKE\_LENGTH;i++){  
ellipse(x,y,SNAKE\_SIZE,SNAKE\_SIZE);  
y+=20;  
}  
y=250;  
}  
void keyPressed(){  
if(key==‘w’){  
drawSnake();  
y -= Y\_SPEED;  
}  
if(key==‘s’){  
drawSnake();  
y += Y\_SPEED;  
}  
if(key==‘a’){  
drawSnake();  
x -= X\_SPEED;  
}  
if(key==‘d’){  
drawSnake();  
x += X\_SPEED;  
}  
}

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 27, 2022, 1:08pm UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/4 "2022-07-27T13:08:06Z")

</div>

Hi @ikay,

please edit your post and [format your code](https://discourse.processing.org/faq#format-your-code)

Take a look on this position to find out what’s wrong…

> [@ikay](#):
>
> void draw(){  
> if(x \>= 500 || x \<= 0) {  
> background(0);  
> }  
> if(y \>= 500 || y \<= 0){  
> background(0);  
> }  
> }
> 
> drawSnake();
> 
> }

Cheers  
— mnse

---

<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:** [July 27, 2022, 4:42pm UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/5 "2022-07-27T16:42:00Z")

</div>

Hello,

remember to hit **ctrl-t** to get auto-format in processing.  
Then you’ll see you had one misplaced }.

I rewrote some of the code. See below.

What you should do: Think of how the snake moves when it’s longer. Best use an array (or two arrays, one for `xValues` and one `yValues`) to keep track of the positions of each cell/ellipse within the snake. Your for loop essentially just displays each position / ellipse of the array.

Moving the snake means to copy from the head into the array and move each data one slot down the array.

Chrisir

**Rewritten code**  
with not much changes

```auto
final int SNAKE_SIZE=20;

final int X_SPEED = 1;
final int Y_SPEED = 1;

int x = 250;
int y = 250;

int SNAKE_LENGTH=3;

// ---------------------------------------------------------------------------

void setup() {
  size(500, 500);
  background(220);

  y=250;
}

//ellipse(250,290,SNAKE_SIZE,SNAKE_SIZE);
void draw() {
  background(220);

  if (x >= 500 || x <= 0) {
    background(0);
  }
  if (y >= 500 || y <= 0) {
    background(0);
  }

  drawSnake();
}

void drawSnake() {
  int y2=0; 
  for (int i=0; i<SNAKE_LENGTH; i++) {
    ellipse(x, y+y2, SNAKE_SIZE, SNAKE_SIZE);
    y2+=20;
  }
}

void keyPressed() {
  if (key=='w') {
    drawSnake();
    y -= Y_SPEED;
  }
  if (key=='s') {
    drawSnake();
    y += Y_SPEED;
  }
  if (key=='a') {
    drawSnake();
    x -= X_SPEED;
  }
  if (key=='d') {
    drawSnake();
    x += X_SPEED;
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![ikay](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ikay](https://discourse.processing.org/u/ikay)\
**Post date:** [July 28, 2022, 12:29am UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/6 "2022-07-28T00:29:46Z")

</div>

thank you so much my problem is getting the snake to increase in size after it eats food my formula is not working

```auto
int growth = 3;
final int X_SPEED = 1;

final int Y_SPEED = 1;

final int SNAKE_SIZE=15;
final int SNAKE_LENGTH=SNAKE_SIZE*growth;

int x = width/2;

int y =height/2;

float foodX = random(0, 500); 
float foodY = random(0, 500); 
int foodSize = 15;

boolean hit = false; //controls hit detection logic for when the snake hits the food 
int totalHits = 0; //the game will start at 0 points and in increase by 1 with each food catpure
final int MAX_HITS =5;

//final int SNAKE_LENGTH=3;

// ---------------------------------------------------------------------------

void setup() {

 size(500, 500);

 background(220);

 y=250;
}

//ellipse(250,290,SNAKE_SIZE,SNAKE_SIZE);

void draw() {

 background(220);

 if (x >= 500 || x <= 0) {

   background(0);
 }

 if (y >= 500 || y <= 0) {

   background(0);
 }

 moveSnake();
 checkIfKeyPressed();
 drawTarget();
 checkIfHitTarget();
}

void moveSnake() {

 //int y2=0;
fill(#FFFFFF);
 for (int iY=0; iY<SNAKE_LENGTH; iY++) {
 if(key == 'w'){
  ellipse(x, y-iY*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
 }
 else if(key == 's'){
  ellipse(x, y+iY*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
 }
  for (int iX=0; iX<SNAKE_LENGTH; iX++) {
     if(key == 'd'){
   ellipse(x - iX*SNAKE_SIZE, y, SNAKE_SIZE, SNAKE_SIZE); 
     }
     else if(key == 'a'){
    ellipse(x + iX*SNAKE_SIZE, y, SNAKE_SIZE, SNAKE_SIZE);
     }
   //y2+=20;
 }
}
}

void checkIfKeyPressed() {

 if (key=='w') {

   moveSnake();

   y -= Y_SPEED;
 }

else if (key=='s') {

   moveSnake();

   y += Y_SPEED;
 }

else if (key=='a') {

   moveSnake();

   x -= X_SPEED;
 }

else if (key=='d') {

   moveSnake();

   x += X_SPEED;
 }
}
void drawTarget(){
 fill(0);
rect(foodX,foodY, foodSize,foodSize);
}
void checkIfHitTarget(){
 if (dist(x, y, foodX, foodY)<=foodSize-5)
{
foodX = random(0+foodSize/2, 500-foodSize/2); 
foodY = random(0+foodSize/2, 500-foodSize/2);
hit = true;
}
if (hit)
{
totalHits += 1;
hit=!hit; 
growth++; 
}
}paste code here

```

---

<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:** [July 28, 2022, 6:55am UTC](https://discourse.processing.org/t/for-loop-in-processing-snake-game/38059/7 "2022-07-28T06:55:43Z")

</div>

> [@ikay](#):
>
> `SNAKE_LENGTH`

Increase this number when hitting an apple
