Hi everybody,
I'm working on a project with a partner for school. Currently we are remixing the BREAK OUT game but we're having some difficulty with the bricks.
We want the bricks to be randomly positioned on the canvas. We want these blocks to...
As it is right now, the program is constantly redrawing the shapes. We also need to do the collision for the ball on the blocks and the paddle... we have the conceptual idea on how to do it, just not sure how to write it out.
Any help would be greatly appreciated (and of course credited!).
I'm working on a project with a partner for school. Currently we are remixing the BREAK OUT game but we're having some difficulty with the bricks.
We want the bricks to be randomly positioned on the canvas. We want these blocks to...
- Generate within a certain area and leave an empty space in the middle.
- Shapes do not overlap each other.
- Don't go over the canvas.
As it is right now, the program is constantly redrawing the shapes. We also need to do the collision for the ball on the blocks and the paddle... we have the conceptual idea on how to do it, just not sure how to write it out.
Any help would be greatly appreciated (and of course credited!).
- //variables
Paddle playingPaddle;
brickWall playingBrickWall;
/* char[] shapeNames = new char[7];
Brick[] brickNumber = new Brick[20];
color[] shapeColor = new color[4]; */
int brickSize = 30;
void setup() {
background(0);
size(500, 500);
smooth();
noStroke();
//Defining the Shape Array
/* shapeNames[0] = 'c'; //Circle
shapeNames[1] = 's'; //square
shapeNames[2] = 'r'; //rectangle
shapeNames[3] = 't'; //triangle
shapeNames[4] = 'p'; //parallelogram
shapeNames[5] = 'd'; //diamond
shapeNames[6] = 'T'; //trapezoid
//Defining the color array
//Red
shapeColor[0] = (color (255, 0, 0));
//Yellow
shapeColor[1] = (color(255, 220, 0));
//Green
shapeColor[2] = (color(0, 200, 50));
//Purple
shapeColor[3] = (color(100, 0, 150));
for (int i=0; i < brickNumber.length; i++) {
brickNumber[i] = new Brick(int(random(width-brickSize)), int(random(height-brickSize)), brickSize, shapeColor[int(random(shapeColor.length))], shapeNames[int(random(shapeNames.length))]);
} */
playingBrickWall = new brickWall();
playingPaddle = new Paddle();
}
void draw() {
background(0);
/* for (int i=0; i<brickNumber.length; i++) {
//println(brickNumber[i].shapeName);
brickNumber[i].drawShape();
} */
playingBrickWall.brickSpace();
playingPaddle.display();
playingPaddle.updatePosition();
}
class brickWall {
//properties
int x = 25;
int y = 25;
int wallWidth = width -x*2;
int wallHeight = height -y*2;
int numBricks;
public brickWall() {
//Commence the arrays!
char[] shapeNames = new char[7];
Brick[] brickNumber = new Brick[20];
color[] shapeColor = new color[4];
//Defining the arrays...
//Bricks
shapeNames[0] = 'c'; //Circle
shapeNames[1] = 's'; //square
shapeNames[2] = 'r'; //rectangle
shapeNames[3] = 't'; //triangle
shapeNames[4] = 'p'; //parallelogram
shapeNames[5] = 'd'; //diamond
shapeNames[6] = 'T'; //trapezoid
//Colors
shapeColor[0] = (color(255, 0, 0)); //Red
shapeColor[1] = (color(255, 220, 0)); //Yellow
shapeColor[2] = (color(0, 200, 50)); //Green
shapeColor[3] = (color(100, 0, 150)); //Purple
}
public void brickSpace() {
//void function for
rectMode(CORNER);
fill(255);
noStroke();
//noFill();
rect(x, y, wallWidth, wallHeight);
//Commence the arrays!
char[] shapeNames = new char[7];
Brick[] brickNumber = new Brick[20];
color[] shapeColor = new color[4];
//Defining the arrays...
//Bricks
shapeNames[0] = 'c'; //Circle
shapeNames[1] = 's'; //square
shapeNames[2] = 'r'; //rectangle
shapeNames[3] = 't'; //triangle
shapeNames[4] = 'p'; //parallelogram
shapeNames[5] = 'd'; //diamond
shapeNames[6] = 'T'; //trapezoid
//Colors
shapeColor[0] = (color(255, 0, 0)); //Red
shapeColor[1] = (color(255, 220, 0)); //Yellow
shapeColor[2] = (color(0, 200, 50)); //Green
shapeColor[3] = (color(100, 0, 150)); //Purple
//Go go power Brick array!!
for (int i=0; i < brickNumber.length; i++) {
brickNumber[i] = new Brick(int(random(width-brickSize)), int(random(height-brickSize)), brickSize, shapeColor[int(random(shapeColor.length))], shapeNames[int(random(shapeNames.length))]);
}
for (int i=0; i<brickNumber.length; i++) {
//println(brickNumber[i].shapeName);
brickNumber[i].drawShape();
/* if (Brick.covers(Brick withBrick) == true) {
//blah blah blah
} */
}
}
}- public class Brick {
//Properties
int brickSize;
int x;
int y;
//Color stuff
int shapeColor = 0;
color fillColor;
//Hit points for shapes
int HP;
char shapeName;
//Constructor
public Brick (int tempX, int tempY, int tempSize, color newColor, char shapeName) {
x = tempX;
y = tempY;
brickSize = tempSize;
fillColor = newColor;
shapeColor = newColor;
this.shapeName = shapeName;
}
//functions
public boolean covers(Brick withBrick) {
//Brick withBrick says COLLISION DETECTED!!
if (this.x >= withBrick.x && this.x <= withBrick.x+brickSize ||
this.y >= withBrick.y && this.y <= withBrick.y+brickSize) {
return (true);
}
//Brick withBrick says no collision detected.
else {
return (false);
}
}
boolean bricksInCanvas() {
//I said keep all hands and feet within the canvas!
if (this.x <= 0 && this.x >= width ||
this.y <= 0 && this.y >= height) {
return (true);
}
//Please enjoy your flight.
else {
return (false);
}
}
void drawShape() {
switch(shapeName) {
case 'c':
//Circle
circleBrick();
break;
case 's':
//Square
squareBrick();
break;
case 'r':
//rectangle
rectBrick();
break;
case 't':
//triangle
triangleBrick();
break;
case 'p':
//parallelogram
parallelBrick();
break;
case 'd':
diamondBrick();
break;
case 'T':
//trapezoid
trapezoidBrick();
break;
}
}
void circleBrick() {
ellipseMode(CORNER);
fill(fillColor);
ellipse(x, y, brickSize, brickSize);
}
void squareBrick() {
fill(fillColor);
rectMode(CORNER);
rect(x, y, brickSize, brickSize);
}
void rectBrick() {
fill(fillColor);
rectMode(CORNER);
rect(x, y, brickSize, (brickSize*2));
}
void triangleBrick() {
fill(fillColor);
triangle(x, y, x+brickSize/2, y-brickSize, x+brickSize, y);
}
void parallelBrick() {
fill(fillColor);
//Parallelogram
quad(x+brickSize/2, y-brickSize/1.5, x+brickSize*2, y-brickSize/1.5, x+brickSize*1.5, y, x, y);
}
void diamondBrick() {
fill(fillColor);
//diamond shape
quad(x, y, x+brickSize/2, y-brickSize, x+brickSize, y, x+brickSize/2, y+brickSize);
}
void trapezoidBrick() {
fill(fillColor);
//Trapezoid
quad(x, y, x+brickSize, y, x+brickSize*2, y+brickSize/1.5, x, y+brickSize/1.5);
}
}
1