# Need help with centipede / snake game

Hello guys!

So I’ve been working on a snake game (kind of ) for school recently but I’m kinda stuck on a part. Because I need to add (invisible) pieces of food on screen and when the snake eats it, the snake needs to be 1 block bigger.

I’ve been brainstorming for almost two days now and I can come up with a decent logic but I can’t just seem to program it well, I can’t use object oriented programming so all the tutorials I’ve seen, haven’t been much help.

What i’m thinking is that if the X and Y position of the head of the snake are equal to the position of the food the snake gets 1 square behind it but I have no clue as to how to program this.

I think I need to use random() in order to spawn random pieces of food in the map but I don’t know how to like give them a value or anything where the snake becomes bigger.

I need some advice on how to do this, without creating classes or objects. Any advice or help is welcome, I have no clue where to go from here, I’ve been stuck.
I hope someone can help me!

(If you need code from me, just let me know)

Accidentally I know this

You can make an array for the positions of all cells in the snake body (or two parallel arrays x and y)

initially the array is empty and you set x[0] with the position

You have a variable sizeSnake which is 1 initially. It indicates how long the snake is and how far you read and display the content of the two arrays.

Upon eating an apple it gets increased by 1

How would the movement happen?

It was late at night when I saw your message so I went to bed but I’ve been thinking about it last night, would a 2D array work for the two parallel arrays? (for x and y), However I have a problem with arrays that I don’t understand and that is how the array gets updated with the positions because if the snake moves around the x and y changes as well. How do I go about making this? I was thinking of a for loop to check the positions maybe although a for loop only runs with a certain condition and when that condition is met it stops running so I’m not sure how to do this. (afaik)

and with the snakeSize variable, I understand that it indicates how big the snake is, but if the array is empty how does it know the positions of the x and y positions of the snake?

the movement currently is done with the following code:

``````void Centipede() {

rect(xPosCent, yPosCent, xBreedteCent, xHoogteCent);
if (key == CODED) {
if (keyPressed == true) {
if (keyCode == UP) {
if (yPosCent >= 0 +xHoogteCent) {
yPosCent = yPosCent - 50;
}
}

rect(xPosCent, yPosCent, xBreedteCent, xHoogteCent);
if (key == CODED) {
if (keyPressed == true) {
if (keyCode == DOWN) {
if (yPosCent <= 800 - xHoogteCent) {
yPosCent = yPosCent + 50;
}
}
}
}

rect(xPosCent, yPosCent, xBreedteCent, xHoogteCent);
if (key == CODED) {
if (keyPressed == true) {
if (keyCode == RIGHT) {
if (xPosCent <= 800 - (2* xBreedteCent) ) {
xPosCent = xPosCent + 50;
}
}
}
}

rect(xPosCent, yPosCent, xBreedteCent, xHoogteCent);
if (key == CODED) {
if (keyPressed == true) {
if (keyCode == LEFT) {
if (xPosCent >= 0 + xBreedteCent ) {
xPosCent = xPosCent - 50;
}
}
}
}
}
}
}
``````

also how do I go about making the centipede itself? Right now I have one square that moves around, but the snake needs to be longer, but I don’t get for example how to make the snake go right when it has multiple body parts.

I hope i’ve provided enough information and I hope you can help me further!

Thank you

No. That won’t work

Chrisir

``````int xPosCent = 100;
int yPosCent = constrain(100, 0, 800);
int xBreedteCent = 50;
int xHoogteCent = 50;
``````

I forgot to add these variables to my code, and thanks for your quick reply^^

okay, let’s say you are at snakeSize = 1

Now the 0th entry of the array = head.

It moves, so you say

``````x[0] += moveX;
y[0] += moveY;
``````

When the snake is longer than 1:

Now, the trick is, before this (head move), you copy the content of the array one further UP (shift all the coordinates back one slot in the array)

So when this is you snake: L and head is upper left corner, the coordinates of the cells all go up.

Explanation

the last cell of the snake (its tail) gets a new value from the 2nd last cell, because that’s where the tail moves to: to the former position of the 2nd last cell.
Example:
so 4th cell gets content from 3rd
so 3rd cell gets content from 2nd
so 2nd gets from 1st
head gets new pos
so, it’s a for-loop for shifting the positions within the array

with this you also fill the array slowly from x[0] to x[1] etc. when the snake gets longer (`snakeSize` increases)

hard to explain, just test it.

No, but you can go for an array of PVector like pos[0].x = …

Chrisir

Hello,

I’ve been trying to make the for loop and to get the array to work but I really can’t figure out what to write, I’ve been stuck on this for the past 2 hours, could you give me a basic example that I could work from?

Thank you for all your help so far!

No.