Learn programming 9: Change screen size, animate colorful lines
-
0:00 - 0:05Now that we have the solution
to the exercise I proposed yesterday... -
0:05 - 0:07I think we should add some color.
-
0:07 - 0:12Now it looks very serious.
A black background and white lines. -
0:12 - 0:18We should add some colors.
We should also make it larger: the window is too small. -
0:18 - 0:21First let's find out how we can add some color.
-
0:21 - 0:26We know this stroke() function is setting the color of the line,
-
0:26 - 0:28but it's just making it white.
-
0:28 - 0:34We have seen that if we add
three values here separated by commas -
0:34 - 0:37instead of just one, we get some color.
-
0:37 - 0:43These are the Red value, the Green value and the Blue value.
-
0:43 - 0:46Now we should be getting red line. Let's try it out.
-
0:46 - 0:50Yes! But it's still not very colorful...
-
0:50 - 0:55It would be good for vampires...
maybe... but we are not vampires :) -
0:55 - 1:10Let's add some random colors!
So we know that if we repeat this random(256)... -
1:10 - 1:13inside the parenthesis 3 times we will get completely random colors.
-
1:13 - 1:22We can get anything: any shade of green, yellow, blue, purple, black or white.
-
1:22 - 1:28But it's still moving very fast and with this
black background it's a bit hard to see. -
1:28 - 1:37Let's remove this black background that it's
being drawn all the time. -
1:37 - 1:40You can deactivate any line
if you add two forward slashes: // -
1:40 - 1:46so you convert this line into a comment
and the line does not run anymore. -
1:46 - 1:56So now that we are no longer erasing the screen
every time it should keep adding colorful lines. -
1:56 - 2:05So there you have the result.
It's adding lines with lots of different colors. -
2:05 - 2:10Let's take a look at the random() function.
-
2:10 - 2:19Let's go to the reference. Here I press CTRL+F
and type "r a n d o m", then click. -
2:19 - 2:27Here's the random() function. There are two ways
of using it. We are using it this way. -
2:27 - 2:34We use random and type inside one number.
But it's possible to enter two numbers. -
2:34 - 2:37What happens in that case?
-
2:37 - 2:44When you enter one number, you get a random number between 0 and the number you entered.
-
2:44 - 2:47This means give me a number between 0 and 100.
-
2:47 - 2:53This means give me a number between 0 and 256.
-
2:53 - 3:01But if we add two numbers, we can have a bottom limit.
What does that mean? -
3:01 - 3:16Let's see. If I enter here "200, 256" this means "give me a random number between 200 and 256"
-
3:16 - 3:28We know that 0 means black, or no light:
no red light, no green light, no blue light. -
3:28 - 3:38We are now forcing the program to give us numbers which are quite high; quite close to the top.
-
3:38 - 3:46What happens now? So now the random colors are, all of them, quite close to white.
-
3:46 - 3:53When we get 256 for all red, green and blue, we get white.
-
3:53 - 3:59So these are random colors, and all of them are close to white.
-
3:59 - 4:10If we do the opposite, if we enter here "100, 150",
what happens then? -
4:10 - 4:18You should try. You should different values here and
see what kind of results do you get. -
4:18 - 4:24Now you can see it looks quite gray. It's quite dark. You can still see some color,
-
4:24 - 4:29but it's not very colorful.
-
4:29 - 4:35You don't have to use always
the same values here three times. -
4:35 - 4:40For example, let's put more red than green or blue.
-
4:40 - 4:48With this example we know we are going to get an amount of red which is between 200 and 256.
-
4:48 - 4:54So it's near the maximum.
But we'll get only a little bit of green and some blue. -
4:54 - 5:09So in the result the overall color is close to red. Not pure red, but close to it.
-
5:09 - 5:25One last example. We can get a lot of red and a lot of green, but just a little bit of blue (50 to 100).
-
5:25 - 5:36What now? We get something like yellow-green-orange. Why is this happening?
-
5:36 - 5:44Well, when we get the maximum value for red and green,
the result is yellow. -
5:44 - 5:55Sometimes it happens that we get less red than green, so the result is more green-like.
-
5:55 - 6:01You can try different combinations here
and see what you get. -
6:01 - 6:04You can get all kinds of different tones.
-
6:04 - 6:12Now it looks more colorful and happier than before.
We mentioned that the screen is too small. -
6:12 - 6:21How can we make it larger? Let's go to the reference and search for size()
-
6:21 - 6:28This is textSize... here it's size().
Maybe it's this one. Let's see what it is. -
6:28 - 6:38"Defines the dimensions of the display window
in units of pixels" -
6:38 - 6:42"The size() function must be the first line in setup()"
-
6:42 - 6:51"If size() is not called" ... like right now, we don't have in our program any call to the size() function ...
-
6:51 - 7:00What is happening then? ... "the default size of the window is 100x100 pixels"
-
7:00 - 7:03That's what we got: 100x100
-
7:03 - 7:09So let's see here in the example how they do it. Here's an example.
-
7:09 - 7:15It says "void setup()" and inside
they call size() and then background() -
7:15 - 7:24So I'm going to add here at the beginning "void setup", then open and close parenthesis...
-
7:24 - 7:32open and close curly braces, and in between I will call size().
-
7:32 - 7:46Currently we have 100x100, so let's make it 200x200 to double the display size. Let's run this program.
-
7:46 - 7:57Ok! Nice. Now you can see the screen is twice as large but we are only drawing on the top left corner.
-
7:57 - 8:03Why is that? Let's look at our program.
-
8:03 - 8:18Ok. One reason is that we are drawing lines from the top to pixel 99. Our screen used to be 100 pixels tall.
-
8:18 - 8:27So this was enough to fill the screen, but now that it's 200 pixels tall, I should make this 199.
-
8:27 - 8:39This way we will draw lines from the top to the bottom of the screen. Let's check it out.
-
8:39 - 8:52That's right! Now lines go from top to bottom, but still only draw on the left half of the screen.
-
8:52 - 8:57We are not drawing lines on the right half. This happens because here we say ...
-
8:57 - 9:10"distance_left" is a random number between 0 and 100, but our screen is 200 pixels wide.
-
9:10 - 9:18Now I tell him I want numbers between 0 and 200, and I run it again.
-
9:18 - 9:24Finally we have a big screen full of colorful lines.
- Title:
- Learn programming 9: Change screen size, animate colorful lines
- Description:
-
We modify the last program we did (animated white vertical lines on black background) to make it more colorful. We see how to use the random function with two parameters instead of one. This way we get a random nomber that is between a minimum and a maximum value. We also show how to change the screen size using the size() function to make it larger. When we don't use the size() function, the screen is by default 100 pixels wide and 100 pixels tall.
- Video Language:
- English
- Duration:
- 09:26
hamoid edited English subtitles for Learn programming 9: Change screen size, animate colorful lines | ||
hamoid added a translation |