Learn creative coding writing simple programs

7. Animate horizontal lines, use a variable

Modify the program from episode 6 to make all lines horizontal. To achieve this we use "variables" for the first time. A variable is like a box where we can store a value and reuse this same value multiple times in our program.

A horizontal line has a starting and an ending point, and both are at the same distance from the top border of the screen. To make sure both points are at the same distance from the top (both have the same Y value), we use twice a variable that contains our Y value.

New: Sam below requested more info about "float", so here it goes: variables (these boxes where you can store things) in Processing have a "type". This means each box can only store certain types of things. Imagine having boxes that could only contain pencils, or only bananas. You have to choose the right kind of box for your item. A "float" is like a box that can store numbers with decimals (like 6.259305). Other available types are "String" (for text), "int" (for integer numbers like 6 or -523), or "boolean" (for true/false).

The reason I used float is because random() gives you back a float.

It is possible to convert from one type to another.

Tags: horizontal, line, animation, variable

Code editor

You can make changes to the code below. Then
Below you can ask and answer questions related to this video. If your question is unrelated to the video, please post it at the Processing Forum as I'm unable to help with all of your programs and still have enough free time to eat and sleep :)
comments powered by Disqus