Every time you press the space key, do you want to toggle the size of the square to twice, once, twice, twice?

Asked 2 years ago, Updated 2 years ago, 114 views

 float x=400, y=100;
int a = 20, b = 10;
void setup() {size(800,200);}
void draw() {
rect(x-10, y-5, a, b);
}
void keyPressed() {
  intk = ';
if(keyCode==RIGHT){
x++;
} else if(keyCode==LEFT){
x--;
} else if(keyCode==UP){
  y--;
} else if (keyCode==DOWN) {
  y++;
}
}

java processing

2022-09-30 16:15

1 Answers

Each time you press a space key, you can toggle the size to manage it by rewriting the variable indicating whether it is expanding .
In the sample code below, the isExtended variable is rewritten each time you press a space to control the a and b values with the triomial operator.

 float x=400, y=100;
int a = 20, b = 10;
boolean isExtended=false;
void setup() {size(800,200);}
void draw() {
  fill(255);
  rect(0,0,800,200);
  rect(x-a/2, y-b/2, a, b);
}
void keyPressed() {
  if(keyCode==RIGHT){
    x++;
  } else if(keyCode==LEFT){
    x--;
  } else if(keyCode==UP){
    y--;
  } else if (keyCode==DOWN) {
    y++;
  } else if(keyCode==''){
    isExtended=!isExtended;
    a=isExtended? 40 : 20;
    b=isExtended? 20 : 10;
  }
}


2022-09-30 16:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.