Loading...
Logo
Processing Forum

Brick Breaker

in Programming Questions  •  1 year ago  
I'm trying to re create a simple version of brick breaker.  I'm trying to use array list to create the bricks but i'm not very familiar with it. I'm giving all the code i have for the program but the main concern is the Main Code and the Bricks code.

here is my code so far:
Main code:

Ball bll;
Paddle Paddle;
ArrayList brickslist;

void setup(){
size(1024,768);
background(0);
smooth();
bll = new Ball (0,0,1,2,color(random(255),random(255),random(255)));//creates a ball that will bounce around the screen
rectMode(CENTER);
Paddle = new Paddle (height,120,30);//creates the paddle that will hit the ball into the bricks
brickslist = new ArrayList();//create an empty array list
brickslist.add(new Bricks (0,0,90,30));
}
void draw(){
background(0);
for (int i = brickslist.size()-1; i>=0; i--){
 Bricks brickslist = (Bricks) brickslist.get(i);
 brickslist.move();
 brickslist.display();
 if (brickslist.finished()){
   brickslist.remove(i);
 }
}
bll.move();//lets the ball move
bll.bounce();//allows ball to bounce off the edges
bll.display();//displays the ball
Paddle.move();//paddle is allowed to move
  Paddle.display();//displays the paddle
}

Ball Code (in new tab):

class Ball{
 float xposition;
 float yposition;
 float xspeed;
 float yspeed;
 color c;
//all the code for the class goes here
Ball(float xpos,float ypos, float xsp,float ysp, color c1){
  xposition = xpos;
  yposition = ypos;
  xspeed = xsp;
 yspeed = ysp;
c = c1;
ysp = 2;
xsp = 2;
}
void move() {
xposition = xposition + xspeed;// Change the x location by speed
yposition = yposition + yspeed;
}
//A function to void bounce the ball
void bounce() {

if ((xposition > width) || (xposition < 0)) {// If we've reached an edge, reverse speed
xspeed = xspeed *  -1;
}
if ((yposition > height) || (yposition <0)) {
  yspeed = yspeed * -1;
}
if ((yposition >680) && (xposition<mouseX+50)){
  yspeed = yspeed * -1;
 
}
}
//A function to display the ball
void display() {
stroke(0);
fill(c);
ellipse(xposition,yposition,32,32);
}
}

Paddle(also in new tab)

//Paddle
class Paddle{
  float xpos;
  float hgt;
  float wdth;
 
  Paddle(float tempXpos, float tempWdth, float tempHgt){
    xpos = tempXpos;
    hgt = tempHgt;
    wdth = tempWdth;
   
  }
 
 void display(){
   noStroke();
   fill(255);
   rectMode(CENTER);
   rect(mouseX,700,120,30);
 }
 
 //mouse moves the paddle horizonally
 void move(){
   constrain(xpos, 0+(hgt/2), 400-(hgt/2));
   xpos=mouseX;  
  }
}

Bricks(again a new tab):

//Bricks
class Bricks{
 float hgt;
 float wdth;
 float x;
 float y;
 Bricks(float tempx,float tempy, float tempwdth, float temphgt){
   hgt = temphgt;
   wdth = tempwdth;
   x = tempx;
   y = tempy;
 }
void display(){
   noStroke();
   fill(255);
   rectMode(CENTER);
   rect(x,y,wdth,hgt);
}
}

Replies(2)

Re: Brick Breaker

1 year ago
" the main concern is the Main Code and the Bricks code"
I don't see a question in your message...
Have you an error message? A specific issue?

Re: Brick Breaker

1 year ago


to keep you motivated a few changes:

look for the word "wrong" and also for "more attention" and think about it further...

Greetings, Chrisir    



