如何按 space 键切换矩形的大小加倍?

How to press space key to toggle doubling the size of the rectangle?

        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;
        }
      }


你能教我如何按 space 键,通过将矩形的大小加倍,1 次,2 次,1 次来切换吗?

给你,我给 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;
    }
  }