今回はブロックを表示させましょう。
Blockというクラスを作ります。Blockクラスのnは、後で使うのですが、デフォルトで 1 、ブロックがなくなれば 0 となるようにします。
class Block{
int n, x, y;
Block(){
n = 1;
}
}
ブロックは複数個×複数個で並んでいます。これを表現するために、このBlockクラスを2次元配列にします。
はじめにBlock型の2次元配列を宣言し、setup()内でBlockクラスのインスタンスとしてblockを生成し、各要素のX座標(block[i][j].x)、Y座標(block[i][j].y)に値を代入します。ブロックの有無を示す block[i][j].n はデフォルトで 1としています。draw() 内で、もし n が 1 なら、その座標にブロックを表示させます。
Block[][] block;
<setup()内>
block = new Block[10][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;
}
}
<draw()内>
for (int i = 0; i < 10; i ++){
for (int j = 0; j < 10; j ++){
if(block[i][j].n == 1) {
fill(255);
rect(block[i][j].x, block[i][j].y, bw, bh);
}
}
}
これでブロックの表示はできました。ですが現状では、ボールとラケット、上左右の壁の衝突判定はできていますが、ブロックとボールの衝突判定を記述していないので、ボールはブロックにぶつからず、すり抜けていきます。

今日のコードです。
Ball ball;
Block[][] block;
Player player;
int bw, bh;
class Ball{
float x, y, r, hr, xDir, yDir;
Ball(){
x = width/2;
y = height/3 * 2;
r = 10;
xDir = random(-1, 1);
yDir = random(2, 4);
hr = r/2;
}
void move(){
if ((x+hr > width) || (x-hr < 0))
xDir *= -1;
if ((y-hr<=0) || ((y+hr >= player.y) && (y+hr <= player.y + 5) && (x >= player.x) && (x <= player.x+50)))
yDir *= -1;
if (y > height) {
player.score --;
xDir = random(-1, 1);
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;
Block(){
n = 1;
}
void display(){
if (n == 1){
fill(255);
rect(x, y, bw, bh);
}
}
}
class Player{
float x, y, easing, targetX;
int score;
Player(){
x = width/2-13;
y = 470;
easing = 0.1;
score = 10;
}
void move(){
targetX = mouseX-25;
x += (targetX - x) * easing;
rect(x, y, 50, 10);
}
}
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;
}
}
}
void draw(){
background(0);
ball.move();
player.move();
for (int i = 0; i < 10; i ++){
for (int j = 0; j < 10; j ++)
block[i][j].display();
}
}