Created
March 30, 2016 20:36
-
-
Save sabotai/a6f1e1e22a7c446c2533e53561dcdafa to your computer and use it in GitHub Desktop.
For Loop/Array Flip Switch Example for GD105
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int howMany = 45; //variable to keep track of how many | |
int[] x = new int[howMany]; //lets make a new array to keep track of x values | |
int[] y = new int[howMany]; //lets make a new array to keep track of y values | |
boolean[] beenClicked = new boolean[howMany]; //lets make a new array to keep track of which ones have been clicked | |
int dia = 100; //diameter of our ellipses | |
void setup(){ | |
size(800,600); | |
for(int i = 0; i < howMany; i++){ //assign each of our x and y positions | |
x[i] = int(random(0,width)); //random x position up to the width of the window | |
y[i] = int(random(0,height));//random y position up to the height of the window | |
beenClicked[i] = false; //by default, none of them have been clicked | |
} | |
} | |
void draw(){ | |
background(0); //draw our background each frame | |
for(int i = 0; i < howMany; i++){ //lets use a for loop to draw each one of our circles, because ... lazy | |
if (mousePressed && (dist(mouseX,mouseY,x[i],y[i]) < dia/2)){ //if the mouse is hovering i.e. the distance from the center is less than the radius | |
beenClicked[i] = !beenClicked[i]; | |
} | |
if (beenClicked[i]){ | |
fill(random(50)); //those that are hovered over should change to a dark color | |
} else { | |
fill(255); //those not being hovered over should be white | |
} | |
ellipse(x[i],y[i],dia,dia); //draw each ellipse with its x and y positions, with a diameter = to dia | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment