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);
}
}
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);
}
}
1