Codekids Lesson 6Animation |
Now, you've learned how to make circles and how to do while loops. In this lesson, we'll put these together with two new things, random numbers and mouse clicks. Random NumbersProcessing lets you pick some random number using the random() function. For example the following command picks a random number between 0 and 255: random(0,255); One thing weird about this is that it could be ANY random number between 0 and 255, like 47.658902 or 0.00003! So, in our program we use another command to convert the random number into a regular "integer" or counting number: mrRandom = random(0,255); What this means is:
Mouse ClicksProcessing lets you know where the mouse is and when it gets clicked. When someone clicks the mouse down, the mousePressed() function automatically does whatever code you put between the curly braces: void mousePressed(){ } Then, when you draw stuff, you can use mouseX and mouseY to draw it at the place where the mouse cursor is. For example, what does this code mean? void mousePressed(){
} It means: "Draw a circle, centred where the mouse cursor is, that is circleSize pixels wide and circleSize pixels tall." Your Ninth ProgramNow, let's try using the random() function and mouse information in a new program. Start up Processing and copy this code into the program:
When it plays, it will draw rings in random colors and sizes, centered at the place where you click. Like this:
Of course, it will be different for everyone, because the circle sizes and colors are all random. How does it work?We'll learn about that in class. Hack your ProgramNow that you've got your ninth program running, do these things:
If you create something cool, use Grab to copy it and email it to me! |
|
|
|
|