1、贪吃蛇图形界面版可编译运行只要建个WIN32应用程序就行了。实测可编译运行!#include#include #include #include /rand()#include /time() 为了用时间产生随机数种子 srand() #define RowOfFrame 20#define ColumnOfFrame 20#define Width 16#define Height 16bool keydown=false;int randomx;int randomy;struct Node /蛇节 int x; int y; Node * next;typedef struct /蛇 i
2、nt length; /蛇长 Node *head; /蛇头 Node *tail; /蛇尾Snake;enum Direction up=1,down,left,right;Direction CurrentDirection;Snake *greedsnake=new Snake; /贪吃蛇LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lp
3、CmdLine, int nShowCmd) /design the window class WNDCLASS wndcls; wndcls.cbClsExtra=0; wndcls.cbWndExtra=0; wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndcls.hInstance=hInstance; wndcls.lpfnWndProc=
4、WinProc; wndcls.lpszClassName=weixin; wndcls.lpszMenuName=NULL; wndcls.style=CS_HREDRAW | CS_VREDRAW; /register the window class RegisterClass(&wndcls); /create the window HWND hwnd; hwnd=CreateWindow(weixin,贪吃蛇游戏,WS_OVERLAPPEDWINDOW &(WS_MAXIMIZEBOX)&(WS_SIZEBOX),0,0,330,360,NULL,NULL,hInstance,NUL
5、L); /show the window ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); /the message loop MSG msg; while(GetMessage(&msg,NULL,0,0) TranslateMessage(&msg); DispatchMessage(&msg); return 0;bool Judge(Snake *greedsnake,int randomx,int randomy) /判断随机食物会不会在蛇身上 Node *node1=greedsnake-head; int length=gre
6、edsnake-length; while(length-) if(node1-x=randomx & node1-y=randomy) return true; /蛇中含有node1节点 node1=node1-next; return false; /蛇中没有node1节点void randomize(Snake *greedsnake,int &randomx,int &randomy) /随机产生食物的位置 srand(unsigned)time(NULL); randomx=rand()%RowOfFrame; randomy=rand()%ColumnOfFrame; while(
7、Judge(greedsnake,randomx,randomy) randomx=rand()%RowOfFrame; randomy=rand()%ColumnOfFrame; void DrawFood(HDC hdc,int randomx,int randomy) /在蛇吃到之前只能画一次!/ COLORREF color;/ color=SetDCBrushColor(hdc,RGB(R,G,B);/ SetDCBrushColor(hdc,RGB(R,G,B);/ HBRUSH hbrush2=(HBRUSH)GetStockObject(DC_BRUSH); HPEN hpen
8、=CreatePen(PS_SOLID,6,RGB(20,200,200); SelectObject(hdc,hpen);/ HBRUSH hbrush2=(HBRUSH)GetStockObject(RGB(80,80,80); / SelectObject(hdc,hbrush2); / SelectObject(hdc,GetStockObject(DC_BRUSH);/ SetDCBrushColor(hdc,RGB(R,G,B); Rectangle(hdc,Width*randomx+4,Height*randomy+4,Width*randomx+12,Height*rando
9、my+12);/ SelectObject(hdc,WHITE_BRUSH);/界面设计void Design(HDC hdc) HPEN hpen; hpen=CreatePen(PS_DOT,1,RGB(255,0,0); HBRUSH hbrush; hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH); SelectObject(hdc,hpen); SelectObject(hdc,hbrush); int i; for(i=0;i=RowOfFrame;i+) if(MoveToEx(hdc,Width*i,0,NULL) /设置起始点 LineTo
10、(hdc,Width*i,Width*RowOfFrame); for(i=0;ix=0; node1-y=0; node1-next=NULL; greedsnake-head=node1; greedsnake-length=1; greedsnake-tail=node1; CurrentDirection=right;/画蛇void DrawSnake(HDC hdc,Snake * greedsnake) int length=greedsnake-length; int x; int y; x=greedsnake-head-x *Width; y=greedsnake-head-
11、y *Height; MoveToEx(hdc,x+8,y+8,NULL); /下面注释的代码是完成画花蛇的功能,但是因为时间的问题会导致程序的运行不正确 /给画笔产生随机颜色/* int R,G,B; srand(unsigned)time(NULL); R=rand()%256; G=rand()%256; B=rand()%256; HPEN hpen=CreatePen(PS_SOLID,6,RGB(R,G,B);*/ /给画笔产生固定的蓝色 HPEN hpen=CreatePen(PS_SOLID,6,RGB(0,0,255); SelectObject(hdc,hpen); if(
12、length=1) LineTo(hdc,x+16,y+8); if(length1) Node *node2=new Node; node2=greedsnake-head; while(-length) /随机产生画笔的颜色 /* R=rand()%256; G=rand()%256; B=rand()%256; hpen=CreatePen(PS_SOLID,6,RGB(R,G,B); SelectObject(hdc,hpen);*/ node2=node2-next; int x2; int y2; x2=node2-x*Width+8; y2=node2-y*Height+8; L
13、ineTo(hdc,x2,y2); MoveToEx(hdc,x2,y2,NULL); /判断两个节点的坐标是否相等bool Equal(Node *node1,Node *nodetemp) if(node1-x=nodetemp-x & node1-y=nodetemp-y) return true; else return false;/判断是否犯规 nodetemp表示下一个即将入队的节点bool Hit(Snake *greedsnake,Node * nodetemp) /judge if the greed snake hit the wall if(nodetemp-xx=20
14、 |nodetemp-yy=20) return true; /hit the wall /judge if the greed snake hit itself Node *node1; node1=greedsnake-head; int length=greedsnake-length; while(length-) if(Equal(node1,nodetemp) return true; /hit itself else node1=node1-next; return false;/求蛇下一个运动的地点void NextNode(Snake *greedsnake,Node *no
15、de1,char c) /calculate the next point keydown=true; if(c=w) /up if(CurrentDirection=down) node1-x=greedsnake-tail-x; node1-y=greedsnake-tail-y+1; else node1-x=greedsnake-tail-x; node1-y=greedsnake-tail-y-1; CurrentDirection=up; else if(c=a) /left if(CurrentDirection=right) node1-x=greedsnake-tail-x+
16、1; node1-y=greedsnake-tail-y; else node1-x=greedsnake-tail-x-1; node1-y=greedsnake-tail-y; CurrentDirection=left; else if(c=s) /down if(CurrentDirection=up) node1-x=greedsnake-tail-x; node1-y=greedsnake-tail-y-1; else node1-x=greedsnake-tail-x; node1-y=greedsnake-tail-y+1; CurrentDirection=down; els
17、e if(c=d) /right if(CurrentDirection=left) node1-x=greedsnake-tail-x-1; node1-y=greedsnake-tail-y; else node1-x=greedsnake-tail-x+1; node1-y=greedsnake-tail-y; CurrentDirection=right; /将下一个地点加入到蛇身上void Push(Snake *greedsnake,Node *node1) /判断是不是食物 if(node1-x!=randomx | node1-y!=randomy) /it is not th
18、e location of food greedsnake-tail-next=node1; greedsnake-tail=node1; Node *node2=greedsnake-head; greedsnake-head=node2-next; delete node2; else greedsnake-tail-next=node1; greedsnake-tail=node1; greedsnake-length+; /吃到食物,蛇的长度要增1 randomize(greedsnake,randomx,randomy); /销毁蛇void Destroy(Snake *greeds
19、nake) Node *node1; node1=greedsnake-head; while(node1-next!=NULL) Node * node2; node2=node1; node1=node1-next; delete node2; greedsnake-length-; LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) switch(uMsg) case WM_CHAR: Node * node1; node1=new Node; node1-next=NULL; RE
20、CT rect; GetClientRect(hwnd,&rect); HDC hdc; hdc=GetDC(hwnd); HBRUSH hbrush; hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH); SelectObject(hdc,hbrush); switch(wParam) case w: /UP NextNode(greedsnake,node1,w); if(Hit(greedsnake,node1) char sz40; sprintf(sz,The score is %d.nClick OK to exit,greedsnake-leng
21、th); KillTimer(hwnd,1); MessageBox(hwnd,sz,You Failed,MB_OK); DestroyWindow(hwnd); Push(greedsnake,node1); break; case a: /LEFT NextNode(greedsnake,node1,a); if(Hit(greedsnake,node1) char sz40; sprintf(sz,The score is %d.nClick OK to exit,greedsnake-length); KillTimer(hwnd,1); MessageBox(hwnd,sz,You
22、 Failed,MB_OK); DestroyWindow(hwnd); Push(greedsnake,node1); break; case s: /DOWN NextNode(greedsnake,node1,s); if(Hit(greedsnake,node1) char sz40; sprintf(sz,The score is %d.nClick OK to exit,greedsnake-length); KillTimer(hwnd,1); MessageBox(hwnd,sz,You Failed,MB_OK); DestroyWindow(hwnd); Push(gree
23、dsnake,node1); break; case d: /RIGHT NextNode(greedsnake,node1,d); if(Hit(greedsnake,node1) char sz40; sprintf(sz,The score is %d.nClick OK to exit,greedsnake-length); KillTimer(hwnd,1); MessageBox(hwnd,sz,You Failed,MB_OK); DestroyWindow(hwnd); Push(greedsnake,node1); break; default: /不是规定的按键,应该将no
24、de1节点空间删除掉 delete node1; break; Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom); /擦除原来的界面 /绘制新的界面 Design(hdc); DrawSnake(hdc,greedsnake); DrawFood(hdc,randomx,randomy); ReleaseDC(hwnd,hdc); break; case WM_PAINT: PAINTSTRUCT ps; HDC hdc2; hdc2=BeginPaint(hwnd,&ps); /画界面函数 Design(hdc2); DrawS
25、nake(hdc2,greedsnake); DrawFood(hdc2,randomx,randomy); EndPaint(hwnd,&ps); break; case WM_CLOSE: KillTimer(hwnd,1); DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_CREATE: /做初始化的一些工作 Init(greedsnake); randomize(greedsnake,randomx,randomy); MessageBox(hwnd,上(w) 下(s) 左(
26、a) 右(d),操作说明,0); / TIMER timer; SetTimer(hwnd,1,1000,NULL); break; case WM_TIMER: if(!keydown) Node * node_temp; node_temp=new Node; node_temp-next=NULL; if(CurrentDirection=up) node_temp-x=greedsnake-tail-x; node_temp-y=greedsnake-tail-y-1; if(Hit(greedsnake,node_temp) char sz40; sprintf(sz,The score is %d.nClick OK to exit,greedsnake-length); KillTimer(hwnd,1); MessageBox(hwnd,sz,You Failed,MB_OK); DestroyWindow(hwnd); else if(CurrentDirection=down) node_temp-x=greedsnake-tail-
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1