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

**URL:** https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413
**Category:** Español
**Tags:** homework
**Created:** [January 2, 2023, 6:26pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413 "2023-01-02T18:26:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![paopoapo](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@paopoapo](https://discourse.processing.org/u/paopoapo)
#### Post date: [January 2, 2023, 6:26pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/1 "2023-01-02T18:26:12Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

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

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [January 2, 2023, 7:32pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/2 "2023-01-02T19:32:46Z")

</div>

This reference shows how to draw a line:[https://processing.org/reference/line\_.html](https://processing.org/reference/line_.html)

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [January 2, 2023, 8:13pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/3 "2023-01-02T20:13:40Z")

</div>

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.  
🤓

---

<div class="post-metadata">

### Author: ![paopoapo](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@paopoapo](https://discourse.processing.org/u/paopoapo)
#### Post date: [January 2, 2023, 8:35pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/4 "2023-01-02T20:35:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [January 2, 2023, 8:46pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/5 "2023-01-02T20:46:48Z")

</div>

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.

🤓

---

<div class="post-metadata">

### Author: ![paopoapo](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@paopoapo](https://discourse.processing.org/u/paopoapo)
#### Post date: [January 3, 2023, 3:45pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/6 "2023-01-03T15:45:58Z")

</div>

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;  
}  
}

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [January 3, 2023, 5:29pm UTC](https://discourse.processing.org/t/lineas-que-se-mueven-how-to-move-a-line-across-the-sketch/40413/7 "2023-01-03T17:29:44Z")

</div>

> [@paopoapo](#):
>
> 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

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:

```auto
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;
    //}
  }

```

🤓
