Win32 SDK 打砖块游戏(四)

2014-11-24 09:00:43 · 作者: · 浏览: 3
e
if (KEY_DOWN(VK_RIGHT))
{
// move paddle to right
paddle_x += 8;

// make sure paddle doesn't go off screen
if (paddle_x > (WINDOW_WIDTH - PADDLE_WIDTH))
paddle_x = WINDOW_WIDTH - PADDLE_WIDTH;
}
else if (KEY_DOWN(VK_LEFT))
{
// move paddle to left
paddle_x += 8;

// make sure paddle doesn't go off screen
if (paddle_x < 0)
paddle_x = 0;
}

// draw blocks
Draw_Blocks();

// move the ball
ball_x += ball_dx;
ball_y += ball_dy;

// keep ball on screen, if the ball hits the edge of screen then
// bounce it by reflecting its velocity
if (ball_x > (WINDOW_WIDTH - BALL_SIZE) || ball_x < 0)
{
// reflect x-axis velocity
ball_dx = -ball_dx;

// update position
ball_x += ball_dx;
}

// now y-axis
if (ball_y < 0)
{
// reflect y-axis velocity
ball_dy = -ball_dy;

// update position
ball_y += ball_dy;
}
else if (ball_y > (WINDOW_HEIGHT - BALL_SIZE))
{
// reflect y-axis velocity
ball_dy = -ball_dy;

// update position
ball_y += ball_dy;

// minus the score
score -= 100;
}

// next watch out for ball velocity getting out of hand
if (ball_dx > 8)
ball_dx = 8;
else if (ball_dx < -8)
ball_dx = -8;

// test if ball hit any blocks or the paddle
Process_Ball();


// draw the paddle
Draw_Rectangle(paddle_x, paddle_y, paddle_x+PADDLE_WIDTH, paddle_y+PADDLE_HEIGHT, PADDLE_COLOR);


// draw the ball
Draw_Rectangle(ball_x, ball_y, ball_x+BALL_SIZE, ball_y+BALL_SIZE, 255);


// draw the info
wsprintf(buffer, TEXT("FREAKOUT Score %d Level %d"), score, level);
DrawText_GUI(buffer, 8, WINDOW_HEIGHT-16, 127);

// sync to 30 fps
Wait_Clock(30);

// check if user is trying to exit
if (KEY_DOWN(VK_ESCAPE))
{
// send message to windows to exit
PostMessage(main_window_handle, WM_DESTROY, 0, 0);

// set exit state
game_state = GAME_STATE_SHUTDOWN;
}
}
else if (game_state == GAME_STATE_SHUTDOWN)
{
// in this state shut everything down and release resources

// switch to exit state
game_state = GAME_STATE_EXIT;
}

return 1;
}

3.小球的控制和绘制(原程序为directDraw绘制,此处改为Win32 API绘制)
[cpp]
/* DRAW FUNCTION **************************************************************************/
int Draw_Rectangle(int x1, int y1, int x2, int y2, int color)
{
// this function uses Win32 API to draw a filled rectangle
HBRUSH hbrush;
HDC hdc;
RECT rect;

SetRect(&rect, x1, y1, x2, y2);

hbrush = CreateSolidBrush(color);
hdc = GetDC(main_window_handle);

FillRect(hdc, &rect, hbrush);
ReleaseDC(main_window_handle, hdc);
DeleteObject(hbrush);

return 1;
}

int DrawText_GUI(TCHAR *text, int x, int y, int color)
{
HDC hdc;

hdc = GetDC(main_window_handle);

// set the colors for t