资源预览内容
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
贪吃蛇代码(C语言) /2021.06.09 /zook0k/zook0k/:83310093#include windows.h#include stdio.h#includetime.h#define C_W 516#define C_H 548/#define C_W 1024/#define C_H 1024#define GO_RIGHT 0x01#define GO_DOWN 0x02#define GO_LEFT 0x03#define GO_UP 0x04#define SNAKE_NUMBER 30typedef struct node_structunsigned char direction;unsigned char cnt;s_node,*s_node_handle;s_node s_countSNAKE_NUMBER ;typedef struct SNAKEunsigned char Head_X;unsigned char Head_Y;unsigned char Tail_X;unsigned char Tail_Y;unsigned char h_index;unsigned char t_index;unsigned char food_state;unsigned char score;unsigned char snake_state; Snake_Data,*Snake_Data_handle;Snake_Data snk_1;#define MAP_X 64#define MAP_Y 64unsigned char game_mapMAP_YMAP_X;LRESULT CALLBACK Win_tetris_Proc(HWND hwnd, / handle to windowUINT uMsg, / message identifierWPARAM wParam, / first message parameterLPARAM lParam / second message parameter);int WINAPI WinMain(HINSTANCE hInstance, / handle to current instanceHINSTANCE hPrevInstance, / handle to previous instanceLPSTR lpCmdLine, / command lineint nCmdShow / show state)snk_1.Head_X = 0x01;/head xsnk_1.Head_Y = 0x00;/head ysnk_1.Tail_X = 0x00;/tail xsnk_1.Tail_Y = 0x00;/tail ysnk_1.h_index=0;snk_1.t_index=0;snk_1.food_state=0;snk_1.score=0;snk_1.snake_state = 1;s_countsnk_1.h_index.cnt=2;s_countsnk_1.h_index.direction=GO_RIGHT;s_countsnk_1.t_index.cnt=2;s_countsnk_1.t_index.direction=GO_RIGHT;WNDCLASS wndcls;wndcls.cbClsExtra=0;wndcls.cbWndExtra=0;wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);wndcls.hInstance=hInstance;wndcls.lpfnWndProc=Win_tetris_Proc;wndcls.lpszClassName=Game_tetris;wndcls.lpszMenuName=NULL;wndcls.CS_HREDRAW | CS_VREDRAW;RegisterClass(wndcls);/Game_TetrisHWND hwnd;hwnd=CreateWindow(Game_tetris,Game_Snake(/zook0k/),WS_OVERLAPPEDWINDOW WS_MAXIMIZEBOX WS_MINIMIZEBOX WS_THICKFRAME,0,0,C_W,C_H,NULL,NULL,hInstance,NULL);ShowWindow(hwnd,SW_SHOWNORMAL);/initial snakeHDC hdc;hdc=GetDC(hwnd);HBRUSH hbr;RECT rect;rect.left = 0;rect.top = 0;rect.right = 16;rect.bottom = 8;hbr= CreateSolidBrush(RGB(255,0,0);FillRect(hdc,rect,hbr);ReleaseDC(hwnd,hdc);game_map00=1;game_map01=1;/initial randSetTimer(hwnd,1,100,NULL) ;srand(int)time(0);UpdateWindow(hwnd);MSG msg;while(GetMessage(msg,NULL,0,0)/TranslateMessage (msg); DispatchMessage(msg);return 0;LRESULT CALLBACK Win_tetris_Proc(HWND hwnd, / handle to windowUINT uMsg, / message identifierWPARAM wParam, / first message parameterLPARAM lParam / second message parameter)char szChar20 = score:;unsigned char xx,yy;switch(uMsg)case WM_KEYDOWN:if(32 = wParam)if(1 = snk_1.snake_state)snk_1.snake_state = 0;elsesnk_1.snake_state = 1;if(1 = snk_1.snake_state)if(wParam 36)(wParam 41)if(38 = wParam)if(s_countsnk_1.h_index.direction = GO_RIGHT)|(s_countsnk_1.h_index.direction = GO_LEFT)snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_countsnk_1.h_index.direction = GO_UP;s_countsnk_1.h_index.cnt = 1;else if(40 = wParam)if(s_countsnk_1.h_index.direction = GO_RIGHT)|(s_countsnk_1.h_index.direction = GO_LEFT)snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_countsnk_1.h_index.direction = GO_DOWN;s_countsnk_1.h_index.cnt = 1;else if(39 = wParam)if(s_countsnk_1.h_index.direction = GO_DOWN)|(s_countsnk_1.h_index.direction = GO_UP)snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_countsnk_1.h_index.direction = GO_RIGHT;s_countsnk_1.h_index.cnt = 1;else if(37 = wParam)if(s_countsnk_1.h_index.direction = GO_DOWN)|(s_countsnk_1.h_index.direction = GO_UP)snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_countsnk_1.h_index.direction = GO_LEFT;s_countsnk_1.h_index.cnt = 1;break;case WM_TIMER:/case WM_PAINT:time_t t;HDC hdc;hdc=GetDC(hwnd);HBRUSH hbr;RECT rect;if(1 = snk_1.snake_state)/headCHECK:switch(s_countsnk_1.h_index.direction)case GO_RIGHT:if(snk_1.Head_X 63)snk_1.Head_X+;else snk_1.Head_X = 0;break;case GO_LEFT:if(snk_1.Head_X 0 )snk_1.Head_X-;else snk_1.Head_X = 63;break;case GO_DOWN :if(snk_1.Head_Y 63)snk_1.Head_Y+;else snk_1.Head_Y = 0; break;case GO_UP:if(snk_1.Head_Y 0)snk_1.Head_Y-;else snk_1.Head_Y = 63;break;default:break;s_countsnk_1.h_index.cnt+;if(0 = game_mapsnk_1.Head_Ysnk_1.Head_X)/no pointgame_map
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号