Copy code
  1. Ball bll;
  2. Paddle Paddle;
  3. ArrayList brickslist;
  4. void setup() {
  5.   size(1024, 768);
  6.   background(0);
  7.   noCursor();
  8.   smooth();
  9.   //creates a ball that will bounce around the screen
  10.   bll = new Ball (175, 420, 1, 2, color(random(255), random(255), random(255)));
  11.   rectMode(CENTER);
  12.   //creates the paddle that will hit the ball into the bricks 
  13.   Paddle = new Paddle (height, 120, 30);
  14.   //create an empty array list for the Bricks
  15.   brickslist = new ArrayList();
  16.   // fill the array list
  17.   for (int x = 1; x<=9; x++) {
  18.     for (int y = 1; y<=4; y++) {
  19.       brickslist.add(new Bricks (x*100+15, y*40+20, 90, 30));
  20.     } // for
  21.   } // for
  22. } // func
  23. //
  24. void draw() {
  25.   background(0);
  26.   //
  27.   //lets the ball move
  28.   bll.move();
  29.   // check collisions with Bricks
  30.   for (int i = brickslist.size()-1; i>=0; i--) {
  31.     // get the brick #i
  32.     Bricks oneSingleBrick = (Bricks) brickslist.get(i);
  33.     // check it
  34.     if (oneSingleBrick.collide( bll.xposition, bll.yposition, bll.radiusBall ) ) {
  35.       // this needs more attention: when he hits from below, yspeed needs to
  36.       // be changed; when from the sides, xspeed
  37.       bll.yspeed = bll.yspeed * -1;
  38.       brickslist.remove(i);
  39.     } // if
  40.   } // for  
  41.   bll.bounce();//allows ball to bounce off the edges
  42.   bll.display();//displays the ball
  43.   // display Bricks
  44.   for (int i = brickslist.size()-1; i>=0; i--) {
  45.     Bricks oneSingleBrick = (Bricks) brickslist.get(i);
  46.     oneSingleBrick.display();
  47.   } // for
  48.   // now Paddle stuff
  49.   Paddle.move();//paddle is allowed to move
  50.   Paddle.display();//displays the paddle
  51. } // func
  52. // ==============================================================
  53. // Ball Code (in new tab):
  54. class Ball {
  55.   float xposition;
  56.   float yposition;
  57.   float xspeed;
  58.   float yspeed;
  59.   color c;
  60.   // two times the radius:
  61.   int diameterBall=32; // wrong name
  62.   // the single radius
  63.   int radiusBall=diameterBall / 2;  // wrong name
  64.   //
  65.   //all the code for the class goes here
  66.   Ball(float xpos, float ypos, float xsp, float ysp, color c1) {
  67.     xposition = xpos;
  68.     yposition = ypos;
  69.     xspeed = xsp;
  70.     yspeed = ysp;
  71.     c = c1;
  72.     ysp = 2;
  73.     xsp = 2;
  74.   }
  75.   // move ball
  76.   void move() {
  77.     xposition = xposition + xspeed;// Change the x location by speed
  78.     yposition = yposition + yspeed;
  79.   }
  80.   //A function to void bounce the ball
  81.   void bounce() {
  82.     // If we've reached an edge, reverse speed: X
  83.     if ((xposition > width-radiusBall) || (xposition < radiusBall)) {
  84.       xspeed = xspeed *  -1;
  85.     }
  86.     // ... and y
  87.     if ((yposition > height-radiusBall) || (yposition < radiusBall)) {
  88.       yspeed = yspeed * -1;
  89.     }
  90.     // paddle
  91.     if ((yposition >  670) && (xposition < mouseX+50)) {
  92.       yspeed = yspeed * -1;
  93.     }
  94.   }
  95.   //A function to display the ball
  96.   void display() {
  97.     stroke(0);
  98.     fill(c);
  99.     ellipse(xposition, yposition, diameterBall, diameterBall);
  100.   } // func
  101. } // class
  102. // ==============================================================
  103. // Paddle(also in new tab)
  104. //Paddle
  105. class Paddle {
  106.   float xpos;
  107.   float hgt;
  108.   float wdth;
  109.   Paddle(float tempXpos, float tempWdth, float tempHgt) {
  110.     xpos = tempXpos;
  111.     hgt = tempHgt;
  112.     wdth = tempWdth;
  113.   }
  114.   void display() {
  115.     noStroke();
  116.     fill(255);
  117.     rectMode(CENTER);
  118.     rect(mouseX, 700, 120, 30);
  119.   }
  120.   //mouse moves the paddle horizonally
  121.   void move() {
  122.     constrain(xpos, 0+(hgt/2), 400-(hgt/2));
  123.     xpos = mouseX;
  124.   } // func
  125. } // class
  126. // ==============================================================
  127. // Bricks(again a new tab):
  128. //Bricks
  129. class Bricks {
  130.   float hgt;
  131.   float wdth;
  132.   float x;
  133.   float y;
  134.   Bricks(float tempx, float tempy, float tempwdth, float temphgt) {
  135.     hgt = temphgt;
  136.     wdth = tempwdth;
  137.     x = tempx;
  138.     y = tempy;
  139.   }
  140.   void display() {
  141.     noStroke();
  142.     fill(255);
  143.     rectMode(CENTER);
  144.     rect(x, y, wdth, hgt);
  145.   }
  146.   boolean collide (float xBall, float yBall, int radusBallHalf) {
  147.     // all wrong because of rectMode(CENTER);
  148.     if ((xBall+radusBallHalf > x) &&
  149.       (yBall+radusBallHalf > y) &&
  150.       (xBall-radusBallHalf < x+wdth) &&
  151.       (yBall-radusBallHalf < y+hgt))
  152.     {
  153.       return (true);
  154.     }
  155.     else {
  156.       return (false);
  157.     }
  158.   } // func
  159. } // class