我将如何堆叠积木?

如何使用空格键冻结当前移动的堆栈,并使新堆栈显示在冻结的堆栈的右侧?当新堆栈以与第一个堆栈相同的方式上下移动时,冻结的堆栈应保持在原位。这种情况一直持续到没有更多的筹码出现空间并且游戏重置为只有4个筹码的情况时,就像开始游戏一样。

class Block {
      float locX;
      float locY;
      float sizeW;
      float sizeH;
      float dir;
      color col;

      Block(float x, float y, float w, float h, float d, color c) {
        locX = x;
        locY = y;
        sizeW = w;
        sizeH = h;
        dir = d;
        col = c;
      }

      void Draw() {
        fill(col);
        int steppedPosX = (int)(locX/sizeW+0.5);
        int steppedPosY = (int)(locY/sizeH+0.5);
        rect(steppedPosX*sizeW, steppedPosY*sizeH, sizeW, sizeH);
      }

      void Move() {
        locY = locY + dir;
        if (locY + sizeH > height || locY < 0) {
          dir = dir * -1;
          locY = locY + dir;
        }
      }
    }

    boolean moving;
    Block blocks[];

    void setup() {
      size (1280, 720);

      blocks = new Block[4];
      for (int i = 0; i < blocks.length; i = 90 ) {
        blocks[i] = new Block(0, i*90, 90, 90, height/720, color(255, 147, 79));
      }
      moving = true;
    }

    void draw() {

      background(255);

      stroke(0);
      strokeWeight(1);

      for (int i = 0; i < width; i++) {
        line(0, i, 1280, i);
        line(i, 0, i, 720);
      }

      for (int i = 0; i < blocks.length; i++ ) {
        blocks[i].Draw();
      }

      if (moving) {
        for (int i = 0; i < blocks.length; i++ ) {
          blocks[i].Move();
        }
      }
    }

    void keyPressed() {
      if (key == ' ') {
        moving = !moving;
      }
    }
评论