< Return to Video

More Mouse Interaction (Video Version)

  • 0:01 - 0:03
    In our last talk-through,
  • 0:03 - 0:05
    we showed how you can animate
    a ball bouncing off the walls
  • 0:05 - 0:08
    using the draw function and if statements.
  • 0:08 - 0:09
    Let's review.
  • 0:09 - 0:12
    First, we set up some initial variables
    for position and speed of a ball.
  • 0:12 - 0:15
    Then, in the draw function,
    which is that special function
  • 0:15 - 0:18
    that gets called over and over
    when your program is running,
  • 0:18 - 0:21
    we repaint the background
    and draw an ellipse on the canvas
  • 0:21 - 0:24
    and we position that ellipse
    based on the position variable
  • 0:24 - 0:27
    and the speed
    and how they affect each other.
  • 0:27 - 0:29
    Now, without if statements,
  • 0:29 - 0:31
    our ball just kept on going
    forever and ever,
  • 0:31 - 0:33
    or until we pressed restart.
  • 0:33 - 0:37
    So we added two if statements down here
    to check and see
  • 0:37 - 0:40
    if the ball was near
    the right side of the screen
  • 0:40 - 0:41
    or the left side of the screen,
  • 0:42 - 0:44
    and if so, we change the speed
    to be positive or negative
  • 0:44 - 0:46
    so that the ball
    basically would bounce back.
  • 0:46 - 0:50
    So now we just have this ball,
    bouncing back and forth forever.
  • 0:50 - 0:51
    So that was pretty cool,
  • 0:51 - 0:53
    and there's a lot
    of really cool animations
  • 0:53 - 0:55
    that we can make with that.
  • 0:55 - 0:58
    But now, I want to add user interaction
    to this program.
  • 0:58 - 1:00
    See, right now,
    this program's like a TV show.
  • 1:00 - 1:02
    if you gave it to a friend,
  • 1:02 - 1:04
    and your friend didn't know
    how to program,
  • 1:04 - 1:06
    they couldn't really interact with it.
  • 1:06 - 1:08
    All they could do is watch it,
    which is cool,
  • 1:08 - 1:11
    but it's a lot more cool
    if they could actually do something.
  • 1:11 - 1:14
    So, let's give the user
    some ways to control it.
  • 1:14 - 1:15
    Remember earlier, we learned
  • 1:15 - 1:20
    about two special global variables
    called mouseX and mouseY.
  • 1:21 - 1:23
    Those variables return numbers
  • 1:23 - 1:26
    that tell us about the current position
    of the user's mouse
  • 1:26 - 1:29
    and they're a great way
    to make a program more interactive.
  • 1:29 - 1:31
    So, let's see. How can we use them?
  • 1:31 - 1:35
    Well, we should use them
    inside the draw function somewhere.
  • 1:35 - 1:37
    Because that's the only code
  • 1:37 - 1:39
    that's called over and over
    as the program runs.
  • 1:40 - 1:43
    Everything outside of draw
    is only called once,
  • 1:43 - 1:45
    when the program first starts.
  • 1:45 - 1:48
    So, it doesn't make sense
    to use mouseX and mouseY there.
  • 1:48 - 1:51
    The user hasn't had a chance
    to interact with it.
  • 1:51 - 1:57
    In draw, we're drawing the ball
    200 pixels down the screen right now.
  • 1:57 - 2:01
    How about we just replace
    that with mouseY?
  • 2:01 - 2:03
    Because that is the y position, right?
  • 2:03 - 2:08
    This way it will just add the y position
    dependent on where the user's y is.
  • 2:08 - 2:10
    Right? So check this out.
  • 2:10 - 2:12
    By just moving my cursor up and down,
  • 2:12 - 2:15
    I can change the line
    that the ball moves along.
  • 2:15 - 2:16
    That's pretty cool.
  • 2:16 - 2:20
    But I want to use mouseX, too.
    So, how should I use that?
  • 2:20 - 2:22
    Well, why don't we just make another ball
  • 2:22 - 2:26
    and have that ball going
    in the opposite direction: up and down.
  • 2:26 - 2:31
    And there we'll just have
    the user control the x position of that.
  • 2:31 - 2:38
    So we kind of just do the reverse.
    We'll say ellipse mouseX position 50 50.
  • 2:40 - 2:42
    Alright? Check this out!
  • 2:42 - 2:47
    I've got these two balls that I control,
    and going in perpendicular directions.
  • 2:49 - 2:53
    But, I'm still not happy.
    I want to give the user even more control.
  • 2:53 - 2:58
    I want to give the user the power
    to start up the second ball.
  • 2:58 - 3:02
    To actually bring it into existence,
    just by pressing their mouse.
  • 3:02 - 3:04
    Well, then I need to figure out
  • 3:04 - 3:08
    how to tell that the user
    is pressing their mouse.
  • 3:08 - 3:13
    Thankfully, we have a super special
    Boolean variable for just that.
  • 3:13 - 3:18
    It's called mouseIsPressed
    and we can use it inside an if statement.
  • 3:18 - 3:22
    So, let's see. This is our second ball.
  • 3:22 - 3:27
    So we can say if mouseIsPressed,
  • 3:27 - 3:32
    and then we'll move
    the ellipse colon to there.
  • 3:32 - 3:34
    So, what this is doing here,
  • 3:34 - 3:39
    is telling the program
    that we only want to draw this ellipse
  • 3:39 - 3:44
    if this is true and mouseIsPressed
    will only be true
  • 3:44 - 3:47
    if the user is pressing their mouse.
  • 3:47 - 3:50
    So, let's try it. Ta da!
  • 3:50 - 3:53
    So now I can make the ball appear
    whenever I press.
  • 3:53 - 3:56
    So it's zooming in
    from this parallel universe.
  • 3:56 - 3:59
    In! In! In! It's awesome!
  • 4:00 - 4:05
    So, the interesting thing
    about this variable mouseIsPressed
  • 4:05 - 4:08
    is that it changes based on
    what the user does
  • 4:08 - 4:10
    not based on what our program does.
  • 4:10 - 4:13
    and since the draw function
    gets called repeatedly over and over,
  • 4:13 - 4:16
    the output of our program
    will change over time
  • 4:16 - 4:19
    just with a little bit of user input.
  • 4:19 - 4:22
    With the combined power
    of if statements and mouseIsPressed,
  • 4:22 - 4:24
    you have everything you need
  • 4:24 - 4:27
    to make awesome things like buttons,
    and drawing programs.
  • 4:27 - 4:28
    Woo hoo!
Title:
More Mouse Interaction (Video Version)
Description:

This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/

more » « less
Video Language:
English
Duration:
04:29

English subtitles

Revisions