void setup()
{
frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
if (keyPressed)
{
if (key == CODED)
{
if (keyCode == UP)
{
color c20=color(108,52,131); //PURPLE
fill(c20);
rect(mouseX+150,mouseY+200,200,180);
}
}
}
}
if (keyCode == DOWN)
{
color c24=color(35,155,86);//GREEN
fill(c24);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == LEFT)
{
color c21=color(244,208,63);//YELLOW
fill(c21);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == RIGHT)
{
color c22=color(230,126,34);//ORANGE
fill(c22);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == SHIFT)
{
color c27=color(174,214,241);//BLUE
fill(c27);
rect(mouseX+150,mouseY+200,200,180);
}
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while(i<5)
{
if (keypressed)
{
if (key == 'a')
{
color c34=color(255,51,227);
fill(c34);
rect(mouseX+150,mouseY+200,200,180);
}
i= i+1;
}
}
rect(mouseX+150,mouseY+200,200,180);
color c2=color(0,100,20);
fill(c2);
triangle(mouseX+130,mouseY+200,mouseX+370,mouseY+200,mouseX+250,mouseY+100);
color c3=color(100,10,0);
fill(c3);
rect(mouseX+234,mouseY+310,30,70);
color c4=color(60,120,200);
fill(c4);
rect(mouseX+160,mouseY+240,40,30);
color c5=color(60,120,200);
fill(c5);
rect(mouseX+300,mouseY+240,40,30);
color c6=color(60,120,200);
fill(c6);
ellipse(mouseX+250,mouseY+255,70,50);
color c7=color(249,249,249);
fill(c7);
rect(mouseX+247,mouseY+230,5,50);
fill(c7);
rect(mouseX+215,mouseY+250,70,5);
fill(c7);
rect(mouseX+160,mouseY+250,40,5);
fill(c7);
rect(mouseX+300,mouseY+250,40,5);
fill(c7);
rect(mouseX+317,mouseY+240,5,30);
fill(c7);
rect(mouseX+177,mouseY+240,5,30);
}
I guess you have this inside the keyPressed section.
So it gets executed only briefly. You see the rect only short time.
Instead have these lines outside the if-clause.
Only c20=color(108,52,131); //PURPLE
belongs into the if-clause;
Attention: color c20;
must be placed before setup, so c20 is known outside the if-clause
AND: Welcome to the forum! Good to have your here!
Chrisir
Thank you so much for replying. I just started using processing last week so I’m pretty new to it. The purpose of this assignment was to create an object (I did a house) and make it change colors by pressing different keys and also have a while loop somewhere in it. I changed what you said so my code is now what I pasted below, but it’s still not working as intended. Now just a white rectangle appears when I run the code. I don’t know what else to do. If you can help further in any way that’d be great but if not thank you anyway!
void setup()
{
frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
if (keyPressed)
{
if (key == CODED)
{
if (keyCode == UP)
{
color c20=color(108,52,131); //PURPLE
fill(c20);
}
}
}
rect(mouseX+150,mouseY+200,200,180);
if (keyCode == DOWN)
{
color c24=color(35,155,86);//GREEN
fill(c24);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == LEFT)
{
color c21=color(244,208,63);//YELLOW
fill(c21);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == RIGHT)
{
color c22=color(230,126,34);//ORANGE
fill(c22);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == SHIFT)
{
color c27=color(174,214,241);//BLUE
fill(c27);
rect(mouseX+150,mouseY+200,200,180);
}
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while(i<5)
{
if (keypressed)
{
if (key == 'a')
{
color c34=color(255,51,227);
fill(c34);
rect(mouseX+150,mouseY+200,200,180);
}
i= i+1;
}
}
rect(mouseX+150,mouseY+200,200,180);
color c2=color(0,100,20);
fill(c2);
triangle(mouseX+130,mouseY+200,mouseX+370,mouseY+200,mouseX+250,mouseY+100);
color c3=color(100,10,0);
fill(c3);
rect(mouseX+234,mouseY+310,30,70);
color c4=color(60,120,200);
fill(c4);
rect(mouseX+160,mouseY+240,40,30);
color c5=color(60,120,200);
fill(c5);
rect(mouseX+300,mouseY+240,40,30);
color c6=color(60,120,200);
fill(c6);
ellipse(mouseX+250,mouseY+255,70,50);
color c7=color(249,249,249);
fill(c7);
rect(mouseX+247,mouseY+230,5,50);
fill(c7);
rect(mouseX+215,mouseY+250,70,5);
fill(c7);
rect(mouseX+160,mouseY+250,40,5);
fill(c7);
rect(mouseX+300,mouseY+250,40,5);
fill(c7);
rect(mouseX+317,mouseY+240,5,30);
fill(c7);
rect(mouseX+177,mouseY+240,5,30);
}
Okay, I demonstrated what I meant only with the color c20
Have c20 before setup() so that it is global
-
change it inside the if-clause (which is executed only briefly)
-
use it outside the if-clause so it is permanent
You need to repeat this for all colors!!!
(You had also a small error in the while loop)
color c20 = color(255);
void setup()
{
//frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
if (keyPressed)
{
if (key == CODED)
{
if (keyCode == UP)
{
c20=color(108, 52, 131); //PURPLE
}
}
}
// rect(mouseX+150, mouseY+200, 200, 180); // duplicate I think - see below
if (keyCode == DOWN)
{
color c24=color(35, 155, 86);//GREEN
fill(c24);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == LEFT)
{
color c21=color(244, 208, 63);//YELLOW
fill(c21);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == RIGHT)
{
color c22=color(230, 126, 34);//ORANGE
fill(c22);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == SHIFT)
{
color c27=color(174, 214, 241);//BLUE
fill(c27);
rect(mouseX+150, mouseY+200, 200, 180);
}
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while (i<5)
{
if (keyPressed)
{
if (key == 'a')
{
color c34=color(255, 51, 227);
fill(c34);
rect(mouseX+150, mouseY+200, 200, 180);
}
}
i= i+1;
}
fill(c20);
rect(mouseX+150, mouseY+200, 200, 180); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
color c2=color(0, 100, 20);
fill(c2);
triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);
color c3=color(100, 10, 0);
fill(c3);
rect(mouseX+234, mouseY+310, 30, 70);
color c4=color(60, 120, 200);
fill(c4);
rect(mouseX+160, mouseY+240, 40, 30);
color c5=color(60, 120, 200);
fill(c5);
rect(mouseX+300, mouseY+240, 40, 30);
color c6=color(60, 120, 200);
fill(c6);
ellipse(mouseX+250, mouseY+255, 70, 50);
color c7=color(249, 249, 249);
fill(c7);
rect(mouseX+247, mouseY+230, 5, 50);
fill(c7);
rect(mouseX+215, mouseY+250, 70, 5);
fill(c7);
rect(mouseX+160, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+300, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+317, mouseY+240, 5, 30);
fill(c7);
rect(mouseX+177, mouseY+240, 5, 30);
}
Your while loop doesn’t do anything
when you have to use one:
don’t use keyPressed inside
consider to do 5 smoke clouds coming from the chimney like
*
*
*
*
*
using a while loop
I made changes so now I have this. Is that what you meant? Its still just a white rectangle showing up when I run it.I haven’t done anything to the while loop yet.
color c20=color(108,52,131);
color c24=color(35,155,86);//GREEN
color c21=color(244,208,63);//YELLOW
color c22=color(230,126,34);//ORANGE
color c27=color(174,214,241);//BLUE
void setup()
{
frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
if (keyPressed)
{
if (key == CODED)
{
if (keyCode == UP)
{
color c20=color(108,52,131); //PURPLE
fill(c20);
}
}
}
rect(mouseX+150,mouseY+200,200,180);
if (keyCode == DOWN)
{
color c24=color(35,155,86);//GREEN
fill(c24);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == LEFT)
{
color c21=color(244,208,63);//YELLOW
fill(c21);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == RIGHT)
{
color c22=color(230,126,34);//ORANGE
fill(c22);
rect(mouseX+150,mouseY+200,200,180);
}
if (keyCode == SHIFT)
{
color c27=color(174,214,241);//BLUE
fill(c27);
rect(mouseX+150,mouseY+200,200,180);
}
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while(i<5)
{
if (keypressed)
{
if (key == 'a')
{
color c34=color(255,51,227);
fill(c34);
rect(mouseX+150,mouseY+200,200,180);
}
}
i= i+1;
}
fill(c20);
rect(mouseX+150,mouseY+200,200,180);
color c2=color(0,100,20);
fill(c2);
triangle(mouseX+130,mouseY+200,mouseX+370,mouseY+200,mouseX+250,mouseY+100);
color c3=color(100,10,0);
fill(c3);
rect(mouseX+234,mouseY+310,30,70);
color c4=color(60,120,200);
fill(c4);
rect(mouseX+160,mouseY+240,40,30);
color c5=color(60,120,200);
fill(c5);
rect(mouseX+300,mouseY+240,40,30);
color c6=color(60,120,200);
fill(c6);
ellipse(mouseX+250,mouseY+255,70,50);
color c7=color(249,249,249);
fill(c7);
rect(mouseX+247,mouseY+230,5,50);
fill(c7);
rect(mouseX+215,mouseY+250,70,5);
fill(c7);
rect(mouseX+160,mouseY+250,40,5);
fill(c7);
rect(mouseX+300,mouseY+250,40,5);
fill(c7);
rect(mouseX+317,mouseY+240,5,30);
fill(c7);
rect(mouseX+177,mouseY+240,5,30);
}
Did you try my code?
You still have if (keypressed) in the while loop
but it must be if (keyPressed) with capital P
I see a house
it doesn’t change color when you press UP - my code does
As I mentioned:
when you say color c24… before setup() ( color c24=color(35, 155, 86);//GREEN )
DO NOT say color c24=… later !!!
instead just say c24 = …
when repeating the word color, you declare a new variable which is overshadowing the first one!!!
In this version, we make use of more functions
but you still have to order the colors, c20 is done
-
and no rects should be in function keyPressed and only color changes in keyPressed
-
all color uses and rects in function house()
color c20 = color(255);
void setup()
{
//frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
house();
}
// -------------------------------------------------------------------------------------------
void house() {
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while (i<5)
{
color c34=color(255, 51, 227);
fill(c34);
rect(mouseX+150, mouseY+140-33*i, 20, 18);
i = i+1;
}
fill(c20);
rect(mouseX+150, mouseY+200, 200, 180);
color c2=color(0, 100, 20);
fill(c2);
triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);
color c3=color(100, 10, 0);
fill(c3);
rect(mouseX+234, mouseY+310, 30, 70);
color c4=color(60, 120, 200);
fill(c4);
rect(mouseX+160, mouseY+240, 40, 30);
color c5=color(60, 120, 200);
fill(c5);
rect(mouseX+300, mouseY+240, 40, 30);
color c6=color(60, 120, 200);
fill(c6);
ellipse(mouseX+250, mouseY+255, 70, 50);
color c7=color(249, 249, 249);
fill(c7);
rect(mouseX+247, mouseY+230, 5, 50);
fill(c7);
rect(mouseX+215, mouseY+250, 70, 5);
fill(c7);
rect(mouseX+160, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+300, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+317, mouseY+240, 5, 30);
fill(c7);
rect(mouseX+177, mouseY+240, 5, 30);
}
// -------------------------------------------------------------------------------------------
void keyPressed() {
if (key == CODED)
{
if (keyCode == UP)
{
c20=color(108, 52, 131); //PURPLE
}
}
// rect(mouseX+150, mouseY+200, 200, 180);
if (keyCode == DOWN)
{
color c24=color(35, 155, 86);//GREEN
fill(c24);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == LEFT)
{
color c21=color(244, 208, 63);//YELLOW
fill(c21);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == RIGHT)
{
color c22=color(230, 126, 34);//ORANGE
fill(c22);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == SHIFT)
{
color c27=color(174, 214, 241);//BLUE
fill(c27);
rect(mouseX+150, mouseY+200, 200, 180);
}
}
Okay so now I have this but its staying purple and not changing color with the press of any keys
color c20=color(108,52,131);
color c24=color(35,155,86);//GREEN
color c21=color(244,208,63);//YELLOW
color c22=color(230,126,34);//ORANGE
color c27=color(174,214,241);//BLUE
void setup()
{
//frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
house();
}
// -------------------------------------------------------------------------------------------
void house() {
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while (i<5)
{
color c34=color(255, 51, 227);
fill(c34);
rect(mouseX+150, mouseY+140-33*i, 20, 18);
i = i+1;
}
fill(c20);
rect(mouseX+150, mouseY+200, 200, 180);
color c2=color(0, 100, 20);
fill(c2);
triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);
color c3=color(100, 10, 0);
fill(c3);
rect(mouseX+234, mouseY+310, 30, 70);
color c4=color(60, 120, 200);
fill(c4);
rect(mouseX+160, mouseY+240, 40, 30);
color c5=color(60, 120, 200);
fill(c5);
rect(mouseX+300, mouseY+240, 40, 30);
color c6=color(60, 120, 200);
fill(c6);
ellipse(mouseX+250, mouseY+255, 70, 50);
color c7=color(249, 249, 249);
fill(c7);
rect(mouseX+247, mouseY+230, 5, 50);
fill(c7);
rect(mouseX+215, mouseY+250, 70, 5);
fill(c7);
rect(mouseX+160, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+300, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+317, mouseY+240, 5, 30);
fill(c7);
rect(mouseX+177, mouseY+240, 5, 30);
}
// -------------------------------------------------------------------------------------------
void keyPressed() {
if (key == CODED)
{
if (keyCode == UP)
{
c20=color(108, 52, 131); //PURPLE
}
}
// rect(mouseX+150, mouseY+200, 200, 180);
if (keyCode == DOWN)
{
color c24=color(35, 155, 86);//GREEN
fill(c24);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == LEFT)
{
color c21=color(244, 208, 63);//YELLOW
fill(c21);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == RIGHT)
{
color c22=color(230, 126, 34);//ORANGE
fill(c22);
rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == SHIFT)
{
color c27=color(174, 214, 241);//BLUE
fill(c27);
rect(mouseX+150, mouseY+200, 200, 180);
}
}
As I mentioned:
when you say color c24… before setup() ( color c24=color(35, 155, 86);//GREEN )
DO NOT say color c24=… later !!!
instead just say c24 = …
when repeating the word color , you declare a new variable which is overshadowing the first one!!!
Remark
-
NO rects should be in function keyPressed and only color changes in keyPressed
-
all color uses and rects in function house()
Like this?
color c20=color(108,52,131);
color c24=color(35,155,86);//GREEN
color c21=color(244,208,63);//YELLOW
color c22=color(230,126,34);//ORANGE
color c27=color(174,214,241);//BLUE
void setup()
{
//frameRate(10);
size(500, 500);
}
void draw()
{
background(200);
house();
}
// -------------------------------------------------------------------------------------------
void house() {
//else
//{
// color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
// }
//}
int i;
i=0;
while (i<5)
{
color c34=color(255, 51, 227);
fill(c34);
rect(mouseX+150, mouseY+140-33*i, 20, 18);
i = i+1;
}
fill(c20);
rect(mouseX+150, mouseY+200, 200, 180);
color c2=color(0, 100, 20);
fill(c2);
triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);
color c3=color(100, 10, 0);
fill(c3);
rect(mouseX+234, mouseY+310, 30, 70);
color c4=color(60, 120, 200);
fill(c4);
rect(mouseX+160, mouseY+240, 40, 30);
color c5=color(60, 120, 200);
fill(c5);
rect(mouseX+300, mouseY+240, 40, 30);
color c6=color(60, 120, 200);
fill(c6);
ellipse(mouseX+250, mouseY+255, 70, 50);
color c7=color(249, 249, 249);
fill(c7);
rect(mouseX+247, mouseY+230, 5, 50);
fill(c7);
rect(mouseX+215, mouseY+250, 70, 5);
fill(c7);
rect(mouseX+160, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+300, mouseY+250, 40, 5);
fill(c7);
rect(mouseX+317, mouseY+240, 5, 30);
fill(c7);
rect(mouseX+177, mouseY+240, 5, 30);
}
// -------------------------------------------------------------------------------------------
void keyPressed() {
if (key == CODED)
{
if (keyCode == UP)
{
c20=color(108, 52, 131); //PURPLE
}
}
// rect(mouseX+150, mouseY+200, 200, 180);
if (keyCode == DOWN)
{
c24=color(35, 155, 86);//GREEN
fill(c24);
//rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == LEFT)
{
c21=color(244, 208, 63);//YELLOW
fill(c21);
//rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == RIGHT)
{
c22=color(230, 126, 34);//ORANGE
fill(c22);
//rect(mouseX+150, mouseY+200, 200, 180);
}
if (keyCode == SHIFT)
{
c27=color(174, 214, 241);//BLUE
fill(c27);
//rect(mouseX+150, mouseY+200, 200, 180);
}
}
Why do you ask me?
Does it work?
Does it do what you expect?
Puuh…
so, when you want the keys to CHANGE the color, don’t give the colors initially the SAME values they get when you press the key… (then you won’t see a change)
say color c20=color(255);
before setup - WHITE - so it gets purple when you hit UP
c27 : color c27=color(174);//gray
before setup()
REMARK
BUT c27 NEVER gets used… try to use it somwhere…
same for the other colors
Chrisir
Ok Im sorry, I am asking you because I am very new at this and I don’t understand half of what you’re saying. I’m trying to do what you’re saying but nothing is changing.Thank you for your help but I just give up on this.
hang on…
I can give you a kick start here
and implement also translate (-200, -200); at start of house();
I just don’t even know what translate means
Try checking out this tutorial on 2D movements https://processing.org/tutorials/transform2d/
so the basic idea is simple:
- The function
keyPressed()
gets excuted only briefly, during the key is down. So no use drawing rects here. Or using fill. Instead we just change the colors (which are stored) to have a permanent effect. Like:c27=color(174, 214, 241);//BLUE
for example. - house() gets executed throughout (it’s called from draw()!). Good. We want to draw rects here using the colors with fill(c20) for example.
- To have the same colors in keyPressed() as in house() we need to declare them globally. That’s before setup(). They are known in all functions. But declare them with another color than the color we are going to change them to in keyPressed() so we see a difference.
Think of the screen as a grid. Instead of moving objects across the grid, you can just move the grid itself! That is basically what translate does. It can be very useful in many situations.