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) {
if (!b.dead) {
ball.bounceOffBricks(b.pos);
}
b.show();
}
ball.update();
ball.bounceOffPlayer();
ball.show();
player.update();
player.show();
}
The brick class:
class Brick {
PVector pos;
boolean dead;
Brick() {
pos = new PVector(0, 10);
dead = false;
}
void show() {
if (!dead) {
rect(pos.x, pos.y, brickWidth, brickHeight);
}
}
}
The ball class:
class SpielBall {
PVector pos;
PVector dir;
int radius;
SpielBall(float x, float y) {
radius = 10;
pos = new PVector(x, y);
dir = new PVector();
dir = PVector.random2D();
dir.mult(SPEED);
}
void update() {
pos.add(dir);
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);
if (pos.y - radius < linksUnten.y && pos.y + radius > rechtsOben.y && pos.x + radius < rechtsUnten.x && pos.x - radius > linksUnten.x) {
dir.y *= -1;
for (Brick g : bricks) {
if (g.pos == brickPos) {
g.dead = true;
}
}
}
}
void show() {
ellipse(pos.x, pos.y, radius*2, radius*2);
}
}
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);
}
}