// Declare and initialize your variables!
float r = 100;
float g = 150;
float b = 200;
float a = 200;
float diam = 20;
float x = 100;
float y = 100;
void setup() {
size(480, 270);
background(255);
}
void draw() {
// Use those variables to draw an ellipse
stroke(0);
// (Remember, the fourth argument for a color is transparency.
fill(r, g, b, a);
ellipse(x, y, diam, diam);
}
random() is a special kind of function. It's a function that returns a value . You have encountered this before. Think back to when you used the abs() function to calculate the absolute value of a number. The idea of a function that calculates a value and returns it will be explored later. For now, just let that idea sink in and start to marinate.
Unlike most of the functions you are comfortable with (ex: line(), ellipse(), and rect()) random() does not draw a color or shape on the screen. Instead, random() answers a question: it returns that answer to you. Here is a bit of dialogue to explain.
Random: Like, no problem dude. How about the number 63?
Now, how would this sequence look in a more formal, Processing environment? The code below is the part of "me" and is played by the variable w.
float w = random(1, 100); // a random float between 1 and 100
rect(100, 100, w, 50);
The random() function requires two arguments and returns a random floating point number ranging from the first argument to the second. The second argument must be larger than the first for it to work properly. The function random() also works with one argument by assuming a range between zero and that argument.
In addition, random() only returns floating point numbers. This is why w is declared as a float . However, if you want a random integer, you can covert the result of the random function to an int. This is called CASTING (the process of converting one data type into another).
int w = int(random(1,100)); // a random integer between 1 and 100;
rect(100, 100, w, 50);
New Feature #1 - Zoog will rise from below the screen and fly off into space (above the screen).
New Feature #2 - Zoog's eyes will be colored randomly as Zoog moves.
// Example 4-8: Variable Zoog
// NEW FEATURE #1: Zoog will rise from below the screen
// and fly off into space (above the screen.)
// NEW FEATURE #2: Zoog's eyes will be colored randomly as Zoog moves.
// Declaring Variables.
// zoogX and zoogY are for feature #1.
// eyeR, eyeG, eyeB are for feature #2.
float zoogX;
float zoogY;
float eyeR;
float eyeG;
float eyeB;
void setup() {
// Set the size of the window
size(480, 270);
// Feature #1. zoogX and zoogY are initialized
// based on the size of the window. Note we cannot
// initialize these variables before the size()
// function is called since we are using the built-in
// variables width and height.
zoogX = width/2; // Zoog always starts in the middle
zoogY = height + 100; // Zoog starts below the screen
}
void draw() {
background(255); // Draw a white background
// Set ellipses and rects to CENTER mode
ellipseMode(CENTER);
rectMode(CENTER);
// Draw Zoog's body
stroke(0);
fill(150);
// Feature #1. zoogX and zoogY are used for the shape locations.
rect(zoogX, zoogY, 20, 100);
// Draw Zoog's head
stroke(0);
fill(255);
ellipse(zoogX, zoogY-30, 60, 60);
// Draw Zoog's eyes
// Feature #2. eyeR, eyeG, and eyeB are given
// random values and used in the fill() function.
eyeR = ___________;
eyeG = ____________;
eyeB = _____________;
fill(eyeR, _______, eyeB);
ellipse(zoogX-19, zoogY-30, 16, 32);
ellipse(zoogX+19, zoogY-30, 16, 32);
// Draw Zoog's legs
stroke(150);
line(zoogX-10, zoogY+50, zoogX-10, height);
line(zoogX+10, zoogY+50, zoogX+10, height);
// Equation so Zoog moves up
_____________________;
}
zoogX = ______________________________;
#4 Can you code a circle to be in the middle of your screen and only move back and forth -2 and 2 pixels. Use random.