I’ve been trying for a long time to make a successful brick breaker game, but I’m still stuck on how to make the ball and bricks to interact. Can someone help me figure out a way for one brick to disappear when the ball touches it? I am mainly having trouble with the “bounceBrick” function in the brickBreaker class. I also think it has to do with the brick class, but I am not sure. Here’s the code:
int rows = 6; //Number of bricks per row
int columns = 6; //Number of columns
int total = rows * columns; //Total number of bricks
brickBreaker brickBreaker;
ball ball;
paddle paddle;
brick[] bricks = new brick[total];
void setup() {
size(800, 650);
brickBreaker = new brickBreaker();
ball = new ball();
paddle = new paddle();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
bricks[i*rows + j] = new brick((i+1) *width/(rows + 1), (j+1) * 75); //places all the bricks into the array, properly labelled.
}
}
}
void draw() {
background(51);
for (int i = 0; i < total; i++) {
bricks[i].show();
}
ball.show();
paddle.update();
brickBreaker.bounce();
brickBreaker.reset();
brickBreaker.showText();
brickBreaker.bounceBrick();
}
void mousePressed() {
brickBreaker.gameStart();
}
BrickBreaker Class
class brickBreaker {
boolean gameStart;
int score;
int gameScore;
boolean hit;
int lives;
boolean override;
int rows = 6; //Number of bricks per row
int columns = 6; //Number of columns
int total = rows * columns; //Total number of bricks
brickBreaker() {
gameStart = false;
score = 0;
gameScore = 0;
hit = false;
lives = 5;
override = false;
}
void gameStart() {
if (gameStart == false) {
gameStart = !gameStart;
}
}
void bounce() {
if (gameStart) {
ball.update();
// If ball hits paddle, switch direction
if (ball.x > mouseX - paddle.psize/2 && ball.x < mouseX + paddle.psize/2 && ball.y == paddle.y) {
// Bounce
ball.vy = ball.vy * -1;
// Decrease paddle size
paddle.psize -= 5;
}
}
}
void reset() {
if (ball.y > height) {
ball.x = 400;
ball.y = 560;
ball.vx = 4;
ball.vy = 4;
lives -= 1;
gameStart = false;
}
if (lives <= 0) {
gameOver();
}
if (score == total) {
win();
}
}
void resetGame() {
ball.x = 400;
ball.y = 560;
gameStart = false;
paddle.psize = 150;
paddle.y = 600;
ball.vx = 4;
ball.vy = 4;
score = 0;
gameScore = 0;
lives = 5;
//Setup array of all bricks on screen
for (int i = 0; i < rows; i++)
{
for (int j = 0; j< columns; j++)
{
bricks[i*rows + j] = new brick((i+1) *width/(rows + 1), (j+1) * 80);
}
}
}
// Where Score and Lives is located
void showText() {
if (override == false) {
textSize(50);
fill(255);
stroke(0);
text(gameScore, 20, 50);
textSize(25);
text("Lives:", 20, 575);
text(lives, 90, 575);
}
}
void hit() {
if (hit == true) {
}
}
void gameOver() {
override = true;
if (override == true) {
ball.x = -100;
ball.vx = 0;
ball.vy = 0;
paddle.y = -100;
textSize(50);
fill(255, 0, 0);
text("GAME OVER!!!", 250, 200);
text("Score: ", 300, 300);
text(gameScore, 450, 300);
text("Click mouse to play again!", 100, 400);
if (mousePressed) {
override = false;
resetGame();
}
}
}
void win() {
override = true;
if (override == true) {
ball.x = -10;
ball.vx = 0;
ball.vy = 0;
paddle.y = -50;
textSize(50);
fill(random(255), random(255), random(255));
text("YOU WON!!!", 250, 200);
text("Score: ", 300, 300);
text(gameScore, 450, 300);
text("Click mouse to play again!", 100, 400);
if (mousePressed) {
override = false;
resetGame();
}
}
}
void bounceBrick() {
for (int i = 0; i < total; i ++)
{
//If ball hits bottom of brick, ball moves down, increment score
if (ball.y <= bricks[i].y + bricks[i].h/2 && ball.y > bricks[i].y && ball.x >= bricks[i].x - bricks[i].w/2 && ball.x <= bricks[i].x + bricks[i].w/2)
{
ball.vy *= -1;
gameScore += 5;
bricks[i].hit();
hit = true;
}
//If ball hits top of brick ball moves up, increment score
if (ball.y >= bricks[i].y && ball.y <= bricks[i].y + bricks[i].h/2 && ball.x >= bricks[i].x && ball.x <= bricks[i].x + bricks[i].w)
{
ball.vy *= -1;
gameScore += 5;
bricks[i].hit();
hit = true;
}
//if ball hits the left of the brick, ball switches to the right, and moves in same direction, increment score
if (ball.x >= bricks[i].x && ball.x <= bricks[i].x + bricks[i].w/2 && ball.y >= bricks[i].y && ball.y <= bricks[i].y + bricks[i].h)
{
ball.vx *= -1;
gameScore += 5;
bricks[i].hit();
hit = true;
}
//if ball hits the right of the brick, ball switches to the left, and moves in same direction, increment score
if (ball.x <= bricks[i].x + bricks[i].w && ball.x >= bricks[i].x + bricks[i].w/2 && ball.y >= bricks[i].y && ball.y <= bricks[i].y + bricks[i].h)
{
ball.vx *= -1;
gameScore += 5;
bricks[i].hit();
hit = true;
}
}
}
}
Ball Class
class ball {
float dia;
float x;
float y;
float vx;
float vy;
color col;
ball() {
dia = 24;
x = 400;
y = 560;
vx = 4;
vy = 4;
col = color(random(128, 255));
}
void show() {
fill(col);
ellipseMode(CENTER);
ellipse(x, y, dia, dia);
}
void update () {
x = x + vx;
y = y + vy;
if (x > width || x < 0) {
vx = vx * -1;
} else if (y > height || y < 0) {
vy = vy * -1;
}
}
}
Paddle Class
class paddle {
int psize;
color pcol;
float y;
paddle () {
psize = 150;
pcol = color(random(128, 255));
y = 600;
}
void update() {
fill(pcol);
rectMode(CENTER);
rect(mouseX, y, psize, 10, 30);
}
}
Brick Class
class brick {
color bcol;
float x;
float y;
float h;
float w;
brick(float tempX, float tempY) {
bcol = color(random(128, 255), random(128, 255), random(128, 255));
x = tempX;
y = tempY;
w = 60;
h = 37.5;
}
void show() {
noStroke();
fill(bcol);
rect(x, y, w, h);
}
void hit() {
if (brickBreaker.hit == true) {
for (int i = 0; i < total; i++) {
bcol = color(51);
rect(x, y, w, h);
brickBreaker.hit = false;
}
}
}
}