# Could u mind help me make brick game?

i want to make special brickgame

It is characteristic that the brick line is continuously generated and comes down from the top.

i can make normal brickgame but its very hard to me

I want you to give me answers or hints.
plz ㅜㅜ help me

``````int x=20, y=20, xd=8, yd=9;
PImage img;
int [ ] [ ] bricks = new int [2][10];
int i, j;
float d, down;
int score;

void setup() {
size(500, 700);

newGame();
}

void draw() {

if (play==true) {

image(img, 0, 0, 500, 700);

noStroke();
fill(255, 0, 0);
ellipse(x, y, 10, 10);
x=x+xd;
y=y+yd;
if (x < 5 || x > width-5) xd=xd*-1;

if (y < 5) yd=yd*-1;
y+5 > height-20)
{

if (keyPressed && keyCode==LEFT) xd -=2;
else if (keyPressed && keyCode==RIGHT) xd+=2;

yd=yd*-1;
}

stroke(0, 0, 255);

fill(255, 0, 0);
;
textSize(20);
text(score, 50, 50);

if (y>height) {
play=false;
}
d+=0.1;
for (i=0; i<2; i++)
for ( j=0; j<10; j++) {
strokeWeight(5);
if (bricks[i] [ j] == 1) fill(255);
else {
fill (0, 0, 0, 0);
strokeWeight(0);
}
down=-(i*20)+20+d;
rect (j*50, down, 50, 20);
}

if (y<down) {
if (bricks [y/20] [x/50]==1) {
bricks [y/20] [x/50] = 0;
yd=yd*-1;
score++;
}
}
fill(255, 0, 0);
;
textSize(20);
text(score, 50, 50);
}

if (mousePressed == true)
newGame();
}
}

void newGame() {
int i, j;
x = width / 2;
y = height / 2;
xd= 8;
yd= 9;
play = true;
score = 0;
for (i=0; i<2; i++)
for ( j=0; j<10; j++) {
bricks[i] [j] = 1;
}
fill (100);
strokeWeight (2);
stroke (255);
}
``````
1 Like

It is unlikely you will get help if you can’t post formatted code. Please edit your post, select your code, and hit the FORMAT THIS LIKE CODE button, which looks like this: </>

You will probably want to start by writing a class for your Brick objects.

I’ll try my best, but since i don’t have your img file and you’d have to make a Brick class anyway, there will be a lot of changes, so i’ll just give you an approximate Brick class and you’ll have to implement it how you want it to have.

It should give you everything you need to get a whole game done just using these. If you have an image for each brick, you can just add a variable in the class. This way you can also have different img for different types of Blocks and also make different resistances for them. Like more or less hits needed. Well, thinking about it, it doesn’t have everything you need to make the whole game, but you’ll only need to add a method to draw the rect/img on the canvas and one to receive damage/ see if it got hit.

``````Brick[] bricks = new Brick[1];

void setup() {
size(500, 700);
bricks[0] = new Brick(0, 0); //create the first brick. Use for loop if you want to start with multiple
}

void draw() {
// i'll just give you examples on how to use the methods

//if you want to move all Bricks one line down
for (int i = 0; i < bricks.length; i++) {
bricks[i].move(0, 20); //if your bricks height is 20
}

//to create a new Brick at the position you want it. ArrayIndex will be last(bricks.length-1)
//if you want to modify it after creating it
createBrick(100, 100);

//remove the brick you want at the index
destroyBrick(1);
//or if you dont know the index, you can get the index from the position
destroyBrick(getBricksIndexByPos(0, 20));
exit();
}

void createBrick(int x, int y) { //not my best work, but will do what it should
Brick[] tempBricks = new Brick[bricks.length+1];
arrayCopy(bricks, tempBricks);
bricks = new Brick[tempBricks.length];
arrayCopy(tempBricks, bricks);
bricks[bricks.length-1] = new Brick(x, y);
}

void destroyBrick(int idx) { //destroy the brick at the specific index
if (idx != -1) {
Brick[] tempBricks = new Brick[bricks.length];
arrayCopy(bricks, tempBricks);
bricks = new Brick[bricks.length-1];
for (int i = 0; i < idx; i++) {
bricks[i] = tempBricks[i];
}
for (int i = idx; i < bricks.length; i++) {
bricks[i] = tempBricks[i+1];
}
}
}

int getBricksIndexByPos(int x, int y) {
for (int i = 0; i < bricks.length; i++) {
if (bricks[i].pos.x == x && bricks[i].pos.y == y) {
return i;
}
}
return -1;
}

class Brick {
PVector pos;

Brick(int x, int y) {
pos = new PVector(x, y);
}

void move(int x, int y) { // to move the Brick down just call this like : move(0, brickHeight);
pos = new PVector(pos.x + x, pos.y + y);
}
}

``````
1 Like