switch with random numbers
in
Programming Questions
•
2 years ago
Hi,
I hope someone can anser my question, cos I need this for a project I have to hand in on Friday and I don't have the slightest idea what I'm doing.
What I want is that the rectangles "glow" when a key is pressed and that they do this randomly, but if one rectangle is glowing, the others don't. So I've done this with a switch and an array to randomly access the states.
This is my code:
char State;
int rectWidth = 100;
int rectHeight = 100;
int glow = 5; //setting strokeWeight when rectangle is the stimulus (thus glowing)
int noGlow = 2; //strokeWeight when rectangle is not the stimulus
/*
States:
U = upRight
D = downRight
L = upLeft
M = downLeft
*/
void setup(){
size(700,700);
background(0);
State = 'U';
}
//four rectangles which can glow or not glow
void Rect(boolean upRight, boolean downRight, boolean upLeft, boolean downLeft){
stroke(0,255,0);
noFill();
rectMode (CENTER);
if(upRight){ //if the upRight rectangle is the stimulus, it is glowing, otherwise it's not
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(550,150,rectWidth,rectHeight); //rectangle in the up right corner
if(downRight){ //if it is the stimulus, it's glowing
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(550,550,rectWidth,rectHeight); //rectangle in the down right corner
if(upLeft){ //same here, if it's the stimulus, it's glowing
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(150,150,rectWidth,rectHeight); //rectangle in the up left corner
if(downLeft){ //and again, if stimulus --> glowing
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(150,550,rectWidth,rectHeight); //rectangle in the down left corner
}
//array for the states, so that they can be accessed randomly
char [] states = {'U','D','L','M'};
//cycle through the glowing rectangles randomly on keyPress
void keyPressed(){
switch(State){
case 'U':
State = states[int(random(0,4))];
println("upRight to downRight");
break;
case 'D':
State = states[int(random(0,4))];
println("downRight to upLeft");
break;
case 'L':
State = states[int(random(0,4))];
println("upLeft to downLeft");
break;
case 'M':
State = states[int(random(0,4))];
println("downLeft to upRight");
break;
}
}
//assign to the states which argument is true
void draw(){
if(State == 'U'){
Rect(true,false,false,false);
}
if(State == 'D'){
Rect(false,true,false,false);
}
if(State == 'L'){
Rect(false,false,true,false);
}
if(State == 'M'){
Rect(false,false,false,true);
}
}
But somehow, the rectangles don't stop to glow, so I guess something is wrong with my "else"condition.
I really hope someone can help me! I'm getting kinda desperate here!
Thanks in advance!
Greets, Isabel
I hope someone can anser my question, cos I need this for a project I have to hand in on Friday and I don't have the slightest idea what I'm doing.
What I want is that the rectangles "glow" when a key is pressed and that they do this randomly, but if one rectangle is glowing, the others don't. So I've done this with a switch and an array to randomly access the states.
This is my code:
char State;
int rectWidth = 100;
int rectHeight = 100;
int glow = 5; //setting strokeWeight when rectangle is the stimulus (thus glowing)
int noGlow = 2; //strokeWeight when rectangle is not the stimulus
/*
States:
U = upRight
D = downRight
L = upLeft
M = downLeft
*/
void setup(){
size(700,700);
background(0);
State = 'U';
}
//four rectangles which can glow or not glow
void Rect(boolean upRight, boolean downRight, boolean upLeft, boolean downLeft){
stroke(0,255,0);
noFill();
rectMode (CENTER);
if(upRight){ //if the upRight rectangle is the stimulus, it is glowing, otherwise it's not
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(550,150,rectWidth,rectHeight); //rectangle in the up right corner
if(downRight){ //if it is the stimulus, it's glowing
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(550,550,rectWidth,rectHeight); //rectangle in the down right corner
if(upLeft){ //same here, if it's the stimulus, it's glowing
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(150,150,rectWidth,rectHeight); //rectangle in the up left corner
if(downLeft){ //and again, if stimulus --> glowing
strokeWeight(glow);
}
else{
strokeWeight(noGlow);
}
rect(150,550,rectWidth,rectHeight); //rectangle in the down left corner
}
//array for the states, so that they can be accessed randomly
char [] states = {'U','D','L','M'};
//cycle through the glowing rectangles randomly on keyPress
void keyPressed(){
switch(State){
case 'U':
State = states[int(random(0,4))];
println("upRight to downRight");
break;
case 'D':
State = states[int(random(0,4))];
println("downRight to upLeft");
break;
case 'L':
State = states[int(random(0,4))];
println("upLeft to downLeft");
break;
case 'M':
State = states[int(random(0,4))];
println("downLeft to upRight");
break;
}
}
//assign to the states which argument is true
void draw(){
if(State == 'U'){
Rect(true,false,false,false);
}
if(State == 'D'){
Rect(false,true,false,false);
}
if(State == 'L'){
Rect(false,false,true,false);
}
if(State == 'M'){
Rect(false,false,false,true);
}
}
But somehow, the rectangles don't stop to glow, so I guess something is wrong with my "else"condition.
I really hope someone can help me! I'm getting kinda desperate here!
Thanks in advance!
Greets, Isabel
1