Complete Beginner - Breakout (game) - How do I fix the brick function
in
Programming Questions
•
1 year ago
I have read other discussions on collision on the following posts:
But I still can't seem to figure it out, and I don't know why.
I would really appreciate some help with some explanation. The code is pasted below:
- // Basic Breakout game
// Code from Matthre Yee-King
int ponto = 0;
// brick position
float brickX;
float brickY; - // brick width and height
float brickH;
float brickW; - // ball position
float ballX;
float ballY; - // ball diameter
float ballD; - // ball direction
float ballDx;
float ballDy; - // bat position
float batX; - //bat width
float batW;
float batH; - //bat colour
float batB; - void setup() {
size (500, 500, P2D);
// set sizes of game items
brickW = 100;
brickH = 50;
batW = 100;
batH = 15;
ballD = 25;
batB = 255;
// random brick position
brickX = random(0, width - brickW);
brickY = random (0, height / 2);
// bat in the centre
batX = (width/2) - (batW/2);
// ball atop bat
ballX = batX + (batW/2);
ballY = height - batH - (ballD/2);
// ball movement
ballDx = random(-5, 5);
ballDy = -5;
rectMode(CORNER);
ellipseMode(CENTER);
} - void draw() {
// check for ball collision
// with top or sides of bat
checkBallAgainstBat();
// check for ball collision with
// left right and top walls
// and bounce
checkBallAgainstWalls();
// check ball against brick
checkBallAgainstBrick();
// move the ball
ballX += ballDx;
ballY += ballDy;
background(0);
// draw the bat
fill(0, 255, 0);
rect(batX, height - batH, batW, batH);
// draw the brick
fill(0, 0, batB);
batB = (batB + 10) % 255;
rect(brickX, brickY, brickW, brickH);
// draw the ball
fill(255, 0, 0);
ellipse(ballX, ballY, ballD, ballD);
if (keyCode == 37) { // left cursor key
batX -= 10;
// keep it on the screen
if (batX < 0) {
batX = 0;
}
}
if (keyCode == 39) {
batX += 10;
if (batX > (width - batW)) {
batX = width - batW;
}
}
} - // when they let go of the key, reset the keyCode
void keyReleased() {
keyCode = -1;
} - // this function checks if the ball has hit the top or sides of the bat and
// updates its direction as appropriate so the ball bouncs off the bat
// I have provided for a function here so that whenever the ball hits
// the bat, it goes in the opposite direction. For more realism, I added
// ballD/2, so the ball gets closer before it bounces off.
// This function is still buggy as I have not been able to work those bugs
// out.
void checkBallAgainstBat() {
if (ballX + ballD/2 > batX && ballX + ballD/2 < batX + batW) {
if (ballY + ballD/2 - ballD > height-batH-ballD) {
ballDy *= -1;
}
}
} - // this function checks if the ball has hit the brick. It should bounce off
// the brick and return true if so
// This function is supposed to work as the checkBallAgainstBat() works
// but for some reason it is all messed up and I can't seem to figure it
// out.
boolean checkBallAgainstBrick() {
if (ballX + ballD/2 > brickX && ballX + ballD/2 < brickX + brickW) {
if (ballY > brickH-ballD) {
ballDy *= -1;
return true;
}
}
return false;
} - // this function checks if the ball has hit the top, left or right
// walls and update its
// direction as appropriate so the ball bounces off the walls
void checkBallAgainstWalls() {
if (ballX + ballD > width) {
ballDx *= -1;
}
if (ballX - ballD < 0) {
ballDx *= -1;
}
if (ballY - ballD < 0) {
ballDy *= -1;
}
}
The function that doesn't seem to work for me is:
- // this function checks if the ball has hit the brick. It should bounce off
// the brick and return true if so
// This function is supposed to work as the checkBallAgainstBat() works
// but for some reason it is all messed up and I can't seem to figure it
// out.
boolean checkBallAgainstBrick() {
if (ballX + ballD/2 > brickX && ballX + ballD/2 < brickX + brickW) {
if (ballY > brickH-ballD) {
ballDy *= -1;
return true;
}
}
return false;
}
I have played around with it, I have done so much to it I can't even remember. I still can't figure out why it acts weird. I don't want the brick to disappear or anything, I will work on that later. All I need to know is why this function isn't operating as normal and how I can fix it.
I'd really appreciate a detailed reply on this as I have been working on it for weeks, literally.
1