Advertisement

return to breakout

Started by January 10, 2013 04:53 AM
26 comments, last by L. Spiro 11 years, 10 months ago
I graduated from college with a b.s. in c.s. in 2005.

Which college?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Phil, what did you not understand about this code I posted?

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

It gives a general idea of how to encapsulate brick's and the ball into it's own structure, and shows how to handle collisions and rendering.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
I graduated from California state san bernardino
thanks beer for the code, so how should I digest the code you have given me, I tend to learn more with smaller amounts of code.

Start with the structures:


 
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;
};
Do you understand why that does, and understand why they are group'd together?
Then, look at the array of bricks, and the loop that initializes the brick array:


#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
  }
}
Do you know what that is doing? And slowly go form there. Just break up " a lot of code" into small pieces of code. read the comments in the code. It explains every thing.
If you have questions about certain parts of it, ask about it. it's better than staring at something not understanding what it's doing.
I graduated from California state san bernardino

Really? BS in CS? Do you have a job as a programmer now?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

yes a bachelors in computer science, I am unemployed at the moment, although I have had some job interviews last year. I am still looking for work right now.
Advertisement
yeah!! thanks for all the help, I finally solved the collision problem with the ball and bricks.
yeah!! thanks for all the help, I finally solved the collision problem with the ball and bricks.

Show us your solution. I've heard you say that before, but you come back and ask the question again.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

well here it is if( g_bBlock[4].m_bActive_three==true) { if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f) { ystep = -ystep; bricks[2][4]=true; g_bBlock[4].m_bActive_three=false; } }

well here it is


if(	g_bBlock[4].m_bActive_three==true)	
	{
	if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f)
	{
	ystep = -ystep;
	bricks[2][4]=true;
	g_bBlock[4].m_bActive_three=false;	
	}
	}

Sigh.

...

Just...Sigh.

I give up. Good luck to you.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement