Creating a snake-like body.
in
Programming Questions
•
1 years ago
I am basically trying to create an array of rectangles that updates its position continuously to form a snake like effect. The problem I am having is that by updating the position of each rectangle 1 by 1 is updating the position too slowly or updating the position of all of them using a for loop updates them to the same position.
Anyone know a good way to make it so that each rectangle updates its position one after the other in a snake like effect?
I should also mention I only want to modify the bodyX[1] onwards values as the 'points' which are 'following' the
bodyX[0] value.
Here are the initial variables:
- int xArrayPos = 0;
- int[] bodyY = new int[50];
- int[] bodyX = new int[50];
heres the code for the bendyBody method:
- public void bendyBody(){
- bodyY[0]=catY+longcat.height-5;
- bodyX[0]=catX+longcat.width/2;
- for(int i = 1; i < bodyY.length; i++){
- bodyY[i] = bodyY[i-1]+5;
- }
- // for(int xArrayPos = 1; xArrayPos < bodyX.length; xArrayPos++){
- xArrayPos++;
- if(xArrayPos == bodyX.length){
- xArrayPos=1;
- }
- bodyX[xArrayPos] = bodyX[xArrayPos-1];
- if(bodyX[xArrayPos] < bodyX[xArrayPos-1]){
- bodyX[xArrayPos]+=10;
- }
- if(bodyX[xArrayPos] > bodyX[xArrayPos-1]){
- bodyX[xArrayPos]-=5;
- }
- //}
- }
here is where I am generating the rectangles:
- for(int i = 0; i < bodyX.length; i++){
- rect(bodyX[i], bodyY[i], 10, 5);
- // Point(bodyX[i], bodyY[i]);
- // curveVertex(bodyX[i], bodyY[i]);
- }
Anyone know a good way to make it so that each rectangle updates its position one after the other in a snake like effect?
at the moment the if statement way of updating is too slow and the for loop is too fast.
1