Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to use a for or while loop for the line genarting speed,

Is there a way where I can make a loop of this in my code ?

}

  line(700,lineY + 90, 0, lineY + 90);
  lineY = lineY + speed
  if(lineY >= 370){
      
    lineY = 250


  }

As u can see in my Code its every time the same code but with a changing of 30


let lineY=250;
let speed=0.3;
var r;
var g;
var b;
var a;
var n;
var m;

function setup() {
  createCanvas(700, 500);
  rectMode(CORNER);
  frameRate(60);
  n = color(255,0,0);
  m = color(0,0,255);
}

function draw() {
  background(0, 0, 0);
  
  r = random(255); // r is a random number between 0 - 255
  g = random(255); // g is a random number betwen 100 - 200
  b = random(255); // b is a random number between 0 - 100
  a = random(200,255); // a is a random number between 200 - 255
  var t = map(mouseX,0,width,0,1.0);
  var c = lerpColor(n,m,t);

  strokeWeight(0); //Horizont Rechteck
  fill(41,0,33)
  rect(0, 0, 700, 250);


  fill(c)
  circle(350, 80, 250);

  fill(0) //Berg links
  triangle(125, 250, 275, 250, 200, 150);

  fill(0) //Berg Mitte
  triangle(225, 250, 475, 250, 350, 100);

  fill(0) // Berg rechts
  triangle(575, 250, 425, 250, 500, 125);

  strokeWeight(5);
  stroke(r,g,b);
  line(125,250, 200, 150);

  strokeWeight(5);
  stroke(r,g,b);
  line(425,250, 500, 125);

  strokeWeight(5);
  stroke(r,g,b);
  line(225,250, 350, 100);


  strokeWeight(3);  // Linie am Horizont
  stroke(0,204,204);
  line(700,250, 0, 250);

  strokeWeight(3); //mitte‚
  stroke(0,204,204);
  line(350,250, 350, 700);

  strokeWeight(3); //linksmitte
  stroke(0,204,204);
  line(250,250, 50, 700);

  strokeWeight(3); //links
  stroke(0,204,204);
  line(100,250, -100, 700);

  strokeWeight(3); //rechtsmitte
  stroke(0,204,204);
  line(450,250, 650, 700);

  strokeWeight(3); //rechts
  stroke(0,204,204);
  line(600,250, 800, 700);


  line(700,lineY , 0, lineY );
  lineY = lineY + speed
  if(lineY >= 280){
      
    lineY = 250


  }
  
  line(700,lineY + 30, 0, lineY + 30);
  lineY = lineY + speed
  if(lineY >= 310){
      
    lineY = 250


  }

  line(700,lineY + 60, 0, lineY + 60);
  lineY = lineY + speed
  if(lineY >= 340){
      
    lineY = 250


  }

  line(700,lineY + 90, 0, lineY + 90);
  lineY = lineY + speed
  if(lineY >= 370){
      
    lineY = 250


  }

  line(700,lineY + 120, 0, lineY + 120);
  lineY = lineY + speed
  if(lineY >= 400){
      
    lineY = 250


  }

  line(700,lineY + 150, 0, lineY + 150);
  lineY = lineY + speed
  if(lineY >= 430){
      
    lineY = 250


  }

  line(700,lineY + 180, 0, lineY + 180);
  lineY = lineY + speed
  if(lineY >= 460){
      
    lineY = 250


  }

  line(700,lineY + 210, 0, lineY + 210);
  lineY = lineY + speed
  if(lineY >= 490){
      
    lineY = 250


  }

}



MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use a for loop :

for (let i = 0; i <= 7; i++) {
  line(700, lineY + (i * 30), 0, lineY + (i * 30));
  lineY += speed; //equivalent to lineY = lineY + speed;
  if (lineY >= 280 + (i * 30)) {
    lineY = 250;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading