《c语言打砖块游戏》由会员分享,可在线阅读,更多相关《c语言打砖块游戏(15页珍藏版)》请在金锄头文库上搜索。
1、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_
2、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)#d
3、efine 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(
4、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 UC
5、HAR;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_ins
6、tance= 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_hi
7、t = 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 systemPAINTSTRUC
8、Tps;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
9、, 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,并且保
10、存程序 * 对其进行的改变。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=
11、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
12、_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