




免费预览已结束,剩余10页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C语言打砖块游戏一、游戏截图2、 游戏源码#include #include #include #include /* DEFINES */ defines for windows#define WINDOW_CLASS_NAMETEXT(WIN32CLASS)#define WINDOW_WIDTH640#define WINDOW_HEIGHT480/ states for game loop#define GAME_STATE_INIT 0#define GAME_STATE_START_LEVEL 1#define GAME_STATE_RUN 2#define GAME_STATE_SHUTDOWN 3#define GAME_STATE_EXIT 4/ block defines#define NUM_BLOCK_ROWS 6#define NUM_BLOCK_COLUMNS 8#define BLOCK_WIDTH 64#define BLOCK_HEIGHT 16#define BLOCK_ORIGIN_X 8#define BLOCK_ORIGIN_Y 8#define BLOCK_X_GAP 80#define BLOCK_Y_GAP 32/ paddle defines#define PADDLE_START_X (WINDOW_WIDTH/2 - 16)#define PADDLE_START_Y (WINDOW_HEIGHT - 32);#define PADDLE_WIDTH 32#define PADDLE_HEIGHT 8#define PADDLE_COLOR RGB(0, 0, 255)/ ball defines#define BALL_START_Y (WINDOW_HEIGHT/2)#define BALL_SIZE 4/ color defines#define BACKGROUND_COLORRGB(0, 0, 0)#define BLOCK_COLORRGB(125, 0, 0)#define BALL_COLORRGB(222, 0, 222)/ these read the keyboard asynchronously#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)#define KEY_UP(vk_code) (GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)/* basic unsigned types */typedef unsigned short USHORT;typedef unsigned short WORD;typedef unsigned char UCHAR;typedef unsigned char BYTE;/* 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 */HWNDmain_window_handle= NULL;/ save the window handleHINSTANCEmain_instance= NULL;/ save the instanceintgame_state= GAME_STATE_INIT;/ 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;/ the scoreintlevel = 1;/ the current levelintblocks_hit = 0;/ tracks number of blocks hitDWORDstart_clock_count= 0;/ used for timing/ this contains the game grid dataUCHAR blocksNUM_BLOCK_ROWSNUM_BLOCK_COLUMNS; /* WINDPROC */LRESULT CALLBACK WindowProc(HWNDhwnd,UINTmsg,WPARAMwparam,LPARAMlparam)/ this is the main message handler of the systemPAINTSTRUCTps;HDChdc;switch (msg)case WM_CREATE:return 0;case WM_PAINT:hdc = BeginPaint(hwnd, &ps);EndPaint(hwnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;default:break;return DefWindowProc(hwnd, msg, wparam, lparam);/* WINMAIN */int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)WNDCLASSwinclass;HWNDhwnd;MSGmsg;HDChdc;PAINTSTRUCTps;/* CS_DBLCLKS Specifies that the window should be notified of double clicks with * WM_xBUTTONDBLCLK messages * CS_OWNDC标志,属于此窗口类的窗口实例都有自己的DC(称为私有DC),私有DC仅属于该窗口实例, * 所以程序只需要调用一次GetDC或BeginPaint获取DC,系统就为窗口初始化一个DC,并且保存程序 * 对其进行的改变。ReleaseDC和EndPaint函数不再需要了,因为其他程序无法访问和改变私有DC。 */winclass.style= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;winclass.lpfnWndProc= WindowProc;winclass.cbClsExtra= 0;winclass.cbWndExtra= 0;winclass.hInstance= hinstance;winclass.hIcon= LoadIcon(NULL, IDI_APPLICATION);winclass.hCursor= LoadCursor(NULL, IDC_ARROW);winclass.hbrBackground= (HBRUSH)GetStockObject(BLACK_BRUSH);winclass.lpszMenuName= NULL;winclass.lpszClassName= WINDOW_CLASS_NAME;/ register the window classif (!RegisterClass(&winclass)return 0;/ Create the window, note the use of WS_POPUPhwnd = CreateWindow(WINDOW_CLASS_NAME,/ classTEXT(WIN32 Game Console),/ titleWS_POPUP,/ style200,/ initial x100,/ initial yWINDOW_WIDTH,/ initial widthWINDOW_HEIGHT,/ initial heightNULL,/ handle to parentNULL,/ handle to menuhinstance,/ instanceNULL);/ creation parmsif (! hwnd)return 0;ShowWindow(hwnd, ncmdshow);UpdateWindow(hwnd);/ hide mouse/ShowCursor(FALSE);/ save the window handle and instance in a globalmain_window_handle= hwnd;main_instance= hinstance;/ perform all game console specific initializationGame_Init();/ enter main event loopwhile (1)if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)/ test if this is a quit msgif (msg.message = WM_QUIT)break;/ translate any accelerator keysTranslateMessage(&msg);/ send the message to the window procDispatchMessage(&msg);/ main game processing goes hereGame_Main();Sleep(30);/ shutdown game and release all resourcesGame_Shutdown();/ show mouse/ShowCursor(TRUE);/ return to windows like thisreturn (msg.wParam);/* DRAW FUNCTION */int Draw_Rectangle(int x1, int y1, int x2, int y2, int color)/ this function uses Win32 API to draw a filled rectangleHBRUSHhbrush;HDChdc;RECTrect;SetRect(&rect, x1, y1, x2, y2);hbrush = CreateSolidBrush(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)HDChdc;hdc = GetDC(main_window_handle);/ set the colors for the text upSetTextColor(hdc, color);/ set background mode to transparent so black isnt copiedSetBkMode(hdc, TRANSPARENT);/ draw the textTextOut(hdc, x, y, text, lstrlen(text);/ release the dcReleaseDC(main_window_handle, hdc);return 1;/* GAME PROGRAMMING CONSOLE FUNCTIONS */void Init_Blocks(void)/ initialize the block fieldfor (int row = 0; row NUM_BLOCK_ROWS; 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 formint x1 = BLOCK_ORIGIN_X;int y1 = BLOCK_ORIGIN_Y;/ draw all the blocksfor (int row = 0; row NUM_BLOCK_ROWS; row+)/ reset column positionx1 = BLOCK_ORIGIN_X;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 & y = paddle_y + PADDLE_HEIGHT)ball_dy = -ball_dy;/ reflect ballball_y += ball_dy;/ push ball out of paddle since it made contact/ add a little english to ball based on motion of paddleif (KEY_DOWN(VK_RIGHT)ball_dx -= rand() % 3;else if (KEY_DOWN(VK_LEFT)ball_dx += rand() % 3;elseball_dx += (-1 + rand() % 3);/ make a little noiseMessageBeep(MB_OK);return 0;/ now scan thru all the blocks and see of ball hit blocksfor (int row = 0; row NUM_BLOCK_ROWS; row+)x1 = BLOCK_ORIGIN_X;/ scan this row of blocksfor (int col = 0; col x1) & (ball_cx y1) & (ball_cy = (NUM_BLOCK_ROWS * NUM_BLOCK_COLUMNS)game_state = GAME_STATE_START_LEVEL;level+;/ add some pointsscore += 5 * (level + (abs)(ball_dx);return 1;x1 += BLOCK_X_GAP;/ advance column positiony1 += BLOCK_Y_GAP;/ advance row positionreturn 0;int Game_Init(void *parms)/ this function is where you do all the initialization for your gamereturn 1;int Game_Shutdown(void *parms)/ this 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 main() in C all the calls for you game go here!TCHARbuffer80;BOOLbPaddleMoved = FALSE;intold_paddle_x, old_paddle_y;intold_ball_x, old_ball_y;/ what state is the game in?if (game_state = GAME_STATE_INIT)/ seed the random number generator so game is different each playsrand(unsigned int)time(0);/ set the paddle position here to the middle bottompaddle_x = PADDLE_START_X;paddle_y = PADDLE_START_Y;/ set ball position and velocityball_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 stategame_state = GAME_STATE_START_LEVEL;else if (game_state = GAME_STATE_START_LEVEL)/ get a new level ready to 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+PADDLE_HEIGHT, PADDLE_COLOR);game_state = GAME_STATE_RUN;/ transition to run stateelse if (game_state = GAME_STATE_RUN)/ 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 - PADDLE_WIDTH;bPaddleMoved = TRUE;else if (KEY_DOWN(VK_LEFT)old_paddle_x = paddle_x;old_paddle_y = paddle_y;paddle_x -= 8;/ move paddle to left/ make sure paddle doesnt go off screenif (paddle_x (WINDOW_WIDTH - BALL_SIZE) | ball_x 0)ball_dx = -ball_dx;/ reflect x-axis velocityball_x += ball_dx;/ update position/ now y-axisif (ball_y (WINDOW_HEIGHT - BALL_SIZE)ball_dy = -ball_dy;/ reflect y-axis 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_Ball()Draw_Blocks();/ draw blocksif (bPaddleMoved)/ 覆盖旧的paddleDraw_Rectangle(old_paddle_x, old_paddle_y, old_paddle_x+PADDLE_WIDTH, old_paddle_y+PADDLE_HEIGHT, BACKGROUND_COLOR);/ draw the paddleDraw_Rectangle(paddle_x, paddle_y, paddle_x+PADDLE_WIDTH, paddle_y+PADDLE_HEIGHT, PADDLE_COLOR);/ 覆盖旧的ballDraw_Rectangle(old_ball_x, old_ball_y, old_ball_x+BALL_SIZE, old_ball_y+BALL_SIZE, BACKGROUND_COLOR);/ draw the ballDraw_Rectangle(ball_x, ball_y, ball_x+BALL_SIZE, ball_y+BALL_SIZE, BALL_COLOR);/ draw the infowsprintf(buffer, TEXT(F
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论