C贪吃蛇源码,帮看看为什么分数上不了200分就玩不了了
程序代码:
#include <windows.h> #include <time.h> typedef enum _bool{false,true} bool; //定义蛇结构 struct _Snake { int X; int Y; int Snake_fx; }Snake[300]; int SnakeNum=3; //蛇长度,初始为3 int SnakeFX=3; //蛇头方向 POINT Food; //食物 int Block=20; //块大小 int WinW,WinH; //屏幕尺寸 int Score;//分数 bool isCONFINE(int x,int y); //蛇头前进方向边界检测 bool isFOOD(int x,int y); //蛇头前进方向食物检测 bool isOWN(int x,int y); //蛇头前进方向是自身 void eatFOOD(); //吃食物 void creFOOD(); //生成食物 bool isOver(HWND hwnd,int x,int y); //游戏结束 void setAppText(HWND hwnd,int score);//设置标题 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("zfSnake") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, TEXT ("zfSnake"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 816, 638, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps ; HPEN hPen,hPenOld; RECT winR; int i; int TempFX1,TempFX2; switch (message) { case WM_CREATE: SetTimer (hwnd, 0, 100, NULL) ;//设置时间 //获取客户区大小 GetClientRect(hwnd,&winR); WinW=winR.right; WinH=winR.bottom; //初始化蛇 for(i=0;i<SnakeNum;i++) { Snake[i].X=400;Snake[i].Y=200+i*20;Snake[i].Snake_fx=SnakeFX; } creFOOD(); //随机生成食物 Score=0; return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps); hPen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 )); hPenOld = ( HPEN )SelectObject ( hdc, hPen ); //画网格地图 for(i=0;i<=WinH;i=i+Block) { MoveToEx(hdc,0,i,NULL); LineTo(hdc,WinW,i); } for(i=0;i<=WinW;i=i+Block) { MoveToEx(hdc,i,0,NULL); LineTo(hdc,i,WinH); } //FillRgn(hdc,CreateRectRgn(0,0,20,20),(HBRUSH)hPen); for(i=0;i<SnakeNum;i++) { FillRgn(hdc,CreateRectRgn(Snake[i].X,Snake[i].Y,Snake[i].X+20,Snake[i].Y+20),(HBRUSH)(hPen)); } FillRgn(hdc,CreateRectRgn(Food.x*20,Food.y*20,Food.x*20+20,Food.y*20+20),(HBRUSH)(hPen)); setAppText(hwnd,Score*10); SelectObject (hdc , hPenOld); EndPaint(hwnd, &ps); return 0; case WM_TIMER: if((SnakeFX==1 && Snake[0].Snake_fx==2) || (SnakeFX==2 && Snake[0].Snake_fx==1)) goto xx; if((SnakeFX==3 && Snake[0].Snake_fx==4) || (SnakeFX==4 && Snake[0].Snake_fx==3)) goto xx; switch(SnakeFX) { case 1: if(isOver(hwnd,Snake[0].X-Block,Snake[0].Y)) return 0; if(isFOOD(Snake[0].X-Block,Snake[0].Y)) { eatFOOD(); } Snake[0].Snake_fx=1; break; case 2: if(isOver(hwnd,Snake[0].X+Block,Snake[0].Y)) return 0; if(isFOOD(Snake[0].X+Block,Snake[0].Y)) { eatFOOD(); } Snake[0].Snake_fx=2; break; case 3: if(isOver(hwnd,Snake[0].X,Snake[0].Y-Block)) return 0; if(isFOOD(Snake[0].X,Snake[0].Y-Block)) { eatFOOD(); } Snake[0].Snake_fx=3; break; case 4: if(isOver(hwnd,Snake[0].X,Snake[0].Y+Block)) return 0; if(isFOOD(Snake[0].X,Snake[0].Y+Block)) { eatFOOD(); } Snake[0].Snake_fx=4; break; } //蛇前进 xx: for(i=0;i<SnakeNum;i++) { switch(Snake[i].Snake_fx) { case 1: Snake[i].X=Snake[i].X-Block; break; case 2: Snake[i].X=Snake[i].X+Block; break; case 3: Snake[i].Y=Snake[i].Y-Block; break; case 4: Snake[i].Y=Snake[i].Y+Block; break; } } TempFX1=Snake[0].Snake_fx; for(i=1;i<SnakeNum;i++) { TempFX2=Snake[i].Snake_fx; Snake[i].Snake_fx=TempFX1; TempFX1=TempFX2; } InvalidateRect(hwnd, NULL, true); return 0; case WM_KEYDOWN: //处理键盘消息 switch(wParam) { case VK_LEFT: SnakeFX = 1; break; case VK_RIGHT: SnakeFX = 2; break; case VK_UP: SnakeFX = 3; break; case VK_DOWN: SnakeFX = 4; break; } return 0; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; } bool isCONFINE(int x,int y) { if((x<0 || x>780) || (y<0 || y>580)) return true; return false; } bool isFOOD(int x,int y) { if(Food.x*20==x && Food.y*20==y) return true; return false; } bool isOWN(int x,int y) { int i; for(i=0;i<SnakeNum;i++) { if(Snake[i].X==x && Snake[i].Y==y) return true; } return false; } void eatFOOD() { SnakeNum++; switch(Snake[SnakeNum-2].Snake_fx) { case 1: Snake[SnakeNum-1].X=Snake[SnakeNum-2].X+Block; Snake[SnakeNum-1].Y=Snake[SnakeNum-2].Y; Snake[SnakeNum-1].Snake_fx=Snake[SnakeNum-2].Snake_fx; break; case 2: Snake[SnakeNum-1].X=Snake[SnakeNum-2].X-Block; Snake[SnakeNum-1].Y=Snake[SnakeNum-2].Y; Snake[SnakeNum-1].Snake_fx=Snake[SnakeNum-2].Snake_fx; break; case 3: Snake[SnakeNum-1].X=Snake[SnakeNum-2].X; Snake[SnakeNum-1].Y=Snake[SnakeNum-2].Y+Block; Snake[SnakeNum-1].Snake_fx=Snake[SnakeNum-2].Snake_fx; break; case 4: Snake[SnakeNum-1].X=Snake[SnakeNum-2].X; Snake[SnakeNum-1].Y=Snake[SnakeNum-2].Y-Block; Snake[SnakeNum-1].Snake_fx=Snake[SnakeNum-2].Snake_fx; break; } Score++; creFOOD();//吃完食物后再随机生成食物 } bool isOver(HWND hwnd,int x,int y) { if((isCONFINE(x,y)) || (isOWN(x,y))) { KillTimer(hwnd,0); MessageBox(hwnd,TEXT("游戏结束!!!"),"GameOver",MB_OK); return true; } return false; } void creFOOD() { srand((unsigned)time(NULL)); Food.x=rand()%(WinW/20); Food.y=rand()%(WinH/20); } void setAppText(HWND hwnd,int score) { TCHAR Buffer[20]; wsprintf(Buffer,TEXT("Snake-[分数:%d]"),score); SetWindowText(hwnd,Buffer); }