Home>

Creating a block break
I am a beginner who just started learning processing.
I am currently trying to make a block break using Processing.
I made a ball and a reflection board, but I don't know how to use the array and for, so I can't make the block well.

Error message

When I hit the blocks side by side, I want to erase the hit blocks while reflecting the ball.

Applicable source code
float bx = 200;
float by = 370;
float bvx = random (1,3);
float bvy = random (-3, -2);
int i, j, blx, bly;
int [] ble = new int [36];
void setup () {
  size (400,400);
  rectMode (CENTER);
  for (int i = 0;i<36;i ++) {
    ble [i] = 1;
  }
}
void draw () {
  background (0);
  Player ();
  Ball ();
  block ();
  }
void block () {
  for (int i = 0;i<36;i ++) {
    if (ble [i] == 1) {
     blx = (i% 6) * 60 + 50;
     bly = (i/6) * 20 + 40;
      if (! ((bx + 5>blx-20)&&(blx + 20<bx-5)&&(bly-10<by + 5)&&(bly + 10>by-5)))
       {return;}
       if ((blx-20<bx-5)&&(blx + 20>bx + 5)) {bvy = -bvy;return;}
       if ((bly-10<by-5)&&(bly + 10>by + 5)) {bvx = -bvx;return;}
       bvx = -bvx;
       bvy = -bvy;
    }
     if (ble [i] == 1) {// 0: invisible 1: visible
       rect (blx, bly, 40,20);
    }
  }
}

void Player () {
rect (mouseX, 380,30,10);
if (mouseX>385) {mouseX = 385;}
if (mouseX<15) {mouseX = 15;}
}
void Ball () {
  ellipse (bx, by, 10,10);
bx = bx + bvx;
by = by + bvy;
if (bx<5 || 395<bx) {bvx = -bvx;}
if (by<5 || by == 370&mouseX-1115<bx&bx<mouseX + 1115) {bvy = -bvy;}
}
  • Answer # 1

    Because it will not remain debugged if it is not drawn, it seems wasteful, but let's draw with a dedicated for first.

    void block () {
      // block drawing
      for (int i = 0;i<36;i ++) {
        if (ble [i] == 1) {
          blx = (i% 6) * 60 + 50;
          bly = (i/6) * 20 + 40;
          rect (blx, bly, 40, 20);
        }
      }
      // hit judgment
      for (int i = 0;i<36;i ++) {
        if (ble [i] == 1) {
        // hit here
      }
    }


    If there isreturn;in the function, the function is exited there. It is the same in for.

    There arebreak;andcontinue;as commands that can be used in for.
    break;is similar toreturn;, but assumes that for has been terminated and jumps after for.
    continue;goes to the next lap without going to the next line.
    (I ’m sorry for the poor explanation. Please check for details ^^;)