贪吃蛇简易代码

上传人:小** 文档编号:62075662 上传时间:2018-12-17 格式:DOCX 页数:10 大小:17.46KB
返回 下载 相关 举报
贪吃蛇简易代码_第1页
第1页 / 共10页
贪吃蛇简易代码_第2页
第2页 / 共10页
贪吃蛇简易代码_第3页
第3页 / 共10页
贪吃蛇简易代码_第4页
第4页 / 共10页
贪吃蛇简易代码_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《贪吃蛇简易代码》由会员分享,可在线阅读,更多相关《贪吃蛇简易代码(10页珍藏版)》请在金锄头文库上搜索。

1、#include#include resource1.h#include #include #include /蛇的结构体typedef struct SNAKEint x;int y;struct SNAKE *pNext;Snake;/豆的结构体typedef struct BEANint x;int y;struct BEAN *pNext;Bean;/函数原型void CreateSnake(Snake *pSnake);void deleteAll(Snake *pSnake);void ShowSnake(Snake *pSnake);void CreateBean(Bean *p

2、Bean,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,WPARAM wParam,LPAR

3、AM 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,MAKEINTRESOURCE(IDI_wnd_iconsm

4、);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 = hIcon;wndClass.hIconSm

5、= 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 = CreateWindow(g_ClassName,贪吃蛇,WS_OVERLAPPEDW

6、INDOW,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 = NULL;Bean *pBean = NULL;Snake *pSnake = NULL;int VK = VK_RIG

7、HT;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:/按下后就开始SetTimer(hwnd,1,150,NULL);break;case VK_UP:VK = VK_UP;break;case VK_D

8、OWN: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 = CreateCompatibleDC(dc);SelectObject(hmeDc,hBitMap);StretchBlt(dc,0,0,rect.right-rect.left,rect.bottom-rec

9、t.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);DeleteBean(&pBean);CreateBean(&pBean,pSnake);SnakeRun(&pSnake);elseSnakeRun(&pSnake);ShowSnake(pSnake);ShowBe

10、an(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-rect.top,hmeDc,0,0,500,600,SRCCOPY);/*背景创建结束*/ShowSnake(pSnake);/显示蛇ShowBean(pBean);/显示豆DeleteDC(hmeDc);break;case WM_

11、CLOSE:deleteAll(&pSnake);DeleteBean(&pBean);DestroyWindow(hwnd);break;case WM_DESTROY:ReleaseDC(hwnd,dc);PostQuitMessage(0);break;case WM_QUIT:break;return DefWindowProc(hwnd,nMsg,wParam,lParam);/函数/创建蛇Create Snake()void CreateSnake(Snake *pSnake)Snake *u = NULL,*w = NULL;int i = 3;/开始的蛇有三截int x = 0

12、;/蛇头的初始位置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;elsew-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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号