编程贪吃蛇小游戏(编程贪吃蛇)

时间:2023-09-16 21:51:17 来源:
导读 您好,现在蔡蔡来为大家解答以上的问题。编程贪吃蛇小游戏,编程贪吃蛇相信很多小伙伴还不知道,现在让我们一起来看看吧!1、这里有我以前敲...

您好,现在蔡蔡来为大家解答以上的问题。编程贪吃蛇小游戏,编程贪吃蛇相信很多小伙伴还不知道,现在让我们一起来看看吧!

1、这里有我以前敲过的贪吃蛇。

2、不介意可以参考一下。

3、(ps:需要建一了application而不是console application)#include #include #include #include #define speed 200#define left 100#define top 50#define right 500#define bottom 350struct snake{ POINT pos; snake *front,*next;};LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);bool InitWindow(HINSTANCE,int);void items(HDC,int,int);void MoveSnake(int);void remove();void destroy();void inf(int,int,int,int);void inf(const char*,int,int,int,int);void inf(const char*,int,int,int);bool is_fail();void eat();void wall();void show_fruit(); UINT ShowMode; PAINTSTRUCT ps; bool onoff; HFONT hf; char judge[20]; int dir; HDC hdc; HWND hwnd; HBRUSH BlackBrush,MainBrush,NULLBrush; HPEN MainPen,BlackPen; snake *head,*tail,*temp; POINT fruit;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){ onoff=1; int i; dir=-2; tail=NULL; InitWindow(hInstance,nCmdShow); for(i=0;i<5;i++) { if(!tail) { head=new snake; tail=head; head->pos.x=300+i*10; head->pos.y=200; items(hdc,head->pos.x,head->pos.y); } else { tail->next=new snake; tail->next->front=tail; tail=tail->next; tail->pos.x=300+i*10; tail->pos.y=200; tail->next=NULL; items(hdc,tail->pos.x,tail->pos.y); } } hf=CreateFont(20,0,0,0,400,0,0,0,GB2312_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"华文行楷"); SelectObject(hdc,hf); SetBkColor(hdc,RGB(124,146,131)); SelectObject(hdc,MainBrush); wall(); show_fruit(); inf("得分:",260,0,0); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } destroy(); return 0;}LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){ static int seed=15; switch(msg) { case WM_TIMER: MoveSnake(dir); if(!is_fail()) { KillTimer(hwnd,1); MessageBox(hwnd,"you are lost","caption",0); } eat(); break; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); wall(); EndPaint(hwnd,&ps); hdc=GetDC(hwnd); case WM_KEYDOWN: switch(wParam) { case VK_UP:dir=(dir!=1?-1:dir);break; case VK_DOWN:dir=(dir!=-1?1:dir);break; case VK_RIGHT:dir=(dir!=-2?2:dir);break; case VK_LEFT:dir=(dir!=2?-2:dir);break; } break; case WM_CLOSE: if(MessageBox(hwnd,"确定退出?","caption",MB_YESNO)==IDYES) DestroyWindow(hwnd); break; case WM_DESTROY: ReleaseDC(hwnd,hdc); KillTimer(hwnd,1); PostQuitMessage(0); break; default:return DefWindowProc(hwnd,msg,wParam,lParam); } return 1;}bool InitWindow(HINSTANCE hInstance,int nCmdShow){ WNDCLASS wc; ***.style=CS_HREDRAW | CS_VREDRAW; wc.lpszClassName="test"; wc.lpszMenuName=NULL; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hbrBackground=CreateSolidBrush(RGB(124,146,131)); wc.hIcon=NULL; wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hInstance=hInstance; wc.lpfnWndProc=WndProc; MainBrush=CreateSolidBrush(RGB(124,146,131)); BlackBrush=(HBRUSH)GetStockObject(BLACK_BRUSH); NULLBrush=(HBRUSH)GetStockObject(NULL_BRUSH); MainPen=CreatePen(PS_SOLID,1,RGB(124,146,131)); BlackPen=CreatePen(PS_SOLID,1,RGB(0,0,0)); if(!RegisterClass(&wc)) return false; hwnd=CreateWindow("test","贪吃蛇",WS_SYSMENU,400,150,616,400,NULL,NULL,hInstance,NULL); hdc=BeginPaint(hwnd,&ps); if(!hwnd) return false; ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); SetTimer(hwnd,1,speed,NULL); return true;}void items(HDC hdc,int x,int y){ SelectObject(hdc,BlackPen); SelectObject(hdc,NULLBrush); Rectangle(hdc,x,y,x+10,y+10); SelectObject(hdc,BlackBrush); Rectangle(hdc,x+2,y+2,x+8,y+8);// DeleteObject(BlackPen);// DeleteObject(BlackBrush);}void MoveSnake(int dir){ static int i=0; remove(); tail=tail->front; delete tail->next; tail->next=NULL; temp=new snake; temp->next=head; head->front=temp; temp->pos.x=head->pos.x+(dir/2)*10; temp->pos.y=head->pos.y+(dir%2)*10; head=temp; i++; items(hdc,head->pos.x,head->pos.y);}void remove(){ SelectObject(hdc,MainBrush); SelectObject(hdc,MainPen); Rectangle(hdc,tail->pos.x,tail->pos.y,tail->pos.x+10,tail->pos.y+10);// DeleteObject(MainBrush);// DeleteObject(MainPen);}void destroy(){ while(head->next) { head=head->next; delete head->front; } delete tail;}void inf(int x,int y,int px,int py){ inf("",x,y,px,py);}void inf(const char*str,int x,int y,int scores){ sprintf(judge,"%s%d",str,scores); TextOut(hdc,x,y,judge,strlen(judge));}void inf(const char*str,int x,int y,int px,int py){ sprintf(judge,"%s(%d,%d) ",str,px,py); TextOut(hdc,x,y,judge,strlen(judge));}bool is_fail(){ temp=head; int px=head->pos.x,py=head->pos.y; if(px=right||py=bottom) return 0; while(temp->next) { temp=temp->next; if(px==temp->pos.x&&py==temp->pos.y) return 0; } return 1;}void show_fruit(){ srand((UINT)time(NULL)); fruit.x=10*((rand()%(right-left-10))/10)+left; fruit.y=10*((rand()%(bottom-top-10))/10)+top; items(hdc,fruit.x,fruit.y);}void eat(){ inf("食物:",0,25,fruit.x,fruit.y); inf("蛇头:",0,0,head->pos.x,head->pos.y); static int scores=0; if(head->pos.x==fruit.x&&head->pos.y==fruit.y) { scores++; inf("得分:",260,0,scores); KillTimer(hwnd,1); temp=new snake; temp->next=head; head->front=temp; temp->pos.x=head->pos.x+(dir/2)*10; temp->pos.y=head->pos.y+(dir%2)*10; head=temp; items(hdc,head->pos.x,head->pos.y); SetTimer(hwnd,1,speed,NULL); show_fruit(); }}void wall(){ SelectObject(hdc,MainBrush); Rectangle(hdc,left-1,top-1,right+1,bottom+1);// DeleteObject(MainBrush);}。

本文就为大家分享到这里,希望小伙伴们会喜欢。

标签:
最新文章