1、贪吃蛇简易代码#include#include resource1.h#include #include #include /蛇的结构体typedef struct SNAKE int x; int y; struct SNAKE *pNext;Snake;/豆的结构体typedef struct BEAN int x; int y; struct BEAN *pNext;Bean;/函数原型void CreateSnake(Snake *pSnake);void deleteAll(Snake *pSnake);void ShowSnake(Snake *pSnake);void Creat
2、eBean(Bean *pBean,Snake *pSnake);void ShowBean(Bean *pBean);void DeleteBean(Bean *pBean);void SnakeRun(Snake *pSnake);void SnakeGrowUp(Snake *pSnake);int CanEatBean(Snake *pSnake,Bean *pBean);int CanDie(Snake *pSnake);/char g_ClassName20 = ClassName;LRESULT CALLBACK WndProc(HWND hwnd,UINT nMsg,WPARA
3、M wParam,LPARAM lParam);HBITMAP hBitMap;int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR pCmdLine, int nCmdShow) HBRUSH hBrush = CreateSolidBrush(RGB(100,100,410); HICON hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_wnd_icon); HICON hIconsm = LoadIcon(hInstance,MAKEINTRESOURC
4、E(IDI_wnd_iconsm); hBitMap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_wdnbitmap); /设计类 WNDCLASSEX wndClass; wndClass.cbClsExtra = NULL; wndClass.cbSize = sizeof(wndClass); wndClass.cbWndExtra = NULL; wndClass.hbrBackground = hBrush; wndClass.hCursor = LoadCursor(hInstance,IDC_ARROW); wndClass.hIcon
5、= hIcon; wndClass.hIconSm = hIconsm; wndClass.hInstance = hInstance; wndClass.lpfnWndProc = WndProc; wndClass.lpszClassName = g_ClassName; wndClass.style = CS_HREDRAW|CS_VREDRAW; wndClass.lpszMenuName = NULL; /注册 if (!RegisterClassEx(&wndClass) MessageBox(NULL,注册失败,消息,MB_OK); /创建 HWND hwnd = CreateW
6、indow(g_ClassName, 贪吃蛇, WS_OVERLAPPEDWINDOW, 200, 50, 600+16, 600+38, NULL, NULL, hInstance, NULL); if (!hwnd) MessageBox(NULL,创建失败!,消息,MB_OK); /显示 ShowWindow(hwnd,nCmdShow); /消息循环 MSG msg; while (GetMessage(&msg,NULL,0,0) TranslateMessage(&msg); /翻译 DispatchMessage(&msg); /回调函数 return 0;HDC dc = NU
7、LL;Bean *pBean = NULL;Snake *pSnake = NULL;int VK = VK_RIGHT;LRESULT CALLBACK WndProc(HWND hwnd,UINT nMsg,WPARAM wParam,LPARAM lParam) switch (nMsg) case WM_CREATE: CreateSnake(&pSnake); /创建蛇 CreateBean(&pBean,pSnake); dc = GetDC(hwnd); break; case WM_KEYDOWN: switch (wParam) case VK_RETURN: /按下后就开始
8、 SetTimer(hwnd,1,150,NULL); break; case VK_UP: VK = VK_UP; break; case VK_DOWN: VK = VK_DOWN; break; case VK_LEFT: VK = VK_LEFT; break; case VK_RIGHT: VK = VK_RIGHT; break; case : KillTimer(hwnd,1); break; break; case WM_TIMER: /*创建背景*/ RECT rect; GetWindowRect(hwnd,&rect); HDC hmeDc = CreateCompati
9、bleDC(dc); SelectObject(hmeDc,hBitMap); StretchBlt(dc,0,0,rect.right-rect.left,rect.bottom-rect.top,hmeDc,0,0,500,600,SRCCOPY); DeleteDC(hmeDc); /*背景创建结束*/ /各种函数 if (CanDie(pSnake) KillTimer(hwnd,1); MessageBox(NULL,笨蛋,撞到自己身上,我死啦!,消息,MB_OK); if ( CanEatBean(pSnake,pBean) ) SnakeGrowUp(pSnake); Delet
10、eBean(&pBean); CreateBean(&pBean,pSnake); SnakeRun(&pSnake); else SnakeRun(&pSnake); ShowSnake(pSnake); ShowBean(pBean); case WM_PAINT: /*创建背景*/ RECT rect; GetWindowRect(hwnd,&rect); HDC hmeDc = CreateCompatibleDC(dc); SelectObject(hmeDc,hBitMap); StretchBlt(dc,0,0,rect.right-rect.left,rect.bottom-r
11、ect.top,hmeDc,0,0,500,600,SRCCOPY); /*背景创建结束*/ ShowSnake(pSnake); /显示蛇 ShowBean(pBean); /显示豆 DeleteDC(hmeDc); break; case WM_CLOSE: deleteAll(&pSnake); DeleteBean(&pBean); DestroyWindow(hwnd); break; case WM_DESTROY: ReleaseDC(hwnd,dc); PostQuitMessage(0); break; case WM_QUIT: break; return DefWindo
12、wProc(hwnd,nMsg,wParam,lParam);/函数/创建蛇Create Snake()void CreateSnake(Snake *pSnake) Snake *u = NULL, *w = NULL; int i = 3; /开始的蛇有三截 int x = 0; /蛇头的初始位置 int y = 0; /蛇尾的初始位置 while (i-) u = (Snake *)malloc(sizeof(Snake); u-pNext = NULL; u-x = x; u-y = y; x +=20; if (NULL = *pSnake) *pSnake = u; else w-
13、pNext = u; w = u; /*蛇的三届创建完毕*/创建豆void CreateBean(Bean *pBean,Snake *pSnake) Snake *ji; int x = 0; /随机的位置x坐标 int y = 0; /随机的位置y坐标 srand(time(NULL); x = (unsigned)rand()%29)*20; y = (unsigned)rand()%29)*20; ji = pSnake; while(pSnake) if (x = pSnake-x) & (y = pSnake-y) ) x = (unsigned)rand()%29)*20; y
14、= (unsigned)rand()%29)*20; pSnake = ji; pSnake = pSnake-pNext; Bean *u; u = (Bean *)malloc(sizeof(Bean); u-pNext = NULL; u-x = x; u-y = y; *pBean = u;/删掉全部蛇void deleteAll(Snake *pSnake) Snake *del; while(*pSnake) del = *pSnake; (*pSnake) = (*pSnake)-pNext; free(del); /删掉豆void DeleteBean(Bean *pBean)
15、 free(*pBean); *pBean = NULL;/显示蛇void ShowSnake(Snake *pSnake) while (pSnake) Rectangle(dc,pSnake-x,pSnake-y,pSnake-x+20,pSnake-y+20); pSnake = pSnake-pNext; /显示豆void ShowBean(Bean *pBean) Rectangle(dc,pBean-x,pBean-y,pBean-x+20,pBean-y+20);/贪吃蛇移动void SnakeRun(Snake *pSnake) Snake *ji, /记录变化的蛇头 *rou
16、nd; /循环遍历 int x, /蛇头的当前位置x坐标 y; /蛇头的当前位置y坐标 ji = *pSnake; round = *pSnake; while (round-pNext) round = round-pNext; x = round-x; y = round-y; switch(VK) case VK_UP: if (y = 0) y = 600; y -=20; break; case VK_DOWN: if (y=600) y = 0; y +=20; break; case VK_LEFT: if (x = 0) x = 600; x -= 20; break; cas
17、e VK_RIGHT: if (x = 600) x = 0; x +=20; break; *pSnake = (*pSnake)-pNext; ji-x = x; ji-y = y; ji-pNext = NULL; round-pNext = ji;/下一步能吃到豆吗? 返回1吃到int CanEatBean(Snake *pSnake,Bean *pBean) while (pSnake-pNext) pSnake = pSnake-pNext; if (pSnake-x=pBean-x)&(pSnake-y=pBean-y) return 1; return 0;/长大void Sn
18、akeGrowUp(Snake *pSnake) Snake *u; u = (Snake *)malloc(sizeof(Snake); u-pNext = NULL; u-x = pBean-x; u-y = pBean-y; while(pSnake-pNext) pSnake = pSnake-pNext; pSnake-pNext = u;/蛇会死吗 返回1会死int CanDie(Snake *pSnake) Snake *ji = NULL; int x = 0, y = 0; ji = pSnake; while (pSnake-pNext) pSnake = pSnake-pNext; x = pSnake-x; y = pSnake-y; switch(VK) case VK_UP: y -=20; break; case VK_DOWN: y +=20; break; case VK_LEFT: x -= 20; break; case VK_RIGHT: x +=20; break; while(ji-pNext) if (x = ji-x) & (y = ji-y) return 1; ji = ji-pNext; return 0;(范文素材和资料部分来自网络,供参考。可复制、编制,期待你的好评与关注)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1