xxxxxxxxxx
float trainX;
// float trainY = 0; // we use this when manually drawing each train compartmet
float trainSpeed = 2; // speed at which the train moves
// How long is the list, i.e. how many trains do I want, right now I want 10
float [] trainYpos = new float [10]; // change this [10] to number of trains you want
void setup () {
size (100, 500);
trainX = width/2; // i want my trains to all start in the top middle of the screen
// we are drawing as many trains as the length of our list -> float [] trainYpos = new float [10]
for (int counter = 0; counter < trainYpos.length; counter = counter+1) {
// draws a new train at this distance apart
trainYpos[counter] = counter * 30; // trains are 30 px apart from each other
// this is because the height of our trains is 30. so my trains are all drawn connected to each other
}
}
void draw () {
background (100);
// train moves down and when it reaches end of screen, it moved back to top of the scree
for (int c = 0; c < trainYpos.length; c = c+1) {
if (trainYpos[c] > height) {
trainYpos[c] = 0;
}
//I want the same speed & colour for all my train boxes, so I put this *inside the for loop*
fill (255, 0, 0); // make them all red
rect (trainX, trainYpos[c], 10, 30); // draw train
// when train reaches the bottom of screen move it to the top
trainYpos[c] = trainYpos[c] + trainSpeed;
}
////---- OLD CODE: when we had only 1 train compartment, we drew it,
////---- and animated it like this, without a for loop
//if (trainY > height) {
// trainY = 0;
// }
//fill (255, 0, 0); // red
// rect (trainX, trainY, 10, 20); // draw train
// trainY = trainY + trainSpeed;
}