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() {
int k=' ';
if (keyCode == RIGHT) {
x++;
} else if (keyCode == LEFT) {
x--;
} else if (keyCode == UP){
y--;
} else if (keyCode == DOWN){
y++;
}else if(key=' '){
a=a*2;
b=b*2;
}
}
您能教我如何通过将矩形的大小加倍1次,2次,1次来按切换的空格键吗?
答案 0 :(得分:0)
在这里,我给x和y加了+5,因为一个像素太小了
float x = 400, y=100;
int a=20,b=10;
void setup()
{
size(800, 200);
}
void draw()
{
background(200); // to erase the screen everyframe
rect(x-a/2, y-b/2, a, b);
}
void keyPressed()
{
if (keyCode == RIGHT) {
x+=5;
}
else if (keyCode == LEFT) {
x-=5;
}
else if (keyCode == UP){
y-=5;
}
else if (keyCode == DOWN){
y+=5;
}
else if(key==' '){ //you need to put double equal for checking instead of just one
a*=2; // same as a=a*2;
b*=2; // same as b=b*2;
}
}