版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文档供参考,可复制、编制,期待您的好评与关注! #include<windows.h>#include "resource1.h"#include <stdio.h>#include <stdlib.h>#include <time.h>/蛇的结构体typedef struct SNAKEint x;int y;struct SNAKE *pNext;Snake;/豆的结构体typedef struct BEANint x;int y;struct BEAN *pNext;Bean;/函数原型void CreateSnake(
2、Snake *pSnake);void deleteAll(Snake *pSnake);void ShowSnake(Snake *pSnake);void CreateBean(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 *pSnak
3、e);/char g_ClassName20 = "ClassName"LRESULT CALLBACK WndProc(HWND hwnd,UINT nMsg,WPARAM 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 = LoadI
4、con(hInstance,MAKEINTRESOURCE(IDI_wnd_icon);HICON hIconsm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_wnd_iconsm);hBitMap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_wdnbitmap);/设计类WNDCLASSEX wndClass;wndClass.cbClsExtra = NULL;wndClass.cbSize = sizeof(wndClass);wndClass.cbWndExtra = NULL;wndClass.hbrBa
5、ckground = hBrush;wndClass.hCursor = LoadCursor(hInstance,IDC_ARROW);wndClass.hIcon = hIcon;wndClass.hIconSm = hIconsm;wndClass.hInstance = hInstance;wndClass.lpfnWndProc = WndProc;wndClass.lpszClassName = g_ClassName;wndClass.style = CS_HREDRAW|CS_VREDRAW;wndClass.lpszMenuName = NULL;/注册if (!Regist
6、erClassEx(&wndClass)MessageBox(NULL,"注册失败","消息",MB_OK);/创建HWND hwnd = CreateWindow(g_ClassName,"贪吃蛇",WS_OVERLAPPEDWINDOW,200,50,600+16,600+38,NULL,NULL,hInstance,NULL);if (!hwnd)MessageBox(NULL,"创建失败!","消息",MB_OK);/显示ShowWindow(hwnd,nCmdShow);/消息
7、循环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_RIGHT;LRESULT CALLBACK WndProc(HWND hwnd,UINT nMsg,WPARAM wParam,LPARAM lParam)switch (nMsg)case WM_CREATE:CreateSnake
8、(&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_DOWN:VK = VK_DOWN;break;case VK_LEFT:VK = VK_LEFT;break;case VK_RIGHT:VK = VK_RIGHT;break;case ' ':K
9、illTimer(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-rect.top,hmeDc,0,0,500,600,SRCCOPY);DeleteDC(hmeDc);/*背景创建结束*/各种函数if (CanDie(pSnake)KillTimer(h
10、wnd,1);MessageBox(NULL,"笨蛋,撞到自己身上,我死啦!","消息",MB_OK);if ( CanEatBean(pSnake,pBean) )SnakeGrowUp(pSnake);DeleteBean(&pBean);CreateBean(&pBean,pSnake);SnakeRun(&pSnake);elseSnakeRun(&pSnake);ShowSnake(pSnake);ShowBean(pBean);case WM_PAINT:/*创建背景*/RECT rect;GetWindowR
11、ect(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_CLOSE:deleteAll(&pSnake);DeleteBean(&pBe
12、an);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;/蛇头的初始位置int y = 0;/蛇尾的初始位置while (i-)u =
13、 (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 = (unsigne
14、d)rand()%29)*20;ji = pSnake;while(pSnake)if (x = pSnake->x) && (y = pSnake->y) )x = (unsigned)rand()%29)*20;y = (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 deleteA
15、ll(Snake *pSnake)Snake *del;while(*pSnake)del = *pSnake;(*pSnake) = (*pSnake)->pNext;free(del);/删掉豆void DeleteBean(Bean *pBean)free(*pBean);*pBean = NULL;/显示蛇void ShowSnake(Snake *pSnake)while (pSnake)Rectangle(dc,pSnake->x,pSnake->y,pSnake->x+20,pSnake->y+20);pSnake = pSnake->pNex
16、t;/显示豆void ShowBean(Bean *pBean)Rectangle(dc,pBean->x,pBean->y,pBean->x+20,pBean->y+20);/贪吃蛇移动void SnakeRun(Snake *pSnake)Snake *ji,/记录变化的蛇头 *round;/循环遍历int x,/蛇头的当前位置x坐标y;/蛇头的当前位置y坐标ji = *pSnake;round = *pSnake;while (round->pNext)round = round->pNext;x = round->x;y = round->
17、;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;case 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吃到
18、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 SnakeGrowUp(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 (pSn
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年大班下学期语言教案黄雨伞教案及反思
- 中职学校计划范文
- 2024年产权补偿专项合同
- 幼儿园大班上学期工作计划
- 2024年学困生辅导计划
- 大学班委工作计划范本
- 小学五年级学期班主任工作计划例文
- 中学理论业务学习工作安排教研学习计划
- 北师大版八年级下学期数学期末复习训练
- 《建筑变形缝》课件
- 基础实验课程设计
- 港口行业深度报告:防御与景气兼具长周期维度的资产
- 自控仪表施工调试方案
- 2024年新人教版道德与法治一年级上册 8 课余生活真丰富 教学课件
- 消防大队联勤联动签订协议书模板
- 建筑信息模型技术员技能竞赛考试题库备赛500题(含答案)
- 公司项目交付管理制度
- 2022年搜索引擎营销实战教程(SEO-SEM)试卷带答案(题库)
- 护工照顾老人协议书范本
- 2024年大学英语三级考试级真题真题试卷
- 幼儿园教育指导纲要(试行)全文
评论
0/150
提交评论