ブロック崩し(9) 特別なブロックを加える

ランダムに得点の高いブロックを出現させます。

通常のブロックを消すと+10点ですが、10個に1個赤いブロックを出現させ、これを消すと+100点となるようにします。
Blockクラスを修正します。

class Block{
  int n, x, y, bc, r; // ランダムの値を入力する "r" を追加
  Block(){
    n = 0;
    bc = 0;
    r = int(random(10)); // 0から9までの値をランダムに入力
  }
  void display(){
    if (n != 0){
      if (r == 1)  fill(255, 0, 0); // r が 1 なら(つまり10%の確率で)赤にする
      else  fill(255); // それ以外はそのまま(白)
      rect(x, y, bw, bh);
      if (~省略~){
          ~省略~
          player.score += 10;
          if(r == 1)  player.score += 90; // r が 1 の場合はさらに90点を追加(1つ前の行と加えて計100点)
          ~省略~
      }
      if (~省略~){
          ~省略~
          player.score += 10;
          if(r == 1)  player.score += 90; // r が 1 の場合はさらに90点を追加(1つ前の行と加えて計100点)
          ~省略~
      }

すると、以下のように赤いブロックが表れ、消すと100点が追加されます。

今日のコードです。

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, r;
  Block(){
    n = 0;
    bc = 0;
    r = int(random(10));
  }
  void display(){
    if (n != 0){
      if (r == 1)  fill(255, 0, 0);
      else  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;
          if(r == 1)  player.score += 90;
          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;
          if(r == 1)  player.score += 90;
          player.bc++;
          player.cnt += 1;
          player.score += player.cnt;
          bN[stage]--;
          n = 0;
      }
    }
    if(bN[stage] == 0)  if(stage == 2)  endMessage();
    else  stage++;
    if(player.life == 0) gameOver();
  }
}

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();
  for (int i = 0; i < 10; i ++){
    for (int j = 0; j < 10; j ++)
      block[stage][i][j].display();
  }
  
  fill(255);
  textSize(15);
  text("life:" + player.life, 20, 20);
  text("score:" + player.score, 100, 20);
  text("hit rate:" + player.hr + "%", 220, 20);
}

void endMessage(){
  textAlign(CENTER, CENTER);
  textSize(80);
  text("CLEAR!", width/2, height/2-50);
  textSize(25);
  text("score:" + player.score, width/2, height/2+20);
  text("life:" + player.life, width/2, height/2+50);
  text("hit rate:" + ball.hr, width/2, height/2+80);
  noLoop();
}

void gameOver(){
  textAlign(CENTER, CENTER);
  textSize(80);
  text("GAME OVER!", width/2, height/2+50);
  textSize(25);
  text("score:" + player.score, width/2, height/2+120);
  text("life:" + player.life, width/2, height/2+150);
  text("hit rate:" + ball.hr, width/2, height/2+180);
  noLoop();
}

Leave a Reply

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