如何按空格键切换矩形的两倍大小?

时间:2019-01-22 14:13:58

标签: java processing

        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次来按切换的空格键吗?

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