Líneas que se mueven — How to move a line across the sketch

please format code with </> button * homework policy * asking questions

hola,
me mandaron un ejercicio y no se cómo hacerlo
dice
líneas de distintos colores que aparecen de un extremo a otro de la pantalla

This reference shows how to draw a line:https://processing.org/reference/line_.html

2 Likes

Hello @paopoapo

The exercise: lines of different colors that appear from one end of the screen to the other

Break down your problem into small steps.

You will need:
1/ to make a random color for each new line
2/ to make a lot of lines
3/ fill the screen with the lines

In the processing reference look up:
line()
color()
random()

Please tell us what you know, or share your code. Then we can give you more specific guidance.
:nerd_face:

y cómo sería el código para hacer esos 3 pasos ?

1 Like

Since this is a homework assignment, I’m not going to do that for you. You won’t learn anything.

I gave you a very specific roadmap.
The next step is to read the reference entries and write some code.
After that, if you have any questions, come back and I will help you.

:nerd_face:

2 Likes

esto es lo que tengo pero necesito que no se salgan de la pantalla sino que choquen de lado a lado y que sean de diferentes colores

final int nbOfLines = 50;
ArrayList lines;

void setup() {
size(900, 900);
frameRate(60);

lines = new ArrayList();

createLines();
}

void draw() {
background(20);

for (int i = 0; i < lines.size(); i++) {
lines.get(i).run();
}

if (lines.get(lines.size() - 1).isOutOfScreen()) {
createLines();
}
}

void createLines() {
lines.clear();
int y = height;

for (int i = 0; i < nbOfLines; i++) {
y += random(10) + 1;
lines.add(new CustomLine(y));
}
}

class CustomLine {
private int y;

CustomLine(int y) {
this.y = y;
}

void run() {
move();
display();
}

void move() {
y -= 2;
}

void display() {
stroke(200);
line(0, y, width, y);
}

boolean isOutOfScreen() {
return y < 0;
}
}

This is what I have but I need them not to go off the screen but to collide from side to side and to be different colors.

///////////////////////////////////////////////////////////////////////

See the comments next to the lines I have added and/or changed.

I hope this helps!

////////////////////////////////////////////////////////

Your code with alterations in place:

final int nbOfLines = 50;
ArrayList <CustomLine> lines; // declare the CustomLine class

void setup() {
  size(900, 900);
  frameRate(60);

  lines = new ArrayList();

  createLines();
}

void draw() {
  background(20);

  for (int i = 0; i < lines.size(); i++) {
    lines.get(i).run();
  }

  //if (lines.get(lines.size() - 1).isOutOfScreen()) { 
  //  createLines();
  //}
}

void createLines() {
  lines.clear();
  int y = 0; // begin lines from top of screen

  for (int i = 0; i < nbOfLines; i++) {
    y += 10;
    lines.add(new CustomLine(y));
  }
}

class CustomLine {
  private int y;

  int ySpeed = 5; // add speed variable to move the lines

  color col = color(random(255), random(255), random(255)); //declare your colors here

  CustomLine(int y) {
    this.y = y;
  }

  void run() {
    bounce(); // call bounce function
    move();
    display();
  }

  void move() {
    y = y + ySpeed;
  }

  
  void bounce() { // add bounce function

    if (y > height || y < 0) { // if y is greater than height, or y is less than "0"
      ySpeed = ySpeed * -1; // change direction
    }
  }


    void display() {
      stroke(col);
      line(0, y, width, y);
    }

    //boolean isOutOfScreen() { // do not need this boolean, it changes the line bounce behavior
    //  return y < 0;
    //}
  }

:nerd_face:

1 Like