Win32 SDK 打砖块游戏(十)

2014-11-24 09:00:43 · 作者: · 浏览: 18
ease all resources
// that you allocated

return 1;
}

int Game_Main(void *parms)
{
// this is the workhorse of your game it will be called continuously in real-time
// this is like main() in C all the calls for you game go here!

TCHAR buffer[80];

// what state is the game in
if (game_state == GAME_STATE_INIT)
{
// seed the random number generator so game is different each play
srand((unsigned int)time(0));

// set the paddle position here to the middle bottom
paddle_x = PADDLE_START_X;
paddle_y = PADDLE_START_Y;

// set ball position and velocity
ball_x = 8 + rand() % (WINDOW_WIDTH-16);
ball_y = BALL_START_Y;
ball_dx = -4 + rand() % (8+1);
ball_dy = 6 + rand() % 2;

// transition to start level state
game_state = GAME_STATE_START_LEVEL;
}
else if (game_state == GAME_STATE_START_LEVEL)
{
// get a new level ready to run

// initialize the blocks
Init_Blocks();

// reset block counter
blocks_hit = 0;

// transition to run state
game_state = GAME_STATE_RUN;
}
else if (game_state == GAME_STATE_RUN)
{
// start the timing clock
Start_Clock();

// clear drawing surface for next frame of animation
Draw_Rectangle(0, 0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 0);

// move the paddle
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, BALL_COLOR);


// draw the info
wsprin