Brick Breaker help
in
Programming Questions
•
1 year ago
Hi Everybody!
I made a brick breaker game that works pretty well besides a few problems with the ball and brick collisions. I have it set up so if the ball touches a brick just its y-speed will change, but I need it to be able to detect the sides of bricks also. I'm not quite sure how to do this though. I think booleans could work well, but I don't know how to set it up so just the sides or just the top and bottom are detected.
Here is my overall code:
- void setup() {
- size(800, 600);
- smooth();
- variables();
- bricklist = new ArrayList();
- for (int tx = 0; tx <= 9; tx++) {
- for (int ty = 0; ty <= layers-1; ty++) {
- bricklist.add(new Brick(tx*w+w/2, ty*h+h/2, ceil(random(4))));
- }
- }
- }
- void draw() {
- background(0);
- gamestarter();
- win_loss();
- next_level();
- complete_victory();
- paddle();
- ball();
- screen_ballConstraints();
- ball_paddleConstraints();
- for (int i = bricklist.size()-1; i >= 0; i--) {
- Brick onebrick = (Brick) bricklist.get(i);
- onebrick.display();
- if (onebrick.collision(bx, by, bsize)) {
- bspeedy *= -1;
- bricklist.remove(i);
- }
- }
- }
- float px, py, bx, by, bspeedx, bspeedy, direction;
- int pwidth, pheight, pspeed, bsize, w, h, layers;
- boolean left, right, started;
- ArrayList bricklist;
- void variables() {
- px = width/2;
- py = height-20;
- pwidth = 100;
- pheight = 15;
- pspeed = 3;
- left = false;
- right = false;
- bx = width/2;
- by = height/2;
- bsize = 20;
- bspeedx = 0;
- bspeedy = 0;
- started = false;
- w = width/10;
- h = 20;
- layers = 4;
- direction = 0;
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == LEFT) {
- left = true;
- }
- }
- if (key == CODED) {
- if (keyCode == RIGHT) {
- right = true;
- }
- }
- }
- void keyReleased() {
- if (key == CODED) {
- if (keyCode == LEFT) {
- left = false;
- }
- }
- if (key == CODED) {
- if (keyCode == RIGHT) {
- right = false;
- }
- }
- }
- void paddle() {
- if (left == true) {
- pspeed = 4;
- px -= pspeed;
- }
- else {
- if (left == false) {
- pspeed = 0;
- }
- }
- if (right == true) {
- pspeed = 4;
- px += pspeed;
- }
- else {
- if (right == false) {
- pspeed = 0;
- }
- }
- fill(255);
- rectMode(CENTER);
- rect(px, py, pwidth, pheight);
- }
- void ball() {
- bx += bspeedx;
- by += bspeedy;
- fill(255);
- ellipse(bx, by, bsize, bsize);
- }
- void ball_paddleConstraints() {
- if (bx > px-pwidth/2 && bx < px+pwidth/2 && by+bsize/3 > py-pheight) {
- bspeedy *= -1;
- if (bx > px) {
- direction = (dist(bx, by+bsize/2, px, py-pheight/2))*-1;
- }
- if (bx < px) {
- direction = dist(bx, by+bsize/2, px, py-pheight/2);
- }
- bspeedx = direction/10;
- }
- }
- void screen_ballConstraints() {
- if (bx+bsize/2 > width || bx-bsize/2 < 0) {
- bspeedx *= -1;
- }
- if (by-bsize/2 < 0) {
- bspeedy *= -1;
- }
- }
- class Brick {
- float x, y;
- int w, h;
- int c;
- Brick(float X, float Y, int C) {
- x = X;
- y = Y;
- c = C;
- w = width/10;
- h = 20;
- }
- void display() {
- if (c == 1) {
- fill(#FF0303);
- }
- if (c == 2) {
- fill(#FFF703);
- }
- if (c == 3) {
- fill(#03FF04);
- }
- if (c == 4) {
- fill(#0A03FF);
- }
- rect(x, y, w, h);
- }
- boolean collision(float Bx, float By, float Bsize) {
- if ((bx+bsize/2 > x-w/2) &&
- (bx-bsize/2 < x+w/2) &&
- (by-bsize/2 < y+h/2) &&
- (by+bsize/2 > y-h/2)) {
- return(true);
- }
- else {
- return(false);
- }
- }
- }
- void gamestarter() {
- if (keyPressed && started == false) {
- if ( key == ENTER || key == RETURN) {
- bspeedy = 4;
- started = true;
- }
- }
- }
- void next_level() {
- if (bricklist.size() == 0 && started == true) {
- if (keyPressed) {
- if (key == ENTER || key == RETURN) {
- background(0);
- bx = width/2;
- by = height/2;
- px = width/2;
- py = height-20;
- bspeedx = 0;
- bspeedy = 0;
- layers += 1;
- for (int tx = 0; tx <= 9; tx++) {
- for (int ty = 0; ty <= layers-1; ty++) {
- bricklist.add(new Brick(tx*80+40, ty*20+10, ceil(random(4))));
- }
- }
- started = false;
- }
- }
- }
- if (by+bsize/2 > height) {
- if (keyPressed) {
- if (key == ENTER || key == RETURN) {
- background(0);
- bx = width/2;
- by = height/2;
- px = width/2;
- py = height-20;
- bspeedx = 0;
- bspeedy = 0;
- layers = 4;
- for (int tx = 0; tx <= 9; tx++) {
- for (int ty = 0; ty <= layers-1; ty++) {
- bricklist.add(new Brick(tx*80+40, ty*20+10, ceil(random(4))));
- }
- }
- started = false;
- }
- }
- }
- }
- void complete_victory() {
- if (bricklist.size() == 0 && layers == 8) {
- background(0);
- bspeedx = 0;
- bspeedy = 0;
- textAlign(CENTER);
- textSize(40);
- fill(255);
- text("You are a SUPER AWESOME and", width/2, height/2);
- text("have completed EVERY LEVEL!", width/2, height/2+40);
- noLoop();
- }
- }
- void win_loss() {
- if (bricklist.size() == 0) {
- bspeedx = 0;
- bspeedy = 0;
- textAlign(CENTER);
- textSize(40);
- fill(255);
- text("You are a WINNER!", width/2, height/2);
- }
- if (by+bsize/2 > height) {
- bspeedx = 0;
- bspeedy = 0;
- textAlign(CENTER);
- textSize(40);
- fill(255);
- text("You are a LOSER!", width/2, height/2);
- }
- }
the boolean collision function is how my current brick detection works.
Thanks for any ideas and help!
1