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++;
}
}
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;
}
}
© 2024 OneMinuteCode. All rights reserved.