Return to Video

Learn programming 3: Create an animation, use random()

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

more » « less
Video Language:
English
Duration:
12:25

English subtitles

Revisions