ライフやスコアなどの情報を表示させます。
プレイヤーの属性として以下を設定します。
ライフ: デフォルト値10。0になったら終了
スコア:ブロックを1つ消したら+10点。連続で壊した場合、2個目で+1点、3個目で+2点・・・というふうに加算されるようにする。10個連続で消したら、10×10+1+2+3+4+5+6+7+8+9=145点となり、10個を個別に消した場合(10×10=100点)よりも点数が高くなる。
ヒット率:破壊したブロック数/ラケットで打った回数。%で表示する。
Playerクラスにscore, life, hit, hr(hit rateの略), bc(block countの略), cnt(countの略)を追加します。
if (hit == 0) hr = 0; else hr = (bc/hit)*100; という個所は、ヒット率の計算に使っています。最初のif文は、hitが0の時はbc/hitという計算ができないため、それを回避するために記述しています。
<Playerクラス>
class Player{
float x, y, easing, targetX;
↓ 以下に修正
class Player{
float x, y, easing, targetX, hit, hr, bc;
int score, life, cnt;
Player(){
x = width/2-25;
y = 470;
easing = 0.1;
}
↓以下に修正
Player(){
x = width/2-25;
y = 470;
easing = 0.1;
hit = 0;
life = 10;
score = 0;
bc = 0;
hr = 0;
}
// move()メソッド 以下を追加
if (hit == 0) hr = 0;
else hr = (bc/hit)*100;
ブロックが消えた時にスコアが10点追加され、消したブロックをカウントするためのbcもその都度1増加するようにします。
連続でブロックを消したときのスコアのために新しい変数cntを用意しました。ブロックを消すたびに1が加算されていき、その都度スコアに加算されます。一度ラケットに触れるとcntはリセットされます。cntはデフォルト値-1に設定していますが、こうすることで、ブロックを1つだけ消した時には、ブロックを消した時にcntが+1されて0になり、加算しても連続のボーナスが入らないようにしています。
<Playerクラス>
class Player{
float x, y, easing, targetX;
↓ 以下に修正
class Player{
float x, y, easing, targetX, hit, hr, bc;
int score, life, cnt;
Player(){
x = width/2-25;
y = 470;
easing = 0.1;
}
↓以下に修正
Player(){
x = width/2-25;
y = 470;
easing = 0.1;
hit = 0;
life = 10;
score = 0;
bc = 0;
hr = 0;
}
以下はスコア計算の処理です。
<Blockクラス>
// display()メソッド内
if ( ~ 省略 ~){
ball.yDir *= -1;
n = 0;
}
if ( ~ 省略 ~){
ball.xDir *= -1;
n = 0;
}
↓ 以下に修正
if ( ~ 省略 ~){
ball.yDir *= -1;
n = 0;
player.score += 10;
player.bc++;
player.cnt += 1;
player.score += player.cnt;
}
if ( ~ 省略 ~){
ball.xDir *= -1;
n = 0;
player.score += 10;
player.bc++;
player.cnt += 1;
player.score += player.cnt;
}
点数などを表示させるため、main()内に以下を追記します。
fill(255);
text("life:" + player.life, 20, 20);
text("score:" + player.score, 80, 20);
text("hit rate:" + player.hr + "%", 140, 20);
これで、ライフ、スコア、ヒット率が左上に表示されるようになりました。ゲームオーバーの処理などはまだ記述していません。

今日のコードです。
Ball ball;
Block[][] block;
Player player;
int bw, bh;
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 = 1;
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;
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;
n = 0;
}
}
}
}
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[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[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[i][j].display();
}
fill(255);
text("life:" + player.life, 20, 20);
text("score:" + player.score, 60, 20);
text("hit rate:" + player.hr + "%", 140, 20);
}