Sort of a snake game problem with code

Hello all,

For school i have to make a kind of snake game. The game has the following rules:

Rules of game:

  • 1 player needs to make as many points before all particles of body dissapeared.
  • game should be a field of 16 by 8 blocks.
  • 20 bananas are hidden in the game and are invisible. when snake eats them player gains 10 points.
  • game has 10 animals on random location in the field. if snake goes over them it loses a part of the body.
  • when only the head is left it should be Game Over and show score.

I currently only have a moving head, I can’t figure out how I can make a body (length of 8 total) which follows the head around the playing area.

I need to hand this in tomorrow so I’m really stressed atm.

I have made a start but it is difficult to get out. I have the feeling that my base is not good either. Hopefully someone can help me on my way… Here below is the code i made so far.

int centipedeX = 50;
int centipedeY = 50;
boolean spelGestart;
int score = 0;
int lengteSlang = 7;  // length of centipede
int grootte = 50;    //size of 1 part of the centipede



void startSpel() {
  tekenSpeelbord();
}

void tekenSpeelbord() {
  background(#154c79);
  int x = 50;
  while (x<width-149) {
    line(x, 50, x, height-149);
    x=x+50;
  }
  int y=50;
  while (y<height-149) {
    line(50, y, width-149, y);
    y=y+50;
  }
}

void score() {
  fill(255, 0, 0);
  text("Score:" + score, 400, 500);
}

void centipedeHoofd() {
  startSpel();
  fill(#4AF060);
  ellipseMode(CORNER);
  circle(centipedeX, centipedeY, 50);
  centipedeX= constrain(centipedeX, 51, 799);
  centipedeY= constrain(centipedeY, 51, height-201);
}


void centipedeLichaam1(){
  
  
}

void keyPressed() {
  if (key== CODED) {
    if (keyCode == UP) {
      centipedeY= centipedeY-50;
    } else if (keyCode == DOWN) {
      centipedeY = centipedeY+50;
    } else if (keyCode ==RIGHT) {
      centipedeX= centipedeX+50;
    } else if (keyCode ==LEFT) {
      centipedeX=centipedeX-50;
    }
    print( centipedeX);
  }
}

Hello @taco1 :slightly_smiling_face:

You might try making your snake with 8 circles lined up using an array? Whenever the snake loses part of its body, one circle disappears. There is shorten method in array you can use. Or, even simpler, set the fill color to noFill() to hide the section of deleted snake.

There is a tutorial here on arrays if you decide to go that route:

Good luck!
:nerd_face:

1 Like

Hi debxyz,

If I make an array of all the body parts.
How is every different segment gonna know which side to go to, now the head can move but how does the second segment of the centipede know which direction to go to???

This is just a beginning roadmap to set up the game. First focus on building the structure and interaction of parts. After you get all of the parts working you can then decide what you want to use as icons for the food, animals and snake.

int num = 8; //number of snake sections
int[] x = new int[num]; //array of x-positions for snake body
int[] y = new int[num]; //array of y=positions for snake body

int num2 = 10; //number of animals
int [] x2 = new int[num2]; //array of x-positions for animals
int [] y2 = new int[num2]; //array of y-posittions for animals

int num3 = 20; //number of bananas
int [] x3 = new int[num3]; //array of x-positions for bananas
int [] y3 = new int[num3]; //array of y-posittions for bananas

int sz = 25; //size for snake section, bananas, and animals

void setup() {
  size(600, 600);
  noStroke();
  rectMode(CENTER);

  for (int j = 0; j < num2; j++) { //for the animals, initialize the 10 x & y positions with for-loop. 
                                   //You must do this in setup() to keep the x, y positions constant
    x2[j] = int(random(width)-sz);
    y2[j] = int(random(height)-sz);
  }
  
  //add code here to initialize bananas 20 x & y positions with for-loop
}

void draw() {
  background(255);

  for (int j = 0; j<num2; j++) { //draw the animals
    fill(#EA8334);
    square (x2[j], y2[j], 25); // placeholder for animals
  }
  
  //add code here to draw the bananas

  
  for (int i = 0; i < num; i++) { //draw the snake
    fill(#70D827);
    circle(mouseX+x[i]*25, mouseY+i*23, sz); // placeholder for snake
  }
  
  //add code here, you need to check when the 1st (top) circle overlaps with 
  //the food & animals using the dist() function
}

Please try to work out the next steps.
Good luck!
:nerd_face:

1 Like

Hey thanks for your help, I’ve come a far way but I still have no clue how to make the snake move and keep some sort of a memory within my code that remembers every move the snake has made, can you help me out with that?

I need this memory because every part of the snake has to follow to head which I move using the arrows on my keyboard.