版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、c語言打磚塊游戲15 / 15、游戲截圖FREAKOUTScore -175Level 1二、游戲源碼iiincludewindows hSincludeSincludeJJinclude/* DEFINES* * * * * * * * * *冬冬* * 左冬* * *冬冬 * *2?:* * */ / defines for windows斗define WIND0W_CLASS_NA5E TEXTWI32CLASS)define WINDOW.WIDTH640define WINDOWHEIGHT480/ states for game loop斗define GAME_STATE_IN
2、IT0Sdefine GAME_STATE_START_LEVEL 1define GAME.STATE.RUN2define GAME.STATE.SHUTDOWX3define GAME_STATE_EXIT4/ block definesdefine NUM.BLOCK.ROWSdefine NUM.BLOCK.COLUMNS68Sdefine BLOCK.WIDTH64Sdefine BLOCK.HEIGHT16define BLOCK_ORIGIN_X8define BLOCK_ORIGIN_Y8Sdefine BLOCK_X_GAP80斗define BLOCK_Y_GAP32/
3、paddle definesiidefine PADDLE.START.Xdefine PADDLE_START_Ydefine PADDLE.WIDTHSdefine PADDLEHEIGHTdefine PADDLE.COLOR/ ball definesdefine BALL_START_Ydefine BALL.SIZE/ color defines:?define BACKGROUND.COLORSdefine BLOCK_COLOR斗define BALL_COLOR(WINDOW.WIDTH/2 - 16)(WINDOW.HEIGHT - 32);32SRGB(O, 0, 255
4、)(WIND0W_HEIGHT/2)4RGB (0, 0, 0)RGB(125, 0, 0)RGB(222, 0, 222)/ these read the keyboard asynchronouslyiJdefine KEY_DOWN(vk_code) (GetAsyncKeyState (vk_code) & 0x8000)? 1 : 0) iJdefine KEY_UP (vk_code) (GetAsyncKeyState (vk_code) & OxSOOO) ? 0 : 1)* basic unsigned types*冬*冬*冬*冬*冬*冬*/typedefunsignedsh
5、ortUSHORT;typedefunsignedshortWORD;typedefunsignedcharUCHAR;typedefunsignedcharBYTE;/* FUNCTION DECLARATION*冬*冬冬*冬*冬*/int Game_Init(void *parms = NULL): int Game_Shutdown(void *parms = NULL); int Game_Main(void *parms = NULL);DWORD Start_Clock(void);DWORD Wait.Clock(DWORD count);* GLOBALS*冬*冬冬*,/HWN
6、Dmain_window_handle=NULL;=NULL;=GAME./ save the window handle/ save the instanceHINSTANCEintmain_instance game_state_STATE_IXIT;/starting stateintpaddle_x = 0,paddle_y =0;/tracks position of paddleintball.x = 0,ball.y =0;/tracks position of ballintball.dx = 0,ball.dy =0;/velocity of ballintscore= 0;
7、/thescoreintlevel= 1;/thecurrent levelintblocks_hit = 0;/tracks number of blocks hit/ used for timingDWORDstar t_c1ock_c ount = 0;/ this contains the game grid data UCHAR blocksNUM_BLOCK_ROWSNUH_BLOCK_COLUMXS;/* WINDPROC* * * * * * * * *冬* *冬* * 冬冬字冬* * *:?:* * */LRESULT CALLBACK WindowProc(HWND hwn
8、d,UINT msg,WPARAM uparam,LPARAM lparam)/ this is the main message handler of the systemPAIXTSTRUCT ps;HDChdc;switch (msg)case WM_CREATE: return 0;case WM.PAINT: hdc = BeginPaint(hwnd, &ps);EndPaint(hwnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(O);return 0;default:break;return DefWindowProc(hwn
9、d, msg, %param, 1param);/* WINMAIN*冬*/ int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)WNDCLASS winclass;HWXD hwnd;MSGmsg;HDChdc;PAINTSTRUCT ps;* CS_DBLCLKS Specifies that the window should be notified of double clicks with* WNLxBUTTONDBLCLK messages* CS.O
10、WNDC標(biāo)志,屈丁此窗口類的窗口實例都有門己的DC(稱為私有DC),私有DC僅屈丁該窗 口實例,*所以程序只需要調(diào)用一次GetDC或BeginPaint獲取DC,系統(tǒng)就為窗口初始化一個DC,井且保 存程序*對其進(jìn)彳亍的改變。ReleaseDC和EndPaint函數(shù)不再需要了,因為其他程序無法訪問和改變私 有DC。*/winclass stylewinclasslpfn甲ndProc=CS.DBLCLKS CS.OWNDC CS.HREDRAW CS_VREDRAW;=WindowProc;winclass cbClsExtra=o;winclass cbWndExtra=o;winclass
11、hlnstance=hinstance;winclass hlcon二 Loadicon(NULL, IDI.APPLICATION);winclass.hCursor= LoadCursor(NULL, IDCARROW):winclass. hbrBackground= (HBRUSH)GetStockObject(BLACK.BRUSH);winclasslpszHenuame = NULL;winclass. lpszClassName= WINDOW_CLASS_NANE;/ register the window classif (!RegisterClass(&winclass)
12、return 0;Create the window, note the use of WS POPUPhwnd = CreateWindow(WINDOW_CLASS_NAJE,TEXT(WIN32 Game Console WS.POPUP,200,100,WINDOW.WIDTH,WINDOWHEIGHT,NULL,NULL, hinstance, NULL);if (! hwnd)return 0;ShowWindow(hwnd, ncmdshow); Up da t eW i ndow(hwnd);/ hide mouse/ShowCursor(FALSE);/ class),/ t
13、itle/ style/ initial x/ initial y/ initial width/ initial height/ handle to parent/ handle to menu/ instance/ creation parms/ save the window handle and instance in a globalma i n_w i ndow_hand1e = hwnd; main_instance = hinstance;/ perform all game console specific initialization Game_Init();/ enter
14、 main event loopwhile (1)if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVT) / 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 Di spatchMessage(&msg);/ main game processing goes hereGame_Main();Sleep(
15、30);/ shutdown game and release all resources Game_Shutdown();/ show mouse/ShowCursor(TRUE);/ return to windows like this return (msg wParam);* DRAW FUNCTION*冬*冬*冬*冬*/ int Draw_Rectangle(int xl, int yl, int x2, int y2, int color)/ this function uses Win32 API to draw a filled rectangleHBRUSHhbrush;H
16、DChdc;RECT rect;SetRect(&rect, xl, yl, x2, y2):hbrush = CreateSoli dBrush(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)
17、;/ set the colors for the text upSetTextColor(hdc, color);/ set background mode to transparent so black isnt copiedSetBkModeChdc, TRANSPARENT);/ draw the textTextOut(hdc, x, y, text, lstrlen(text):/ release the deReleaseDC(main_window_handle, hdc);return 1;/* GAME PROGRAMMING CONSOLE FUNCTIONS*冬*voi
18、d Init_Blocks(void)/ initialize the block fieldfor (int row = 0; row NUM_BL0CK_R0WS; row+)for (int col = 0; col NUM.BLOCK.COLUMNS; col+) blocksrowcol = 1;void Draw_Blocks(void)/ this function draws all the blocks in row major form int xl = BLOCK.ORIGIN.X;int yl = BLOCKORIGIN.Y;/ draw all the blocksf
19、or (int row = 0; row NUM_BLOCK_ROWS; roh+)/ reset column positionxl = BLOCKORIGPLX;for (int col = 0; col (WINDOW.HEIGHT/2) & ball.dy 0)/ extract leading edge of ballint x = ball.x + (BALL.SIZE/2);int y = ball_y + (BALL.SIZE/2);/ test for collision with paddleif (x = paddle.x & x = paddle& y = paddle
20、.y 十 PADDLE.HEIGHT)ball_dy = ball_dy;reflect ballball_y *= ball_dy;push ball out of paddle since it made/ add a little english to ball based on motion of paddleif (KEY_DOWN(VK_RIGHT)ball_dx -二 randO % 3;else if (KEY_DOWN(VK_LEFT)ball_dx += randO % 3;elseball_dx +二(-1 + rand () % 3);/ make a little n
21、oiseMessageBeep(MB_OK);return 0;/ now scan thru all the blocks and see of ball hit blocksfor (int row = 0; row NUM.BLOCK.ROWS; row+)xl = BLOCK.ORIGIN.X;/ scan this row of blocksfor (int col = 0; col xl) & (ball.cx yl) & (ball_cy = (NUM.BLOCK.ROWS * NUM.BLOCK.COLUMNS)gamestate 二 GAME_STATE_START_LEVE
22、L; level+;/ add some pointsscore += 5 * (level + (abs)(ball_dx):return 1;xl +二 BLOCK_X_GAP;advance column positionyl += BLOCK_Y_GAP;advance row positionreturn 0;int Game_Init(void *parms)/ this function is where you do all the initialization for your game return 1;int Game_Shutdown(void *parms)/ thi
23、s function is where you shutdown your game and release all resources/ that you allocatedreturn 1;int Game_Main(void *parms)/ this is the workhorse of your game it will be called continuously in real-time / this is like mainO in C all the calls for you game go here!TCHARbuffer80;BOOL bPaddleMoved = F
24、ALSE;intoldpaddlex, old_paddle_y;intold_ball_x, old_ball_y;/ what state is the game in?if (game.state = GA5E_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 bottompaddle.x = PADDLE.START.X;paddle_y =
25、 PADDLE.START.Y;/ set ball position and velocityball.x =8 + randO % (WINDOW.WIDTH-16);ball.y = BALL_START_Y;ball_dx = 4 + randO % (81);ball_dy =6 + rand() % 2;/ transition to start level stategame.state = GAME_STATE_START_LEVEL;else if (game.state = GAME_STATE_START_LEYEL)/ get a new level ready to
26、runInit_Blocks();/ initialize the blocksblocks_hit = 0;reset block counterDraw_Blocks();draw blocks/ draw the paddleDraw.Rectangle(paddle.x, paddle_y, paddle.x+PADDLE.WIDTH, paddle.y+PADDLEHEIGHT, PADDLE.COLOR);game_state = GAME_STATE_RUN; transition to run stateelse if (game.state = GAME_STATE_RUN)
27、/ move the paddleif (KEY_DOWN(VK_RIGHT)old_paddle_x = paddle_x;old_paddle_y = paddle_y;paddle_x *= 8:move paddle to right/ make sure paddle doesnt go off screenif (paddle.x (WINDOW_WIDTH - PADDLE.WIDTH) paddle.x = WINDOW.WIDTH - PADDLEJHDTH;bPaddleMoved = TRUE;else if (KEY_DOWN(VK_LEFT)old_paddle_x
28、= paddle_x;old_paddle_y = paddle_y;paddle_x 一二 8:move paddle to left/ make sure paddle doesnt go off screenif (paddle_x (WIXDOW.WIDTH - BALL.SIZE) ball.x 0)ball_dx = ball_dx;reflect xaxis velocityball_x += ball_dx;update position/ now y-axisif (ball_y (WINDOW.HEIGHT - BALL.SIZE)ball_dy = ball_dy;ref
29、lect yaxis velocityball_y += ball_dy;update positionscore = 100;minus the score/ next watch out for ball velocity getting out of handif (ball.dx 8)ball.dx = 8;else if (ball_dx 8)ball.dx = -8;/ test if ball hit any blocks or the paddleif (Process_Ball0)Draw_Blocks 0;draw blocksif (bPaddleMoved)/覆蓋舊的p
30、addleDraw_Rectangle (old_paddle_xt oldpaddley, old_paddle_x+PADDLEIDTH,old_paddle_y+PADDLE_HEIGHT,BACKGROUND.COLOR);/ draw the paddleDraw_Rectang1e(paddle_x, paddle_y, paddle_x+PADDLE_WIDTH, paddle_y+PADDLE_HEIGHT, PADDLE.COLOR);/覆蓋舊的ballDraw_Rec tangle (old_ball_x, old_ball_y, o 1 d_b a 1 l_x+B ALL_S I ZE,old.ball_y+BALL_SIZE, BACKGROUND.COLOR);/ draw the ballDraw.Rectangle(ball.x, ball_yf ball_x+BALL_SIZE, ball_yBALL_SIZE, BALL_COLOR);/ draw the infowsprintf(buffer, TE
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 校園教學(xué)樓智慧校園系統(tǒng)安裝合同
- 太陽能項目合同發(fā)電效率
- 劇院租賃合同模板
- 保安設(shè)備融資租賃合同示范
- 醫(yī)療設(shè)備安裝工程總承包合同
- 農(nóng)業(yè)設(shè)施保溫施工合同
- 紡織服裝展位租賃協(xié)議
- 珠寶首飾存儲續(xù)約合同
- 生態(tài)工業(yè)園房產(chǎn)購置合同模板
- 真石漆施工合同私人會所外墻翻新
- GB/Z 44047-2024漂浮式海上風(fēng)力發(fā)電機(jī)組設(shè)計要求
- 2024版統(tǒng)編版一年級道德與法治上冊《2 我向國旗敬個禮》教學(xué)課件
- 國開(內(nèi)蒙古)2024年《漢語中的中國文化》形成性考核1-3終結(jié)性考核答案
- 司法臨床司法鑒定培訓(xùn)
- 第47屆世界技能大賽江蘇省選拔賽計算機(jī)軟件測試項目樣題
- 勞務(wù)合同保證金合同模板
- 小學(xué)足球課課件
- 國家職業(yè)技術(shù)技能標(biāo)準(zhǔn) 4-07-05-04 消防設(shè)施操作員 人社廳發(fā)201963號
- 七年級上冊語文第三單元知識速記清單(統(tǒng)編版2024)
- 2023-2024學(xué)年全國初中七年級下地理人教版期末考試試卷(含答案解析)
-
評論
0/150
提交評論