#include #define ID_TIMER 999 #define GAME_INIT 0 //#define GAME_IDLE 1 #define GAME_START 1 #define GAME_RUN 2 #define GAME_SHUTDOWN 3 #define GAME_EXIT 4 int game_state = GAME_INIT; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void DrawBlocks(HWND hwnd, int num_rows, int num_cols, int block_x, int block_y); void DrawBall(HWND); void HitMan(HWND hwnd, int NUM_ROWS, int NUM_COLS); void DrawPaddle(HWND); void GameText(HWND); int Game_Main(HWND); int TOTAL_WIDTH = GetSystemMetrics(SM_CXFULLSCREEN); int TOTAL_HEIGHT = GetSystemMetrics(SM_CYFULLSCREEN); bool keys[256]; const int WIDTH = 600; const int HEIGHT = 500; /*int ball_left = 100; int ball_top = 400; int ball_right = 120; int ball_bottom = 420;*/ int ball_left = 0; int ball_top = 0; int ball_right = 0; int ball_bottom = 0; int cxClient, cyClient; int ball_movex= 1; int ball_movey= 1; int xPixel, yPixel; int paddle_left=0; int paddle_top = 0; int paddle_right = 0; int paddle_bottom = 0; struct tagBlock { int left_pt; int top_pt; int right_pt; int bottom_pt; bool life; } Block[5][5]; void Init_Block(int NUM_ROWS, int NUM_COLS, int x1, int y1) { int i,j; for(j =0; j < NUM_ROWS; j++) { for(i=0; i< NUM_COLS; i++) { Block[j][i].left_pt = x1; Block[j][i].top_pt = y1; Block[j][i].right_pt = x1 + 70; Block[j][i].bottom_pt = y1 + 20; Block[j][i].life = true; x1 +=90; } x1 = 40;//start back at the left y1 += 40;//start new column } } //================================================== ======================================== int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { TCHAR szAppName[]= TEXT("IconDemo"); HWND hwnd; MSG msg; WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = szAppName; if(!RegisterClass(&wc)) { MessageBox(NULL, TEXT("Register Class error!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("icondemo"), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, TOTAL_WIDTH /5, TOTAL_HEIGHT / 5, WIDTH , HEIGHT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while(1) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.wParam == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { Game_Main(hwnd); } } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { // HDC hdc; // PAINTSTRUCT ps; switch (message) { case WM_CREATE: SetTimer(hwnd, ID_TIMER, 100, NULL); ball_left = 100; ball_top = 400; ball_right = 110; ball_bottom = 410; paddle_left = 100; paddle_top = 430; paddle_right = 170; paddle_bottom = 450; // Init_Block(5, 5, 40, 60); // game_state =GAME_INIT; return 0; case WM_TIMER: InvalidateRect(hwnd, NULL ,TRUE); return 0; case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); return 0; case WM_KEYDOWN: keys[wParam] = TRUE; return 0; case WM_KEYUP: keys[wParam] = FALSE; return 0; /* case WM_PAINT: hdc =BeginPaint(hwnd, &ps); GameText(hwnd); DrawBall(hwnd); DrawBlocks(hwnd,5, 5, 40, 60); // DrawPaddle(hwnd); HitMan(hwnd, 5, 5); EndPaint(hwnd, &ps); return 0;*/ case WM_DESTROY: KillTimer(hwnd, ID_TIMER); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } void DrawBlocks(HWND hwnd,int num_rows, int num_cols, int block_x, int block_y) { HDC hdc; HBRUSH hBrush,old_brush; int i,j; int space =10;//horizontal space between each block hdc = GetDC(hwnd); hBrush = CreateSolidBrush(RGB(0,0,255)); old_brush = (HBRUSH) SelectObject(hdc, hBrush); /* //initialize the blocks position for(j =0; j< num_rows; j++) { for(i=0; i< num_cols; i++) { Block[j][i].left_pt = block_x; Block[j][i].top_pt = block_y; Block[j][i].right_pt = block_x+70; Block[j][i].bottom_pt = block_y +20; // Rectangle(hdc, block_x, block_y, block_x+70, block_y+20); block_x+=90;//add space between the blocks } block_x = 40;//start back at the left block_y += 40;//start new column } */ //draw the blocks for(j =0; j< num_rows; j++) { for(i=0; i< num_cols; i++) { //if the block has not been hit draw it if(Block[j][i].life == true) { Rectangle(hdc, Block[j][i].left_pt, Block[j][i].top_pt, Block[j][i].right_pt, Block[j][i].bottom_pt); } else continue; } } DeleteObject(SelectObject(hdc, old_brush)); ReleaseDC(hwnd, hdc); } //process the ball void DrawBall(HWND hwnd) { HDC hdc; HBRUSH hBrush, old_brush; hdc = GetDC(hwnd); hBrush = CreateSolidBrush(RGB(255,255,0));//create a yellow brush for the ball old_brush = (HBRUSH)SelectObject(hdc, hBrush);//select brush in the device context //draws a small yellow ellipse to look like a ball Ellipse(hdc, ball_left, ball_top, ball_right, ball_bottom); //if the ball hits the bototm or top reverse its direction if((ball_bottom > cyClient) || (ball_top < 0)) ball_movey *= -1; //if the ball hits the left or right border reverse the directions if((ball_right > cxClient) || (ball_left < 0)) ball_movex *= -1; ball_left += ball_movex;//moves the ball ball_right += ball_movex; ball_top += ball_movey; ball_bottom += ball_movey; DeleteObject(SelectObject(hdc, old_brush));//deletes yellow brush ReleaseDC(hwnd, hdc);//release hdc // InvalidateRect(hwnd, NULL, TRUE); } void DrawPaddle(HWND hwnd) { HBRUSH paddle, old_brush; HDC hdc; hdc = GetDC(hwnd); paddle = CreateSolidBrush(RGB(255,0, 0));//create a red brush for the paddle old_brush = (HBRUSH)SelectObject(hdc, paddle);//select the red brush for coloring //draw the paddle and color it red Rectangle(hdc, paddle_left, paddle_top, paddle_right, paddle_bottom); //delete the brush and restore the default value DeleteObject(SelectObject(hdc, old_brush)); } /*decide if a block was hit or not X increases to the right----> Y increase values increases going down | | V left_pt and top_pt =============== | O | =============== block bottom_pt and right_pt */ void HitMan(HWND hwnd, int NUM_ROWS, int NUM_COLS) { int i,j; HDC hdc; hdc = GetDC(hwnd); //go through each block and decide if it active, if so decide if it has been struct for(j=0; j< NUM_ROWS; j++) { for(i=0; i< NUM_COLS; i++) {//check to see if the four corners of the ball are less then the four //corners of the ball. if so make the block life false and reverse the balls direction if((Block[j][i].life == true) && (ball_left > Block[j][i].left_pt) && (ball_right < Block[j][i].right_pt) && (ball_top > Block[j][i].top_pt) && (ball_bottom < Block[j][i].bottom_pt)) { Block[j][i].life = false; ball_movey *= -1; ball_movex *= -1; } }//End of INNER FOR loop }// end of OUTER FOR loop }//end of HITMAN function void GameText(HWND hwnd) { HDC hdc; HPEN hPen, old_pen; static TCHAR szGame[]= TEXT("BreakOut"); int size = strlen(szGame); hdc = GetDC(hwnd); hPen = CreatePen(PS_SOLID, 1, RGB(255,255,0)); old_pen = (HPEN) SelectObject(hdc, hPen); SetTextColor(hdc, RGB(0,0,0)); SetBkColor(hdc, RGB(255,255,255)); TextOut(hdc,20, 450, szGame, size); DeleteObject(SelectObject(hdc, old_pen)); ReleaseDC(hwnd, hdc); } //game loop int Game_Main(HWND hwnd) { if(game_state == GAME_INIT) { //GameText(hwnd); game_state = GAME_START; //return 1; } else if(game_state == GAME_START) { Init_Block(5,5,40,60); game_state = GAME_RUN; // return 1; } else if(game_state == GAME_RUN) { DrawPaddle(hwnd); DrawBlocks(hwnd, 5,5,40,60); DrawBall(hwnd); HitMan(hwnd, 5,5); if(keys[VK_ESCAPE]) { game_state = GAME_EXIT; } // return 1; } else if (game_state == GAME_EXIT) return 0; // else // return 0; }