C语言贪吃蛇项目

    技术2024-11-15  20

    C语言贪吃蛇项目

    #include<windows.h> #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) struct inf { unsigned x:5; unsigned y:5; }; bool stop; inf *all,food; int len=0; HHOOK hook; int tox=0,toy=0; HWND wins; bool quit; long CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); HWND CW(HINSTANCE h1,char *classname,char *windowname) { WNDCLASS wc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hbrBackground=::CreateSolidBrush(RGB(255,255,255)); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hIcon=NULL; wc.hInstance=h1; wc.lpfnWndProc=WndProc; wc.lpszClassName=classname; wc.lpszMenuName=NULL; wc.style=0; ::RegisterClass(&wc); return CreateWindow(classname,windowname,WS_OVERLAPPEDWINDOW&~WS_THICKFRAME,200,200,510,510,NULL,NULL,h1,NULL); } int WINAPI WinMain(HINSTANCE h1,HINSTANCE h2,LPSTR cmd,int show)//主函数 { HWND hwnd=CW(h1,"tcs","贪吃蛇"); wins=hwnd; ShowWindow(hwnd,1); UpdateWindow(hwnd); MSG msg; while(::GetMessage(&msg,0,0,0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } return 0; } void start_paint(HWND hwnd) { HDC hdc=GetDC(hwnd); Rectangle(hdc,-7,-7,510,510); HBRUSH hr=::CreateSolidBrush(RGB(0,0,0)); HBRUSH old=(HBRUSH)::SelectObject(hdc,hr); Rectangle(hdc,-7,-7,510,510); hr=::CreateSolidBrush(RGB(255,255,255)); SelectObject(hdc,hr); Rectangle(hdc,5,5,495,475); hr=::CreateSolidBrush(RGB(255,0,0)); SelectObject(hdc,hr); int x=5+((all[0].x-1)*25),y=5+((all[0].y-1)*25); Rectangle(hdc,x,y,x+20,y+20); hr=::CreateSolidBrush(RGB(0,255,0)); SelectObject(hdc,hr); for(int i=1;i<len;i++) { x=5+((all[i].x-1)*25); y=5+((all[i].y-1)*25); Rectangle(hdc,x,y,x+20,y+20); } hr=::CreateSolidBrush(RGB(255,255,0)); SelectObject(hdc,hr); x=5+(food.x*25); y=5+(food.y*25); Rectangle(hdc,x,y,x+20,y+20); SelectObject(hdc,old); } void key() { if((KEY_DOWN(87)||KEY_DOWN(38))&&!toy)//W { toy=-1; tox=0; } else if((KEY_DOWN(83)||KEY_DOWN(40))&&!toy)//S { toy=1; tox=0; } else if((KEY_DOWN(65)||KEY_DOWN(37))&&!tox)//A { toy=0; tox=-1; } else if((KEY_DOWN(68)||KEY_DOWN(39))&&!tox)//D { toy=0; tox=1; } if(KEY_DOWN(32)) { stop=stop?0:1; } } DWORD WINAPI game(LPVOID lp) { HDC hdc=GetDC(wins); MessageBox(wins,"点击确定开始游戏!","贪吃蛇",0); toy=1; tox=0; food.x=rand()%19; food.y=rand()%18; start_paint(wins); while(1) { if(stop) continue; if(quit)break; Sleep(200);//控制速度 if(quit)break; for(int i=len-1;i>=1;i--) { all[i].x=all[i-1].x; all[i].y=all[i-1].y; } all[0].x+=tox; all[0].y+=toy; if((((all[0].x-1)==food.x)&&((all[0].y-1)==food.y))) { food.x=rand()%19; food.y=rand()%18; len++; inf *temp=(inf*)malloc(10*len); for(int i=0;i<len-1;i++) { temp[i].x=all[i].x; temp[i].y=all[i].y; } temp[len-1].x=all[len-2].x; temp[len-1].y=all[len-2].y; inf *t=all; free(t); all=temp; while(1) { bool yes=true; for(int i=0;i<len;i++) { if((all[i].x-1)==food.x) { if((all[i].y-1)==food.y) { yes=false; break; } } } if(yes)break; else { food.x=rand()%19; food.y=rand()%18; } } } bool die=false; if(all[0].x==0||(all[0].y)==0||(all[0].x)==21||(all[0].y)==20) { MessageBox(NULL,"你撞到了墙!","游戏结束",16); break; } for(int i=1;i<len;i++) { if(all[i].x==all[0].x) { if(all[i].y==all[0].y) { die=true; } } } if(die) { MessageBox(NULL,"你咬到了自己!","游戏结束",16); break; } start_paint(wins); } switch(MessageBox(0,"重新开始游戏?","贪吃蛇",1)) { case 1: { free(all); stop=false; quit=false; all=(inf*)malloc(sizeof(inf)*3); all[0].x=2; all[0].y=3; all[1].x=2; all[1].y=2; all[2].x=2; all[2].y=1; len=3; CreateThread(0,0,game,0,0,0); break; } case 2: return 0; } return 0; } long CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wpa,LPARAM lpa) { switch(msg) { case WM_CREATE: { stop=false; quit=false; all=(inf*)malloc(sizeof(inf)*3); all[0].x=2; all[0].y=3; all[1].x=2; all[1].y=2; all[2].x=2; all[2].y=1; len=3; CreateThread(0,0,game,0,0,0); break; } case WM_DESTROY : { quit=true; PostQuitMessage(0); break; } case WM_PAINT: { break; } case WM_KEYDOWN: { key(); break; } } return DefWindowProc(hwnd,msg,wpa,lpa); }
    Processed: 0.012, SQL: 9