Help! I'm completely stuck :/
in
Programming Questions
•
5 months ago
Hey there,
I'm trying to make a program which has a blue cell moved by the arrow keys and when the mouse is pressed a green cell appears in the cell it was pressed in and a path is drawn across and down or up from the blue to the green cell. If you replace the "GreyScaleShade" under fill in each for loop with "random" and comment out the two lines above it it will do this with the path constantly in a random colour. However, I need the path to be shaded from white to grey in evenly spaced shades of grey from 255 next to the blue cell to 0 next to the grey cell. I have already achieved this for the condition when you click the mouse inside a cell which is to the left or to the right of the blue cell, and I'm pretty sure it would'nt be too hard to do the same if the mouse is clicked somewhere above or below the blue cell. The problem arises when I need this same thing to take place if i click to the right and below the blue cell. In other words, I need the path to be shaded evenly from white to black around a corner. I have absolutely exhausted my brain trying to achieve this and my attempts have unfortunately proven futile. If anyone could help me that would be greatly appreciated.
If you run the code below it will give you "cannot find anything named pathX" which is a bit of an annoyance. Also, my code will look like quite a mess because my variable names are relatively long and because there are quite a lot of parentheses. However, if anyone could get the code to draw an evenly shaded path depending on how many cells are between the blue and green cell with the cell adjacent to the blue being white and the cell adjacent to the green being black I would be truly grateful. If anyone has any more questions as to what exactly it is I'm trying to achieve please ask, but I feel I have described it as best as I can above.
Here is the code that I have written so far, I hope it isn' too jumbled up. Any help is greatly appreciated.
TAB 1
int n = 8;
float cellwidth;
float CellVariableX = 0.0;
float CellVariableY = 0.0;
float GreyScaleColour = 255.0;
boolean GreenCellActivated;
int MouseVariableX;
int MouseVariableY;
int MouseVariableY;
float GreyScaleShade;
void setup() {
size(300, 300);
background(255);
cellwidth = width/(n+0.0);
}
size(300, 300);
background(255);
cellwidth = width/(n+0.0);
}
void draw() {
background(255);
CellVariableX = constrain(CellVariableX, 0.0, n-1.0);
CellVariableY = constrain(CellVariableY, 0.0, n-1.0);
for (float n = 0; n <= width; n = n+cellwidth) {
line(0, n, width, n);
line(n, 0, n, width);
fill(0, 7, 185);
rect(CellVariableX*cellwidth, CellVariableY*cellwidth, cellwidth, cellwidth);
}
background(255);
CellVariableX = constrain(CellVariableX, 0.0, n-1.0);
CellVariableY = constrain(CellVariableY, 0.0, n-1.0);
for (float n = 0; n <= width; n = n+cellwidth) {
line(0, n, width, n);
line(n, 0, n, width);
fill(0, 7, 185);
rect(CellVariableX*cellwidth, CellVariableY*cellwidth, cellwidth, cellwidth);
}
if (GreenCellActivated == true) {
for (float pathX = (CellVariableX*cellwidth) + cellwidth; pathX < (PositionDifferenceX (MouseVariableX, width/n))*cellwidth; pathX = pathX + cellwidth) {
float GreyControlVariableXPositive = (((pathX - (CellVariableX*cellwidth))/cellwidth)-1);
GreyScaleShade = 255.0 - ((GreyScaleCellColour (GreyScaleColour, (((PositionDifferenceX (MouseVariableX, width/n)) - ((CellVariableX) + 1))-1)))*GreyControlVariableXPositive);
fill(GreyScaleShade);
rect(pathX, CellVariableY*cellwidth, cellwidth, cellwidth);
}
for (float pathX = (CellVariableX*cellwidth) + cellwidth; pathX < (PositionDifferenceX (MouseVariableX, width/n))*cellwidth; pathX = pathX + cellwidth) {
float GreyControlVariableXPositive = (((pathX - (CellVariableX*cellwidth))/cellwidth)-1);
GreyScaleShade = 255.0 - ((GreyScaleCellColour (GreyScaleColour, (((PositionDifferenceX (MouseVariableX, width/n)) - ((CellVariableX) + 1))-1)))*GreyControlVariableXPositive);
fill(GreyScaleShade);
rect(pathX, CellVariableY*cellwidth, cellwidth, cellwidth);
}
for (float pathX = (CellVariableX*cellwidth) - cellwidth; pathX > (PositionDifferenceX (MouseVariableX, width/n))*cellwidth; pathX = pathX - cellwidth) {
float GreyControlVariableXNegative = ((((CellVariableX*cellwidth) - pathX)/cellwidth)-1);
GreyScaleShade = 255.0 - ((GreyScaleCellColour (GreyScaleColour, ((((CellVariableX) - 1)-(PositionDifferenceX (MouseVariableX, width/n)))-1)))*GreyControlVariableXNegative);
fill(GreyScaleShade);
rect(pathX, CellVariableY*cellwidth, cellwidth, cellwidth);
}
float GreyControlVariableXNegative = ((((CellVariableX*cellwidth) - pathX)/cellwidth)-1);
GreyScaleShade = 255.0 - ((GreyScaleCellColour (GreyScaleColour, ((((CellVariableX) - 1)-(PositionDifferenceX (MouseVariableX, width/n)))-1)))*GreyControlVariableXNegative);
fill(GreyScaleShade);
rect(pathX, CellVariableY*cellwidth, cellwidth, cellwidth);
}
for (float pathY = CellVariableY*cellwidth; pathY < (PositionDifferenceY (MouseVariableY, width/n))*cellwidth; pathY = pathY + cellwidth) {
float GreyControlVariableYPositive = ((((pathX + pathY) - ((CellVariableX*cellwidth) + (CellVariableY*cellwidth)))/cellwidth)-1);
GreyScaleShade = 255.0 - ((GreyScaleCellColour (GreyScaleColour, ((((PositionDifferenceX (MouseVariableX, width/n)) + (PositionDifferenceY (MouseVariableY, width/n))) - ((CellVariableX + CellVariableY)+1))-1))));
fill(GreyScaleShade);
rect(((CellVariableX*cellwidth) + ((PositionDifferenceX (MouseVariableX, width/n))*cellwidth) - (CellVariableX*cellwidth)), pathY, cellwidth, cellwidth);
}
float GreyControlVariableYPositive = ((((pathX + pathY) - ((CellVariableX*cellwidth) + (CellVariableY*cellwidth)))/cellwidth)-1);
GreyScaleShade = 255.0 - ((GreyScaleCellColour (GreyScaleColour, ((((PositionDifferenceX (MouseVariableX, width/n)) + (PositionDifferenceY (MouseVariableY, width/n))) - ((CellVariableX + CellVariableY)+1))-1))));
fill(GreyScaleShade);
rect(((CellVariableX*cellwidth) + ((PositionDifferenceX (MouseVariableX, width/n))*cellwidth) - (CellVariableX*cellwidth)), pathY, cellwidth, cellwidth);
}
for (float pathY = (CellVariableY*cellwidth); pathY > ((PositionDifferenceY (MouseVariableY, width/n))*cellwidth); pathY = pathY - cellwidth) {
fill(random(255), random(255), random(255));
rect(((CellVariableX*cellwidth) + ((PositionDifferenceX (MouseVariableX, width/n))*cellwidth) - (CellVariableX*cellwidth)), pathY, cellwidth, cellwidth);
}
fill(random(255), random(255), random(255));
rect(((CellVariableX*cellwidth) + ((PositionDifferenceX (MouseVariableX, width/n))*cellwidth) - (CellVariableX*cellwidth)), pathY, cellwidth, cellwidth);
}
fill(63, 255, 46);
rect((PositionDifferenceX (MouseVariableX, width/n))*cellwidth, ((PositionDifferenceY (MouseVariableY, width/n))*cellwidth), cellwidth, cellwidth);
rect((PositionDifferenceX (MouseVariableX, width/n))*cellwidth, ((PositionDifferenceY (MouseVariableY, width/n))*cellwidth), cellwidth, cellwidth);
if (CellVariableX*cellwidth == ((PositionDifferenceX (MouseVariableX, width/n))*cellwidth)) {
fill(0, 7, 185);
rect(CellVariableX*cellwidth, CellVariableY*cellwidth, cellwidth, cellwidth);
}
}
}
fill(0, 7, 185);
rect(CellVariableX*cellwidth, CellVariableY*cellwidth, cellwidth, cellwidth);
}
}
}
TAB 2
int PositionDifferenceX (int mousePositionX, int widthOfCell) {
return mousePositionX/widthOfCell;
}
return mousePositionX/widthOfCell;
}
int PositionDifferenceY (int mousePositionY, int widthOfCell) {
return mousePositionY/widthOfCell;
}
return mousePositionY/widthOfCell;
}
float GreyScaleCellColour (float White, float CellsOnPathMinusOne) {
return White/CellsOnPathMinusOne;
}
return White/CellsOnPathMinusOne;
}
void mousePressed() {
GreenCellActivated = true;
MouseVariableX = mouseX;
MouseVariableY = mouseY;
}
TAB 3
void keyPressed() {
if (keyCode == RIGHT) {
CellVariableX += 1;
}
if (keyCode == LEFT) {
CellVariableX -= 1;
}
if (keyCode == UP) {
CellVariableY -= 1;
}
if (keyCode == DOWN) {
CellVariableY += 1;
}
}
if (keyCode == RIGHT) {
CellVariableX += 1;
}
if (keyCode == LEFT) {
CellVariableX -= 1;
}
if (keyCode == UP) {
CellVariableY -= 1;
}
if (keyCode == DOWN) {
CellVariableY += 1;
}
}
1