For Loop in Processing (snake game)

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

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?!?

2 Likes

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;
}
}

Hi @ikay,

please edit your post and format your code

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

Cheers
— mnse

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

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;
  }
}
//

2 Likes

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

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

Increase this number when hitting an apple