贪吃蛇简易代码.docx
《贪吃蛇简易代码.docx》由会员分享,可在线阅读,更多相关《贪吃蛇简易代码.docx(16页珍藏版)》请在冰豆网上搜索。
![贪吃蛇简易代码.docx](https://file1.bdocx.com/fileroot1/2023-2/4/50cf5ee5-8884-4711-bcc4-38a23114d0ff/50cf5ee5-8884-4711-bcc4-38a23114d0ff1.gif)
贪吃蛇简易代码
#include
#include"resource1.h"
#include
#include
#include
//蛇的结构体
typedefstructSNAKE
{
intx;
inty;
structSNAKE*pNext;
}Snake;
//豆的结构体
typedefstructBEAN
{
intx;
inty;
structBEAN*pNext;
}Bean;
//函数原型
voidCreateSnake(Snake**pSnake);
voiddeleteAll(Snake**pSnake);
voidShowSnake(Snake*pSnake);
voidCreateBean(Bean**pBean,Snake*pSnake);
voidShowBean(Bean*pBean);
voidDeleteBean(Bean**pBean);
voidSnakeRun(Snake**pSnake);
voidSnakeGrowUp(Snake*pSnake);
intCanEatBean(Snake*pSnake,Bean*pBean);
intCanDie(Snake*pSnake);
/////////////////////////////////////////////////////////////////////////////////////
charg_ClassName[20]="ClassName";
LRESULTCALLBACKWndProc(HWNDhwnd,UINTnMsg,WPARAMwParam,LPARAMlParam);
HBITMAPhBitMap;
intCALLBACKWinMain(HINSTANCEhInstance,
HINSTANCEhPreInstance,
LPSTRpCmdLine,
intnCmdShow)
{
HBRUSHhBrush=CreateSolidBrush(RGB(100,100,410));
HICONhIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_wnd_icon));
HICONhIconsm=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_wnd_iconsm));
hBitMap=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_wdnbitmap));
//设计类
WNDCLASSEXwndClass;
wndClass.cbClsExtra=NULL;
wndClass.cbSize=sizeof(wndClass);
wndClass.cbWndExtra=NULL;
wndClass.hbrBackground=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(!
RegisterClassEx(&wndClass))
{
MessageBox(NULL,"注册失败","消息",MB_OK);
}
//创建
HWNDhwnd=CreateWindow(g_ClassName,
"贪吃蛇",
WS_OVERLAPPEDWINDOW,
200,
50,
600+16,
600+38,
NULL,
NULL,
hInstance,
NULL);
if(!
hwnd)
{
MessageBox(NULL,"创建失败!
","消息",MB_OK);
}
//显示
ShowWindow(hwnd,nCmdShow);
//消息循环
MSGmsg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);//翻译
DispatchMessage(&msg);//回调函数
}
return0;
}
HDCdc=NULL;
Bean*pBean=NULL;
Snake*pSnake=NULL;
intVK=VK_RIGHT;
LRESULTCALLBACKWndProc(HWNDhwnd,UINTnMsg,WPARAMwParam,LPARAMlParam)
{
switch(nMsg)
{
caseWM_CREATE:
{
CreateSnake(&pSnake);//创建蛇
CreateBean(&pBean,pSnake);
dc=GetDC(hwnd);
}
break;
caseWM_KEYDOWN:
{
switch(wParam)
{
caseVK_RETURN:
//按下后就开始
SetTimer(hwnd,1,150,NULL);
break;
caseVK_UP:
VK=VK_UP;
break;
caseVK_DOWN:
VK=VK_DOWN;
break;
caseVK_LEFT:
VK=VK_LEFT;
break;
caseVK_RIGHT:
VK=VK_RIGHT;
break;
case'':
KillTimer(hwnd,1);
break;
}
break;
}
caseWM_TIMER:
{
/*创建背景*/
RECTrect;
GetWindowRect(hwnd,&rect);
HDChmeDc=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(hwnd,1);
MessageBox(NULL,"笨蛋,撞到自己身上,我死啦!
","消息",MB_OK);
}
if(CanEatBean(pSnake,pBean))
{
SnakeGrowUp(pSnake);
DeleteBean(&pBean);
CreateBean(&pBean,pSnake);
SnakeRun(&pSnake);
}
else
{
SnakeRun(&pSnake);
}
ShowSnake(pSnake);
ShowBean(pBean);
}
caseWM_PAINT:
{
/*创建背景*/
RECTrect;
GetWindowRect(hwnd,&rect);
HDChmeDc=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;
}
caseWM_CLOSE:
deleteAll(&pSnake);
DeleteBean(&pBean);
DestroyWindow(hwnd);
break;
caseWM_DESTROY:
ReleaseDC(hwnd,dc);
PostQuitMessage(0);
break;
caseWM_QUIT:
break;
}
returnDefWindowProc(hwnd,nMsg,wParam,lParam);
}
/////////////////////////////////函数///////////////////////////////////////////////////////////
//创建蛇CreateSnake()
voidCreateSnake(Snake**pSnake)
{
Snake*u=NULL,
*w=NULL;
inti=3;//开始的蛇有三截
intx=0;//蛇头的初始位置
inty=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->pNext=u;
}
w=u;
}
/*蛇的三届创建完毕*/
}
//创建豆
voidCreateBean(Bean**pBean,Snake*pSnake)
{
Snake*ji;
intx=0;//随机的位置x坐标
inty=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=((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;
}
//删掉全部蛇
voiddeleteAll(Snake**pSnake)
{
Snake*del;
while(*pSnake)
{
del=*pSnake;
(*pSnake)=(*pSnake)->pNext;
free(del);
}
}
//删掉豆
voidDeleteBean(Bean**pBean)
{
free(*pBean);
*pBean=NULL;
}
//显示蛇
voidShowSnake(Snake*pSnake)
{
while(pSnake)
{
Rectangle(dc,pSnake->x,pSnake->y,pSnake->x+20,pSnake->y+20);
pSnake=pSnake->pNext;
}
}
//显示豆
voidShowBean(Bean*pBean)
{
Rectangle(dc,pBean->x,pBean->y,pBean->x+20,pBean->y+20);
}
//贪吃蛇移动
voidSnakeRun(Snake**pSnake)
{
Snake*ji,//记录变化的蛇头
*round;//循环遍历
intx,//蛇头的当前位置x坐标
y;//蛇头的当前位置y坐标
ji=*pSnake;
round=*pSnake;
while(round->pNext)
{
round=round->pNext;
}
x=round->x;
y=round->y;
switch(VK)
{
caseVK_UP:
if(y==0)
{
y=600;
}
y-=20;
break;
caseVK_DOWN:
if(y==600)
{
y=0;
}
y+=20;
break;
caseVK_LEFT:
if(x==0)
{
x=600;
}
x-=20;
break;
caseVK_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吃到
intCanEatBean(Snake*pSnake,Bean*pBean)
{
while(pSnake->pNext)
{
pSnake=pSnake->pNext;
}
if((pSnake->x==pBean->x)&&(pSnake->y==pBean->y))
{
return1;
}
return0;
}
//长大
voidSnakeGrowUp(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会死
intCanDie(Snake*pSnake)
{
Snake*ji=NULL;
intx=0,
y=0;
ji=pSnake;
while(pSnake->pNext)
{
pSnake=pSnake->pNext;
}
x=pSnake->x;
y=pSnake->y;
switch(VK)
{
caseVK_UP:
y-=20;
break;
caseVK_DOWN:
y+=20;
break;
caseVK_LEFT:
x-=20;
break;
caseVK_RIGHT:
x+=20;
break;
}
while(ji->pNext)
{
if((x==ji->x)&&(y==ji->y))
{
return1;
}
ji=ji->pNext;
}
return0;
}
(范文素材和资料部分来自网络,供参考。
可复制、编制,期待你的好评与关注)