# Problem while creating a brick breaker game

Hi,
Can somebody help me display the bricks correctly. I can´t get it to work.
I think the problem is the array and how I determine the position of the bricks.
Thank you!

Here`s the main Program:

``````SpielerFlaeche player;
SpielBall ball;
int brickWidth;
int brickHeight;
int REIHEN = 8;
int SPEED = 6;
Brick[] bricks = new Brick[(width/(brickWidth+10))*REIHEN];

void setup() {
size(1000, 1000);
brickWidth = width/10;
brickHeight = height/25;

player = new SpielerFlaeche(width/2, height-height/8);
ball = new SpielBall(width/2, height/2);
for (int i = 0; i < bricks.length; i++) {
bricks[i] = new Brick();
}
}
void draw() {
background(0);
stroke(255);
strokeWeight(2);

for (int j = 10; j < (brickHeight+10)*REIHEN; j += (brickHeight+10)) {
for (int i = 10; i < width; i += (brickWidth+10)) {
int arrayPlace = (i/(brickWidth+10))+(j/(brickHeight+10));
bricks[arrayPlace].pos.set(i, j);
}
}
for (Brick b : bricks) {
ball.bounceOffBricks(b.pos);
}
b.show();
}

ball.update();
ball.bounceOffPlayer();
ball.show();
player.update();
player.show();

}
``````

The brick class:

``````class Brick {
PVector pos;

Brick() {
pos = new PVector(0, 10);
}
void show() {
rect(pos.x, pos.y, brickWidth, brickHeight);
}
}
}
``````

The ball class:

``````class SpielBall {
PVector pos;
PVector dir;

SpielBall(float x, float y) {
pos = new PVector(x, y);
dir = new PVector();
dir = PVector.random2D();
dir.mult(SPEED);
}

void update() {
if (pos.x - radius < 0 || pos.x + radius > width) {
dir.x *= -1;
}
if (pos.y - radius < 0 || pos.y + radius > height) {
dir.y *= -1;
}
}

void bounceOffPlayer() {
if (pos.x - radius < player.pos.x + width/20 && pos.x + radius > player.pos.x - width/20 && pos.y + radius < player.pos.y + height/200 && pos.y + radius > player.pos.y - height/200) {
float dirNumber = player.pos.x - pos.x;
println(dirNumber);
dir.y *= -1;
dir.x = map(dirNumber, -60, 60, 7, -7);
dir.normalize();
dir.mult(SPEED);
}
}

void bounceOffBricks(PVector brickPos) {
PVector linksOben = new PVector(brickPos.x, brickPos.y);
PVector linksUnten = new PVector(brickPos.x, brickPos.y + brickHeight);
PVector rechtsOben = new PVector(brickPos.x + brickWidth, brickPos.y);
PVector rechtsUnten = new PVector(brickPos.x + brickWidth, brickPos.y + brickHeight);

dir.y *= -1;
for (Brick g : bricks) {
if (g.pos == brickPos) {
}
}
}
}

void show() {
}
}
``````

And the playerPanel class:

``````class SpielerFlaeche {
PVector pos;

SpielerFlaeche(float x, float y) {
pos = new PVector(x, y);
}

void update() {
//pos.x = ball.pos.x;
pos.x = mouseX;
//if (pos.x > ball.pos.x) {
//  pos.x -= 100;
//} else if (pos.x < ball.pos.x) {
//  pos.x += 100;
//}
}

void show() {
rect(pos.x-width/20, pos.y-height/200, width/10, height/100);
}
}
``````
1 Like

Instead of the calculation you did for arrayPlace, try using j *10+i and use j and i as x and y coorinates for the bricks. This will set them from the top left, to the top right and then Start the Next row underneath, while continuing the number linearly.

Like :

``````for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
bricks[i*rows+j].pos.set(i*width + offset, j*height + offset)
}
}
``````

And this should only need to be set once in setup…

2 Likes

Thank you that worked.

like that

``````bricks[i*rows+j].pos.set(i*brickWidth + offset*i, j*brickHeight + offset*j)
``````
1 Like