ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:16.99KB ,
资源ID:9557705      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9557705.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(贪吃蛇简易代码.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

贪吃蛇简易代码.docx

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