Learn programming 3: Create an animation, use random()
-
0:00 - 0:04So let's recap what we have seen until now.
-
0:04 - 0:10We now know two instructions. One is called 'point', the other is 'line'.
-
0:10 - 0:18We know that after these instructions there's something inside parentheses...
-
0:18 - 0:23A sequence of values separated by commas.
-
0:23 - 0:27Sometimes, like in this case it's only two values...
-
0:27 - 0:30'line' expects four.
-
0:30 - 0:38And at the end of the line there should be a semi-colon to make the computer know that the line is finished.
-
0:38 - 0:42Let's try to run it without it and see what happens.
-
0:42 - 0:46You see here, syntax error. Maybe I'm missing a semi-colon.
-
0:46 - 0:51Yeah, exactly. This is a bit hard to understand.
-
0:51 - 1:01It's a very long error, but this missing semi-colon is simple. So we know, we add the semi-colon.
-
1:01 - 1:06... and now it's working.
-
1:06 - 1:10Then we have learned something about the display like this little display we have here.
-
1:10 - 1:16Seems to have 100 pixels width and 100 pixels height.
-
1:16 - 1:24I'm going to explain a little bit more about this display in this drawing.
-
1:24 - 1:35Let's imagine this is our display. It's made of a lot of little pixels, like this one.
-
1:35 - 1:42The reason why we are using these two number over here: (50, 50).
-
1:42 - 1:52Is because each of the columns is numbered (0, 1, 2, 3, 4, ...)
-
1:52 - 2:03Depending on how large your screen is you can have 1280 or maybe 1024.
-
2:03 - 2:09If you're on a cellphone, cellphones have less resolution. Maybe it's 400.
-
2:09 - 2:20The same for the row. We have here (0, 1, 2, 3, ...) up to wathever, ... it may be 768.
-
2:20 - 2:33So to draw a pixel here you have to know which column and which row you're looking for.
-
2:33 - 2:42In this case it would be (3,3). So that why you write these two numbers. Or (50, 50)...
-
2:42 - 2:49... this way you can chose which pixel on the screen you want to illuminate, or change the color.
-
2:49 - 2:59Ok, so now that we know these two instructions. 'point' and 'line' this is a bit boring at the moment ...
-
2:59 - 3:06... because everytime we run program we get exactly the same drawing.
-
3:06 - 3:12It would be nice if something was changing. Maybe you can add some randomness.
-
3:12 - 3:19Instead of getting always the same line. Maybe everytime we run the program it can be a bit different.
-
3:19 - 3:28So let's search on our reference. What is there about random?
-
3:28 - 3:34If you press ctrl+f, on most browsers, you can search for something in the page...
-
3:34 - 3:38... so we'll type here 'random'. Ok.
-
3:38 - 3:47I see here just a few things about random. Looks like there's 5 functions that do something.
-
3:47 - 3:52Maybe this last one, let's check it out 'random'.
-
3:52 - 3:58Here's some examples. They probably look a little complicated at the moment.
-
3:58 - 4:07Let's read the description. "Generates random numbers. Each time the random() functions is called...
-
4:07 - 4:15... it returns an unexpected value within the specified range. If one parameter is passed to the function...'
-
4:15 - 4:21it will return a float... What's a 'float'? We don't know that yet...
-
4:21 - 4:30But let's see how it's used here. 'random(high)' or 'random(low, high)'.
-
4:30 - 4:37Here it says you can use 'random(5)' and it will return a number between 0 and 5.
-
4:37 - 4:40That sounds fun.
-
4:40 - 4:44So instead of drawing the same thing all the time.
-
4:44 - 4:52We could draw some random lines. We can replace all these numbers...
-
4:52 - 4:55... by some random values.
-
4:55 - 5:00Let's start replacing these last two '99' for example.
-
5:00 - 5:10So we'll draw a line from (0,0) which is the first pixel on the screen to ...
-
5:10 - 5:17... somewhere randomly on the screen.
-
5:17 - 5:22You have to be careful with these parentheses.
-
5:22 - 5:33When you put the cursor here it illuminates the other correspoding parentheses.
-
5:33 - 5:37That's because every parentheses you have opened must be closed.
-
5:37 - 5:50So this is 'random(100)' and this is again 'random(100)' and back here you close the parenthesis from the beggining.
-
5:50 - 5:57You can add some spaces if you want so it's more clear.
-
5:57 - 6:02This is drawing a line from (0,0) to a random point somewhere on the screen.
-
6:02 - 6:07Let's run this thing and see what happens.
-
6:07 - 6:13So now it starts here at (0,0) and ends on this point.
-
6:13 - 6:20What if run it again? It's very different.
-
6:20 - 6:32Everytime I run the program it's drawing a line that starts from the corner and ends somewhere on the screen.
-
6:32 - 6:39Let's replace the first two numbers with two random ones.
-
6:39 - 7:04Now we'll draw a line from some random horizontal and vertical position on the screen and to a random destination x and y.
-
7:04 - 7:08Oh that's a funny coincidence, it's totally vertical!
-
7:08 - 7:18So everytime I run the program it's going to be drawing some random line.
-
7:18 - 7:25One nice thing about Processing is that you can make animations and it's very easy.
-
7:25 - 7:32I'm going to delete all these points here. Just leave the line.
-
7:32 - 7:42I have to remember how to do this... it's 'void draw()'.
-
7:42 - 7:45Let's see if it works and then I explain what it does.
-
7:45 - 7:49Wow! That's a lot of lines!
-
7:49 - 7:53It's becoming all black.
-
7:53 - 7:56What is it doing?
-
7:56 - 8:03This is one nice thing in Processing. Of course, in other languages, you can do graphics and draw...
-
8:03 - 8:07... but usually it's much more complicated than this.
-
8:07 - 8:14Here with just two lines of code we're making this animation.
-
8:14 - 8:23'draw()' is a special function, that's already defined. It allows you to run something continuously.
-
8:23 - 8:24What does it mean?
-
8:24 - 8:31You know movies have frames. They have many frames per second so...
-
8:31 - 8:37... when you show many images per second you get the illusion of movement, animation.
-
8:37 - 8:42Well, that's what draw is doing here.
-
8:42 - 8:51You just have to know that anything you put inside this 'void draw() { }'.
-
8:51 - 8:57All the lines of code that are in between will be run many times per second.
-
8:57 - 9:03That's why it's drawing random lines many times per second. So when you run it...
-
9:03 - 9:12Because we're not erasing anything it's becoming slowly all black.
-
9:12 - 9:22It would be nice if we would erase the last line. Then it would look as if the line's moving.
-
9:22 - 9:27This will be the last instruction I explain on this video.
-
9:27 - 9:31It's called 'background'.
-
9:31 - 9:42Let's go to the manual and check it out. Using 'ctrl+f', searching for 'background'.
-
9:42 - 9:53Here's some examples. Looks like 'background(51)' makes this grey background.
-
9:53 - 9:57If we use these numbers it makes a yellow one....
-
9:57 - 10:01... looks like we can even use and image as background.
-
10:01 - 10:08The 'background' function sets the color used for the backround of the Processing window.
-
10:08 - 10:12The default background is light grey. We noticed that.
-
10:12 - 10:20If we run it again you can see... oh what did I break?
-
10:20 - 10:23Sorry I had this 'backround()' which is not finished.
-
10:23 - 10:30Ok I run this. You can see the backround is light grey.
-
10:30 - 10:33What else does it say?
-
10:33 - 10:40In the 'draw' function the backround color is used to clear the display at the beginning of each frame.
-
10:40 - 10:44That's exactly what we want to do right now.
-
10:44 - 10:50I like this yellow here so I'm going to copy this line.
-
10:50 - 10:55I'll paste this here...
-
10:55 - 10:57So what will happen now?
-
10:57 - 11:04Many times per second, this function is going to be called. The first thing it will do is make the whole screen yellow.
-
11:04 - 11:09Then it will draw a random line. Let's check it out.
-
11:09 - 11:14I don't know if you can see, but in my computer it's going super fast.
-
11:14 - 11:20Drawing all these crazy lines. Jumping up and down.
-
11:20 - 11:29I think that's enough for today. You can already do some experiments.
-
11:29 - 11:35You can try replacing this line by maybe some random dots.
-
11:35 - 11:42You can also try changing these background values to see what happens. Try getting different colors.
-
11:42 - 11:52If you check the reference you can see sometimes it's using one number, sometimes it's using 3 numbers.
-
11:52 - 11:57Or even images. So you can experiment.
-
11:57 - 12:05One thing I hope to show you is that it's good to experiment.
-
12:05 - 12:10To search on the manual, to search on the internet and go changing things.
-
12:10 - 12:18Usually you can't break anything too big playing here in Processing.
-
12:18 - 12:22Either it will work or it will complain with some error.
-
12:22 - 12:25Then you just have to find the bug and fix it.
- Title:
- Learn programming 3: Create an animation, use random()
- Description:
-
A short review. What happens if we forget a semicolon at the end of the line? A little more detailed explanation of the display as a grid of pixels, with hundreds of rows and columns. We try out the function random() to make our program create a different image each time. Finally we try out the function draw() and background() to be able to animate lines and change the background color.
- Video Language:
- English
- Duration:
- 12:25
lfz edited English subtitles for Learn programming 3: Create an animation, use random() | ||
lfz edited English subtitles for Learn programming 3: Create an animation, use random() | ||
lfz edited English subtitles for Learn programming 3: Create an animation, use random() | ||
lfz added a translation |