ブロック崩し(6) ステージを増やす

ステージを増やしましょう。

そのために、これまで2次元配列で作っていたブロックを3時限配列に書き換えます。2次元配列を表す[][]という個所を、[][][]に書き換えます。また、ステージを表す変数stage、各ステージのブロック数を記録する配列 bN[] を追加します。

Block[][] block;
Player player;
int bw, bh;
↓ 修正
Block[][][] block;
Player player;
int bw, bh, stage;
int[] bN = {50, 50, 100};

これにあわあせて、Blockクラス、setup()内のブロックの初期化の処理も修正します。ブロックが消える都度、ブロック数 bN[stage] を減らします。bN[stage] が 0 になったら stage を 1 増やし、次のステージに進みます。

<Blockクラス>
n = 1;
↓ 修正
n = 0;

if (~省略~){
  ball.yDir *= -1;
  player.score += 10;
  player.bc++;
  player.cnt += 1;
  player.score += player.cnt;
  n = 0;
}
↓ 修正 (bN[stage]--; を追加)
if (~省略~){
  ball.yDir *= -1;
  player.score += 10;
  player.bc++;
  player.cnt += 1;
  player.score += player.cnt;
  n = 0;
  bN[stage]--;
}

// display()メソッドの中に以下を追記
if(bN[stage] == 0)  stage++;

<setup()内>
block = new Block[10][10];
bw = 40;
bh = 10;
for (int i = 0; i < 10; i++){
  for(int j = 0; j < 10; j++){
    block[i][j] = new Block();
    block[i][j].x = i + 50 + i * 50;
    block[i][j].y = j + 45 + j * 20;
  }
}
↓ 以下に修正
block = new Block[3][10][10];
bw = 40;
bh = 10;
stage = 0;
for (int k = 0; k < 3; k++){
  for (int i = 0; i < 10; i++){
    for(int j = 0; j < 10; j++){
      block[k][i][j] = new Block();
      block[k][i][j].x = i + 50 + i * 50;
      block[k][i][j].y = j + 45 + j * 20;
      if(k == 0)  if((i+j)%2 == 0)  block[k][i][j].n = 1; // stage 1
      if(k == 1)  if(i%2 == 0)  block[k][i][j].n = 1; // stage 2
      if(k == 2)  block[k][i][j].n = 1; // stage 3
    }
  }
}

すると、1つのステージが終わると次のステージに進みます。現状で3つのステージを作り、それぞれでブロックの並び方が変わるようにしています。この並び方の処理は、上記のコードの最後の方のif~の個所です。最後の終わりの処理はしていないのですべてのブロックを消すと強制的にプログラムが止まります。

今日のコードです。

Ball ball;
Block[][][] block;
Player player;
int bw, bh, stage;
int[] bN = {50, 50, 100};

class Ball{
  float x, y, r, hr, xDir, yDir, dis;
  Ball(){
    x = width/2;
    y = height/3 * 2;
    r = 10;
    xDir = 0;
    yDir = random(2, 4);
    hr = r/2;
  }
  void move(){
    if ((x+hr > width) || (x-hr < 0)) 
      xDir *= -1;
    if (y-hr<=0) yDir *= -1; 
    if ((y+hr >= player.y) && (y+hr <= player.y + 5) && (x >= player.x) && (x <= player.x+50)){ 
      yDir *= -1; 
      dis = ball.x - (player.x+25); 
      xDir = dis/6; 
      player.hit++; 
      player.cnt = -1; 
    } 
    if (y > height) {
      player.life --;
      xDir = 0;
      yDir = random(2, 4);
      x = width/2;
      y = height/3 * 2;
    }
    
    x += xDir; 
    y += yDir;
    fill(255);
    ellipse(x, y, r, r);
  }   
}

class Block{
  int n, x, y, bc;
  Block(){
    n = 0;
    bc = 0;
  }
  void display(){
    if (n == 1){
      fill(255);
      rect(x, y, bw, bh);
      if (((x <= ball.x) && (ball.x <= x+bw) && (ball.y+ball.hr >= y) && (ball.y+ball.hr <= y+5)) || // top
          ((x <= ball.x) && (ball.x <= x+bw) && (ball.y-ball.hr >= y+5) && (ball.y-ball.hr <= y+bh))){ //down
          ball.yDir *= -1;
          player.score += 10;
          player.bc++;
          player.cnt += 1;
          player.score += player.cnt;
          bN[stage]--;
          n = 0;
      }
      if (((ball.x+ball.hr <= x+5) && (x <= ball.x+ball.hr) && (ball.y >= y) && (ball.y <= y + bh)) || // left
          ((ball.x+ball.hr <= x+bw) && (x+bw-5 <= ball.x-ball.hr) && (ball.y >= y) && (ball.y <= y + bh))){ //right
          ball.xDir *= -1;
          player.score += 10;
          player.bc++;
          player.cnt += 1;
          player.score += player.cnt;
          bN[stage]--;
          n = 0;
      }
    }
    if(bN[stage] == 0)  stage++;
  }
}

class Player{
  float x, y, easing, targetX, hr, bc, hit;
  int score, life, cnt;

  Player(){
    x = width/2-13;
    y = 470;
    easing = 0.1;
    life = 10;
    score = 0;
    hit = 0;
    hr = 0;
    bc = 0;
    cnt = -1;
  }
  void move(){
    targetX = mouseX-25; 
    x += (targetX - x) * easing; 
    rect(x, y, 50, 10);
    if (hit == 0) hr = 0;
    else hr = floor((bc/hit)*100);
  }
}

void setup(){
  size(600, 500);
  ball = new Ball();
  player = new Player();
  block = new Block[3][10][10];
  bw = 40;
  bh = 10;
  stage = 0;
  for (int k = 0; k < 3; k++){
    for (int i = 0; i < 10; i++){
      for(int j = 0; j < 10; j++){
        block[k][i][j] = new Block();
        block[k][i][j].x = i + 50 + i * 50;
        block[k][i][j].y = j + 45 + j * 20;
        if(k == 0)  if((i+j)%2 == 0)  block[k][i][j].n = 1;
        if(k == 1)  if(i%2 == 0)  block[k][i][j].n = 1;
        if(k == 2)  block[k][i][j].n = 1;
      }
    }
  }
}

void draw(){
  background(0);
  ball.move();
  player.move();
  //player.x = constrain(x, 0, width-50);
  for (int i = 0; i < 10; i ++){
    for (int j = 0; j < 10; j ++)
      block[stage][i][j].display();
  }
  
  fill(255);
  text("life:" + player.life, 20, 20);
  text("score:" + player.score, 60, 20);
  text("hit rate:" + player.hr + "%", 140, 20);
}

Leave a Reply

Your email address will not be published. Required fields are marked *