Win32 SDK 打砖块游戏(二)

2014-11-24 09:00:43 · 作者: · 浏览: 14
uName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;

// register the window class
if (!RegisterClass(&winclass))
return 0;

// Create the window, note the use of WS_POPUP
hwnd = CreateWindow(
WINDOW_CLASS_NAME, // class
TEXT("WIN32 Game Console"), // title
WS_OVERLAPPEDWINDOW, // style
0, 0, // initial x, y
WINDOW_WIDTH, // initial width
WINDOW_HEIGHT, // initial height
NULL, // handle to parent
NULL, // handle to menu
hinstance, // instance
NULL); // creation parms

if (! hwnd)
return 0;

ShowWindow(hwnd, ncmdshow);
UpdateWindow(hwnd);

// hide mouse
//ShowCursor(FALSE);

// save the window handle and instance in a global
main_window_handle = hwnd;
main_instance = hinstance;

// perform all game console specific initialization
//Game_Init();

// enter main event loop
while (1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// test if this is a quit msg
if (msg.message == WM_QUIT)
break;

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);
}

// main game processing goes here
//Game_Main();
}

// shutdown game and release all resources
//Game_Shutdown();

// show mouse
//ShowCursor(TRUE);

// return to windows like this
return (msg.wParam);
}


2. 游戏控制流程

[cpp]
int Game_Init(void *parms)
{
// this function is where you do all the initialization for your game

return 1;
}

int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and release 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 paddl