• Create Account

#ActualBeerNutts

Posted 21 December 2012 - 01:29 PM

Here's something to get you started:

struct TBrick {
int XPosition;
int YPosition;
int XSize;
int YSize;
uint32_t Color;
uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1
};

struct TBall {
int XPosition;
int YPosition;
int XSize;
int YSize;
int XSpeed;
int YSpeed;
};

#define BRICK_COLUMNS 20 // how many bricks in a column
#define BRICK_ROWS 10 // how many bricks ina  row
#define BRICK_START_X 60 // Where the bricks start on the x-axis
#define BRICK_START_Y 40 // Where the bricks start on the y-axis
#define BRICK_WIDTH 30 // brick width in pixels
#define BRICK_HEIGHT 15 // brickheight in pixels

// make this a global array
TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];

// Initialize the Brick Array somewhere at the start of your code
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
BrickArray[x][y].XSize = BRICK_WIDTH;
BrickArray[x][y].YSize = BRICK_HEIGHT;
BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;
BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;
BrickArray[x][y].Color = 0xFFFFFFFF; // white
BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick

// in main loop do this:
// move Ball in X direction, then check collision
Ball.XLocation += Ball.XSpeed;
if (CheckCollision(Ball)) {
// The ball hit something, move to original location and negate X speed
Ball.XLocation -= Ball.XSpeed;
Ball.XSpeed = -Ball.XSpeed;
}

// Do same for Y movement
Ball.YLocation += Ball.YSpeed;
if (CheckCollision(Ball)) {
// The ball hit something, move to original location and negate Y speed
Ball.YLocation -= Ball.YSpeed;
Ball.YSpeed = -Ball.YSpeed;
}

RenderBricks();

... // the rest of the main loop

// Here are those functions
bool CheckCollisions(TBall ball)
{
// loop through every brick and see if we've hit it
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
// Only check against bricks that are active
if (BrickArray[x][y].ActiveState > 0) {
if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||
ball.YLocation  + ball.YSize < BrickArray[x][y].YLocation ||
ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||
ball.YLocation  < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {
// It's collided wit brick, decrment Active state of brick and return true
BrickArray[x][y].ActiveState--;
return true;
}
}
}

// Check if we've hit the paddle and
// Check if we've hit the wall (I'll leave this up to you)
}

// This Draws the bricks
void RenderBricks()
{
// loop through every brick and draw if active
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
if (BrickArray[x][y].ActiveState > 0) {
// Call you GL draw function, whatever it is.  it would be simple to do in SFML however
GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,
BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);
}
}
}
}

This is just some simple code that shows how you can handle the bricks.  You might have a class for bricks which would make more sense, but this gets the point across.

What's missing is checking if the ball hits the paddle, or if the ball hit a wall.  it also doesn't check if the ball has left the screen (i, the paddle missed it), and it doesn't check if all blocks are cleared (level is over).

Can you show that code?  It would be a good step for you.

Good luck!

#2BeerNutts

Posted 21 December 2012 - 01:25 PM

Here's something to get you started:

struct TBrick {
int XPosition;
int YPosition;
int XSize;
int YSize;
uint32_t Color;
uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1
};

struct TBall {
int XPosition;
int YPosition;
int XSize;
int YSize;
int XSpeed;
int YSpeed;
};

#define BRICK_COLUMNS 20 // how many bricks in a column
#define BRICK_ROWS 10 // how many bricks ina  row
#define BRICK_START_X 60 // Where the bricks start on the x-axis
#define BRICK_START_Y 40 // Where the bricks start on the y-axis
#define BRICK_WIDTH 30 // brick width in pixels
#define BRICK_HEIGHT 15 // brickheight in pixels

// make this a global array
TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];

// Initialize the Brick Array somewhere at the start of your code
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
BrickArray[x][y].XSize = BRICK_WIDTH;
BrickArray[x][y].YSize = BRICK_HEIGHT;
BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;
BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;
BrickArray[x][y].Color = 0xFFFFFFFF; // white
BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick

