So I am making a palette and am trying to learn for loops(nested loops) and arrays. I am having difficulty understanding the mechanics of them as well as how I am to implement them to this project.
I will explain first what I am trying to do.
I need to draw a palette with 7 colors or more. Then draw an empty grid with circles covering each row and column (will explain later). I need to draw an identical grid next to the empty one however, instead of being empty I need each individual rectangle to have a random color out of the 7 colors I have chosen. This is because I want to program that the goal is to match the empty grid with that colored grid.
I want to be able to, when I click on a rectangle, to fill that rectangle with whatever color I chose from the palette. And if I select a color and then a circle or multiple circles, I want to color in those entire rows or columns with that color. I also need a counter at the bottom which counts how many moves it takes me to successfully replicate the grid next to it.
As of now I need help turning my many lines of rect and ellipse code into for loops to make it better and help on how to code to color a square or row I want colored and also to make that second grid with the filled in random colors. I know this is a lot but I am stumped and confused.
I right now have it that when the mouse selects a color it just draws a line of the color it has selected as well as a lot of lines of code that I need to turn into for loops. Whoever has read this far and wants to help, I sincerely thank you for your services. Hereās my code so far:
final int PALETTE_HEIGHT = 30; //The height of the palette at the bottom
final int PALETTE_WIDTH = 30; //The width of EACH choice item in the palette
final int PALETTE_SPACING = 40;
final int RED_LEFT = 50; //The red button is right in the corner
final int GREEN_LEFT = RED_LEFT+PALETTE_SPACING; //The green button is next to it
final int BLUE_LEFT = RED_LEFT+2*PALETTE_SPACING; //Then the blue button
final int YELLOW_LEFT = RED_LEFT+3*PALETTE_SPACING; //Then the yellow button
final int PURPLE_LEFT = RED_LEFT+4*PALETTE_SPACING; //Then the purple button
final int PINK_LEFT = RED_LEFT+5*PALETTE_SPACING; //Then the pink button
final int ORANGE_LEFT = RED_LEFT+6*PALETTE_SPACING; //Then the orange button
final int PALETTE_RIGHT = ORANGE_LEFT+PALETTE_WIDTH;
int rectWidth = 20, rectHeight = 20;
int ellipseDiam = 15;
void setup(){
size(500,500);
background(0);
drawPalette();
strokeWeight(1);
}
void draw(){
mousePressed();
grid();
circleSelectors();
numMoves();
}
void drawPalette(){
fill(255,0,0); //The red button
rect(RED_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
fill(0,255,0); //The green button
rect(GREEN_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
fill(0,0,255); //The blue button
rect(BLUE_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
fill(#F4F539); //yellow
rect(YELLOW_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
fill(#EB39F5); //purple
rect(PURPLE_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
fill(#F716CF); //pink
rect(PINK_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
fill(#F7C316); //orange
rect(ORANGE_LEFT, 0, PALETTE_WIDTH, PALETTE_HEIGHT);
}
void chooseColour(){
if(mouseX < GREEN_LEFT-10)
stroke(255, 0, 0); //It's left of green, so it's in the red button
else if(mouseX < BLUE_LEFT-10 && mouseX > GREEN_LEFT)
stroke(0, 255, 0); //Not far enough over for blue, so it's green
else if(mouseX < YELLOW_LEFT-10 && mouseX > BLUE_LEFT)
stroke(0, 0, 255);
else if(mouseX < PURPLE_LEFT-10 && mouseX > GREEN_LEFT)
stroke(#F4F539);
else if(mouseX < PINK_LEFT-10 && mouseX > PURPLE_LEFT)
stroke(#EB39F5);
else if(mouseX < ORANGE_LEFT-10 && mouseX > PINK_LEFT)
stroke(#F716CF);
else if(mouseX < PALETTE_RIGHT && mouseX > ORANGE_LEFT)
stroke(#F7C316);
}
void mousePressed(){
//Don't do anything until the mouse button is pressed
if(mousePressed) {
//First test to see if the mouse is anywhere in the palette
if(mouseX<PALETTE_RIGHT &&
mouseX>RED_LEFT &&
mouseY < (PALETTE_HEIGHT)) {
//It's in the palette. Set the colour according to mouseX
chooseColour();
}//if mouse in palette
else {
//Mouse is not in the palette - draw a line in that case
line(pmouseX,pmouseY,mouseX,mouseY);
}
}//if mouse pressed
}
void grid(){
noFill();
strokeWeight(1);
stroke(255);
//Column 1
rect(50, 100, rectWidth, rectHeight);
rect(50, 120, rectWidth, rectHeight);
rect(50, 140, rectWidth, rectHeight);
rect(50, 160, rectWidth, rectHeight);
rect(50, 180, rectWidth, rectHeight);
rect(50, 200, rectWidth, rectHeight);
rect(50, 220, rectWidth, rectHeight);
rect(50, 240, rectWidth, rectHeight);
rect(50, 260, rectWidth, rectHeight);
rect(50, 280, rectWidth, rectHeight);
rect(50, 300, rectWidth, rectHeight);
rect(50, 320, rectWidth, rectHeight);
//Column 2
rect(70, 100, rectWidth, rectHeight);
rect(70, 120, rectWidth, rectHeight);
rect(70, 140, rectWidth, rectHeight);
rect(70, 160, rectWidth, rectHeight);
rect(70, 180, rectWidth, rectHeight);
rect(70, 200, rectWidth, rectHeight);
rect(70, 220, rectWidth, rectHeight);
rect(70, 240, rectWidth, rectHeight);
rect(70, 260, rectWidth, rectHeight);
rect(70, 280, rectWidth, rectHeight);
rect(70, 300, rectWidth, rectHeight);
rect(70, 320, rectWidth, rectHeight);
//Column 3
rect(90, 100, rectWidth, rectHeight);
rect(90, 120, rectWidth, rectHeight);
rect(90, 140, rectWidth, rectHeight);
rect(90, 160, rectWidth, rectHeight);
rect(90, 180, rectWidth, rectHeight);
rect(90, 200, rectWidth, rectHeight);
rect(90, 220, rectWidth, rectHeight);
rect(90, 240, rectWidth, rectHeight);
rect(90, 260, rectWidth, rectHeight);
rect(90, 280, rectWidth, rectHeight);
rect(90, 300, rectWidth, rectHeight);
rect(90, 320, rectWidth, rectHeight);
//Column 4
rect(110, 100, rectWidth, rectHeight);
rect(110, 120, rectWidth, rectHeight);
rect(110, 140, rectWidth, rectHeight);
rect(110, 160, rectWidth, rectHeight);
rect(110, 180, rectWidth, rectHeight);
rect(110, 200, rectWidth, rectHeight);
rect(110, 220, rectWidth, rectHeight);
rect(110, 240, rectWidth, rectHeight);
rect(110, 260, rectWidth, rectHeight);
rect(110, 280, rectWidth, rectHeight);
rect(110, 300, rectWidth, rectHeight);
rect(110, 320, rectWidth, rectHeight);
//Column 5
rect(130, 100, rectWidth, rectHeight);
rect(130, 120, rectWidth, rectHeight);
rect(130, 140, rectWidth, rectHeight);
rect(130, 160, rectWidth, rectHeight);
rect(130, 180, rectWidth, rectHeight);
rect(130, 200, rectWidth, rectHeight);
rect(130, 220, rectWidth, rectHeight);
rect(130, 240, rectWidth, rectHeight);
rect(130, 260, rectWidth, rectHeight);
rect(130, 280, rectWidth, rectHeight);
rect(130, 300, rectWidth, rectHeight);
rect(130, 320, rectWidth, rectHeight);
//Column 6
rect(150, 100, rectWidth, rectHeight);
rect(150, 120, rectWidth, rectHeight);
rect(150, 140, rectWidth, rectHeight);
rect(150, 160, rectWidth, rectHeight);
rect(150, 180, rectWidth, rectHeight);
rect(150, 200, rectWidth, rectHeight);
rect(150, 220, rectWidth, rectHeight);
rect(150, 240, rectWidth, rectHeight);
rect(150, 260, rectWidth, rectHeight);
rect(150, 280, rectWidth, rectHeight);
rect(150, 300, rectWidth, rectHeight);
rect(150, 320, rectWidth, rectHeight);
//Column 7
rect(170, 100, rectWidth, rectHeight);
rect(170, 120, rectWidth, rectHeight);
rect(170, 140, rectWidth, rectHeight);
rect(170, 160, rectWidth, rectHeight);
rect(170, 180, rectWidth, rectHeight);
rect(170, 200, rectWidth, rectHeight);
rect(170, 220, rectWidth, rectHeight);
rect(170, 240, rectWidth, rectHeight);
rect(170, 260, rectWidth, rectHeight);
rect(170, 280, rectWidth, rectHeight);
rect(170, 300, rectWidth, rectHeight);
rect(170, 320, rectWidth, rectHeight);
//Column 8
rect(190, 100, rectWidth, rectHeight);
rect(190, 120, rectWidth, rectHeight);
rect(190, 140, rectWidth, rectHeight);
rect(190, 160, rectWidth, rectHeight);
rect(190, 180, rectWidth, rectHeight);
rect(190, 200, rectWidth, rectHeight);
rect(190, 220, rectWidth, rectHeight);
rect(190, 240, rectWidth, rectHeight);
rect(190, 260, rectWidth, rectHeight);
rect(190, 280, rectWidth, rectHeight);
rect(190, 300, rectWidth, rectHeight);
rect(190, 320, rectWidth, rectHeight);
}
void circleSelectors(){
noFill();
strokeWeight(1);
stroke(255);
//Row
ellipse(60, 80, ellipseDiam, ellipseDiam);
ellipse(80, 80, ellipseDiam, ellipseDiam);
ellipse(100, 80, ellipseDiam, ellipseDiam);
ellipse(120, 80, ellipseDiam, ellipseDiam);
ellipse(140, 80, ellipseDiam, ellipseDiam);
ellipse(160, 80, ellipseDiam, ellipseDiam);
ellipse(180, 80, ellipseDiam, ellipseDiam);
ellipse(200, 80, ellipseDiam, ellipseDiam);
//Column
ellipse(30, 110, ellipseDiam, ellipseDiam);
ellipse(30, 130, ellipseDiam, ellipseDiam);
ellipse(30, 150, ellipseDiam, ellipseDiam);
ellipse(30, 170, ellipseDiam, ellipseDiam);
ellipse(30, 190, ellipseDiam, ellipseDiam);
ellipse(30, 210, ellipseDiam, ellipseDiam);
ellipse(30, 230, ellipseDiam, ellipseDiam);
ellipse(30, 250, ellipseDiam, ellipseDiam);
ellipse(30, 270, ellipseDiam, ellipseDiam);
ellipse(30, 290, ellipseDiam, ellipseDiam);
ellipse(30, 310, ellipseDiam, ellipseDiam);
ellipse(30, 330, ellipseDiam, ellipseDiam);
}
void numMoves(){
fill(255);
textSize(18);
rect(150, height-80, 200, 30);
String word = "Num moves: ";
int x = 0;
String score = word + x;
fill(0);
text(score, 180, height-60);
}