Basic " Snake"Array Trial question from the Learningprocessing Book
in
Programming Questions
•
5 months ago
Hi there, i cant understand this part :
- // shift array values
- for ( int i = 0 ; i < xpos.length-1 ; i++){
- xpos [i] = xpos[i+1];
- }
Ill explain why i don't understand :
// declare arrays, which have a length of 4
int [] xpos = new int [4];
ok... we make 4 elements in a lis, with an index of 0,1,2 and 3
void setup (){
size (600,600);
smooth ();
// initialize, we give each of them a starting value of 0
for ( int i = 0; i < xpos.length ; i++){
xpos[i] = 0 ;
}}
void draw (){
background (255);
// shift array values
for ( int i = 0 ; i < xpos.length-1 ; i++){
xpos [i] = xpos[i+1];
}
so we are saying, to the whole list minus one ( index 3 in this case ), we would do ''i++ ''
which means: lets add one. So what would that look like ? :
0,1,2 then these plus one ?
1,2... and then what , i cant go further is length minus one... what i'm i doing wrong ?
This is the whole code :
- // declare arrays
- int [] xpos = new int [50];
- int [] ypos = new int [50];
- void setup (){
- size (600,600);
- smooth ();
- // initialize
- for ( int i = 0; i < xpos.length ; i++){
- xpos[i] = 0 ;
- ypos[i] = 0 ;
- }
- }
- void draw (){
- background (255);
- // shift array values
- for ( int i = 0 ; i < xpos.length-1 ; i++){
- xpos [i] = xpos[i+1];
- ypos [i] = ypos[i+1];
- }
- // new location
- xpos[xpos.length-1] = mouseX;
- ypos[ypos.length-1] = mouseY;
- // draw everything
- for ( int i = 0 ; i < xpos.length ; i++ ){
- noStroke ();
- fill ( 255-i*5);
- ellipse ( xpos[i],ypos[i],i,i);
- }
- }
1