Win32 SDK 打砖块游戏(五)

2014-11-24 09:00:43 · 作者: · 浏览: 1
he text up
SetTextColor(hdc, color);

// set background mode to transparent so black isn't copied
SetBkMode(hdc, TRANSPARENT);

// draw the text
TextOut(hdc, x, y, text, lstrlen(text));

// release the dc
ReleaseDC(main_window_handle, hdc);

return 1;
}


/* GAME PROGRAMMING CONSOLE FUNCTIONS *****************************************************/
void Init_Blocks(void)
{
// initialize the block field
for (int row = 0; row < NUM_BLOCK_ROWS; row++)
for (int col = 0; col < NUM_BLOCK_COLUMNS; col++)
blocks[row][col] = row*16 + col*3 + 16;
}

void Draw_Blocks(void)
{
// this function draws all the blocks in row major form
int x1 = BLOCK_ORIGIN_X;
int y1 = BLOCK_ORIGIN_Y;

// draw all the blocks
for (int row = 0; row < NUM_BLOCK_ROWS; row++)
{
// reset column position
x1 = BLOCK_ORIGIN_X;

for (int col = 0; col < NUM_BLOCK_COLUMNS; col++)
{
if (blocks[row][col] != 0)
{
Draw_Rectangle(x1, y1, x1+BLOCK_WIDTH, y1+BLOCK_HEIGHT, blocks[row][col]);
}

// advance column position
x1 += BLOCK_X_GAP;
}

// advance to next row position
y1 += BLOCK_Y_GAP;
}
}

void Process_Ball(void)
{
// this function tests if the ball has hit a block or the paddle if so, the ball is bounced
// and the block is removed from the playfield note: very cheesy collision algorithm :)

// first test for ball block collisions

// the algorithm basically tests the ball against each block's bounding box this is inefficient,
// but easy to implement, later we'll see a better way

// current rendering position
int x1 = BLOCK_ORIGIN_X;
int y1 = BLOCK_ORIGIN_Y;

// computer center of ball
int ball_cx = ball_x + (BALL_SIZE/2);
int ball_cy = ball_y + (BALL_SIZE/2);

// test the ball has hit the paddle
if (ball_y > (WINDOW_HEIGHT/2) && ball_dy > 0)
{
// extract leading edge of ball
int x = ball_x + (BALL_SIZE/2);
int y = ball_y + (BALL_SIZE/2);

// test for collision with paddle
if ((x >= paddle_x && x <= paddle_x + PADDLE_WIDTH) &&
(y >= paddle_y && y <= paddle_y + PADDLE_HEIGHT))
{
// reflect ball
ball_dy = -ball_dy;

// push ball out of paddle since it made contact
ball_y += ball_dy;

// add a little english to ball based on motion of paddle
if (KEY_DOWN(VK_RIGHT))
ball_dx -= rand() % 3;
else if (KEY_DOWN(VK_LEFT))
ball_dx += rand() % 3;
else
ball_dx += (-1 + rand() % 3);

// test if there are no blocks, if so send a message to game loop to start another level
if (blocks_hit >= (NUM_BLOCK_ROWS * NUM_BLOCK_COLUMNS))
{
game_state = GAME_STATE_START_LEVEL;
level++;
}

// make a little noise
MessageBeep(MB_OK);

return;
}
}

// now scan thru all the blocks and see of ball hit blocks
for (int row = 0; row < NUM_BLOCK_ROWS; row++)
{
x1 = BLOCK_ORIGIN_X;

// scan this row of blocks
for (int col = 0; col < NUM_BLOCK_COLUMNS; col++)
{
if (blocks[row][col] != 0)
{
// test ball against bounding box of block
if ((ball_cx > x1) && (ball_cx < x1 + BLOCK_WIDTH) &&
(ball_cy > y1) && (ball_cy < y1 + BLOCK_HEIGHT))
{
// remove the block
bloc