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