// in main loop do this:
// move Ball in X direction, then check collision
Ball.XLocation += Ball.XSpeed;
if (CheckCollision(Ball)) {
// The ball hit something, move to original location and negate X speed
Ball.XLocation -= Ball.XSpeed;
Ball.XSpeed = -Ball.XSpeed;
}

// Do same for Y movement
Ball.YLocation += Ball.YSpeed;
if (CheckCollision(Ball)) {
// The ball hit something, move to original location and negate Y speed
Ball.YLocation -= Ball.YSpeed;
Ball.YSpeed = -Ball.YSpeed;
}

RenderBricks();

... // the rest of the main loop

// Here are those functions
bool CheckCollisions(TBall ball)
{
// loop through every brick and see if we've hit it
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
// Only check against bricks that are active
if (BrickArray[x][y].ActiveState > 0) {
if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||
ball.YLocation  + ball.YSize < BrickArray[x][y].YLocation ||
ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||
ball.YLocation  < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {
// It's collided wit brick, decrment Active state of brick and return true
BrickArray[x][y].ActiveState--;
return true;
}
}
}

// Check if we've hit the paddle and
// Check if we've hit the wall (I'll leave this up to you)
}

// This Draws the bricks
void RenderBricks()
{
// loop through every brick and draw if active
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
if (BrickArray[x][y].ActiveState > 0) {
// Call you GL draw function, whatever it is.  it would be simple to do in SFML however
GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,
BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);
}
}
}
}

This is just some simple code that shows how you can handle the bricks.  You might have a class for bricks which would make more sense, but this gets the point across.

Good luck!

#1BeerNutts

Posted 21 December 2012 - 01:23 PM

Here's something to get you started:

struct TBrick {
int XPosition;
int YPosition;
int XSize;
int YSize;
uint32_t Color;
uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1
};

struct TBall {

int XPosition;
int YPosition;
int XSize;
int YSize;
int XSpeed;
int YSpeed;
};

#define BRICK_COLUMNS 20 // how many bricks in a column
#define BRICK_ROWS 10 // how many bricks ina  row
#define BRICK_START_X 60 // Where the bricks start on the x-axis
#define BRICK_START_Y 40 // Where the bricks start on the y-axis
#define BRICK_WIDTH 30 // brick width in pixels
#define BRICK_HEIGHT 15 // brickheight in pixels

// make this a global array
TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];

// Initialize the Brick Array somewhere at the start of your code
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
BrickArray[x][y].XSize = BRICK_WIDTH;
BrickArray[x][y].YSize = BRICK_HEIGHT;
BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;
BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;
BrickArray[x][y].Color = 0xFFFFFFFF; // white
BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick

// in main loop do this:
// move Ball in X direction, then check collision
Ball.XLocation += Ball.XSpeed;
if (CheckCollision(Ball)) {
// The ball hit something, move to original locaiton and negate X speed
Ball.XLocation -= Ball.XSpeed;
Ball.XSpeed = -Ball.XSpeed;
}

// Do same for Y movement

Ball.YLocation += Ball.YSpeed;
if (CheckCollision(Ball)) {
// The ball hit something, move to original location and negate Y speed
Ball.YLocation -= Ball.YSpeed;
Ball.YSpeed = -Ball.YSpeed;
}

RenderBricks();

// Here are those functions
bool CheckCollisions(TBall ball)
{
// loop through every brick and see if we've hit it
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
// Only check against bricks that are active
if (BrickArray[x][y].ActiveState > 0) {
if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||
ball.YLocation  + ball.YSize < BrickArray[x][y].YLocation ||
ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||
ball.YLocation  < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {
// It's collided wit brick, decrment Active state of brick and return true
BrickArray[x][y].ActiveState--;
return true;
}
}
}

// Check if we've hit the paddle and
// Check if we've hit the wall (I'll leave this up to you)
}

// This Draws the bricks
void RenderBricks()
{
// loop through every brick and draw if active
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
if (BrickArray[x][y].ActiveState > 0) {
// Call you GL draw function, whatever it is.  it would be simple to do in SFML however
GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,
BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);
}
}
}
}

This is just some simple code that shows how you can handle the bricks.  You might have a class for bricks which would make more sense, but this gets the point across.

Good luck!

PARTNERS