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
wsprintf(buffer, TEXT("FREAKOUT Score %d Level %d"), score, level);
DrawText_GUI(buffer, 8, WINDOW_HEIGHT-26, 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;
}
/* CLOCK FUNCTIONS ************************************************************************/
DWORD Get_Clock(void)
{
// this function returns the current tick count
// return time
return GetTickCount();
}
DWORD Start_Clock(void)
{
// this function starts the block, that is, saves the current count,
// use in conjunction with Wait_Clock()
return (start_clock_count = Get_Clock());
}
DWORD Wait_Clock(DWORD count)
{
// this function is used to wait for a specific number of clicks since
// the call to Start_Clock
while (Get_Clock() - start_clock_count < count)
;
return Get_Clock();
}