-
So now that we learned how to use text,
let's get a bit more fancy.
-
The first fancy thing is that now we're
-
going to start using the word "string".
-
You might think, "String?
That's what my cat plays with."
-
But for programmers,
string is a special name
-
we use for bits of text.
-
You can think of it like
a string of characters, if it helps.
-
Your name is a string,
your school's name is a string,
-
and every text message
you've ever sent is also a string.
-
Basically, just think, string equals text.
-
So we used drawing
and coloring with strings already.
-
Could we use variables
with strings? Of course!
-
Just like you might normally
put a number into a variable,
-
you could also put a string
into a variable.
-
We just say something like,
-
"var myName = (whatever your name is)"
so "Sophia";
-
remembering those quotes.
-
And then we put the variable myName
-
into the quoted text
instead of writing "Sophia" directly,
-
like we were doing before.
-
And this way though it
looks the same right now,
-
we can actually,
write this multiple times
-
you know, one time there, one time there,
-
you know maybe we
can be a little artistical.
-
And then all you have to do
is to change this one variable
-
to be your name, and then you have
-
your name written three times.
-
So, just flipping that back,
what if we want to get
-
kind of crazy, can we
start adding together strings?
-
Like you remember when we maybe
started adding together variables?
-
Well we can actually do that,
-
the computer will just
stick the two strings together.
-
So for example, you know,
we can get rid of this,
-
to do it only one time
and then we can use this other variable
-
message that's going to be my name,
plus say a bunch of exclamation marks.
-
And if we stick message into text
to draw this variable
-
instead of my name,
we can see that
-
all it did is just stuck those exclamation marks
-
that we had onto the back of my name.
-
And we can even make this
a little bit more exciting,
-
if we wanted to write message, say, twice.
-
So this looks pretty reasonable,
you know adding strings means
-
you stick one string
onto the back of the other string.
-
But you might be thinking,
can we get really crazy?
-
And start multiplying or dividing
or subtracting strings?
-
Could we do that? Well no we can't,
-
with strings we can only add them.
-
You're right that we could divide
and subtract with number variables,
-
but string variables only allow us to add.
-
But still being curious and asking
those sorts of questions,
-
is the right attitude in programming.
-
You might think, "What would
it mean to divide a string?"
-
So we can also use animation
and mouse interaction with text strings.
-
For example, we can make the string
follow the mouse around.
-
Just by sticking it in a draw loop
like we did with the rectangles.
-
So if you think about how to do this,
we would say var draw,
-
and then we'll stick all of this, right in here.
-
This probably looks really familiar,
from "intro to animation."
-
And you might think, alright, well to
make it follow the mouse around,
-
we certainly need to use
mouseX, and mouseY
-
and there you have it we're
actually painting with our name.
-
And of course if we wanted,
we could easily set a background,
-
say to, you know, a nice light blue.
-
And now we have that the text
is just following the mouse around,
-
just like we saw with the rectangles.
-
And now as a last fun trick,
let's use animation,
-
and make the text size
grow and grow and grow.
-
So, here we're setting the textSize to 30,
as we learned in variables
-
instead of using 30, we could also say,
var howBig equals 30;
-
and then use this variable
instead of doing 30 directly.
-
Well that's the exact same thing,
-
and we need to obviously
be changing something
-
in order to get an animation.
-
So we can do that by saying,
how big equals how big plus one
-
which actually means,
if you remember howBig gets,
-
you know how big it was before,
plus one to make it a little bit larger
-
and now we just need to
restart the program,
-
and here we have that
as we're moving the mouse around,
-
the text size is growing and growing
and growing and growing
-
because of this line right here.
-
And you know, just on your own,
-
you might think, okay how
can we make it grow faster?
-
So now, not only do you know how
to manipulate shapes with code,
-
but you can also use text with code as well.