0. Other games you can play
What if we want to make the lightning bolt move faster? Change the line where we are incrementing the value of x to something like:
x = x + 5;What if we want to make the lightning bolt move slower? Change the line where were are incrementing the value of x to something like:
x = x + 0.5;
If you haven't programmed before, then it may take some extra thinking to grasp the meaning of an expression such as x = x + 5;. If this were math class, then you'd quickly realize that there is no such value of x that will make the equation x = x + 5 work. You'd collect like terms and be left with the expression 0 = 5 which is no good. But this isn't math class.
Two things to remember here:
- Recall what I said before about equals signs. One is telling, two is asking.
- Math and math-like actions are done right to left in pretty much all procedural programming languages, including this one.
So the expression x = x + 5; is not there to ask the computer to solve for x. What it is saying is: take the value of x that you have in memory right now, add 5 to it, and then take whatever answer you get and reassign that number to x. (Tell x to be the new value you just computed.)
If x were 1, then after performing the command x = x + 5, the value of x will be 6. Any other command or expression performed from here on out will substitute "6" every time x shows up in the program, until x gets reassigned to something else.
1. Float variables
There's no rule that says we have to use whole numbers, except for the fact that when we first declared x in our program, we declared it as an integer. That means we cannot give it a fractional part. Therefore if you write x=x+0.5 like I suggested you try out, you will get an error telling you "cannot convert from float to int." What's that about?
If we want to be able to allow x to have a fractional part, we need x to be a floating point number instead of an integer. These are called "floats" in Processing. A float is a floating-point number, which is a number that is not an integer, such as 1.5 or PI (π). Why bother with the difference between ints and floats? Why not just call them all “numbers” or something? Well, one difference between a float and an int is the amount of memory your computer has to allocate to store it. Also, integers are precise and floats are not.
Let’s say I have a cookie. I can declare int numCookie = 1 to tell Processing that I have exactly one cookie. If I’m going to share it with 6 other friends of mine I will break it into seven pieces. If I am really accurate at cookie-breaking we will all get exactly one seventh of the cookie. We can write this precisely as 1/7 on paper. However, if we wanted to express that in a Processing program, we’d probably declare something like
float cookiePortion0 = 0.14; float cookiePortion1 = 0.14; float cookiePortion2 = 0.14; float cookiePortion3 = 0.14; float cookiePortion4 = 0.14; float cookiePortion5 = 0.14; float myCookiePortion = 0.14;
If we add up all the floats we get 0.98, not 1. We could write out each number to more decimal places. 1/7 is actually 0.142857 . . . but it is a repeating decimal. We can’t write an infinite number of digits past the decimal because we don’t want our entire computer’s memory taken up by deciding how much of a cookie each person is getting. The computer has to truncate the number at some point, and it will probably be good enough, but it is not perfect. That's the difference between an int and a float.
In our lightning bolt program, we'll change the declaration of the variable x up at the top to say
float x = 10;
and then we are all set to increment x by a float later on in the program.
We also don't have to make the lightning bolt move from right to left. It can go wherever we want, limited by imagination only.
To make it go down instead of sideways, increment y instead of x, like so:
y++; if (y == height) { y = 0; }
If you want the lightning bolt to move diagonally, change both x and y with each loop through draw.
If you want the lightning bolt to move back and forth, you have to find a way to use the if test to change the direction of motion in x or y or both. Try it yourself and then check my screencast below to see how I did it.