首页 > 代码库 > 抠图划线

抠图划线

在贪吃蛇的代码中,我们在定时器中将pa函数注释掉,可以解决其不动的问题,我们用此背景可以进行画图,我们首先可以画一条直线,此时我们不能用HDC去画,应该创建一个临时的HDC和位图,进行画,之后将临时的加载到原HDC中,因为如果不创建临时的,在画完起点后就被release掉了,最后结果就是终点和(0,0)的直线(默认0,0)

  1 #include<Windows.h>  2 #include "resource.h"  3 #include <time.h>  4 typedef char boolean;  5 #define TRUE 1  6 #define FALSE 0  7   8   9  10  11  LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM); 12  13  typedef struct E{ 14     int h,l; 15     struct E *pnext; 16     struct E *plast; 17  }Apple,Snake,node; 18   19  Apple apple={5,6}; 20  Snake *top=NULL; 21  Snake *end=NULL; 22  23  24  25  26  27 HBITMAP h_back; 28 HBITMAP h_apple; 29 HBITMAP h_body; 30 HBITMAP h_l; 31 HBITMAP h_r; 32 HBITMAP h_u; 33 HBITMAP h_d; 34 int g_fx=1; 35 void add(int h,int l); 36 void initSnake(); 37 void show(HDC hdc); 38 void showApple(HDC hdc); 39 void showSnake(HDC hdc); 40  41 void pa(); 42 boolean iseat(); 43 void Newapple(); 44 boolean isziyao(); 45 boolean iszhuang(); 46  47  48  49  50  51  52 int WINAPI  WinMain(HINSTANCE hlnstance,HINSTANCE hPrelnstance, 53             LPSTR lpCmdLine,int nCmdShow){        //callback 54  55  56     //设计 57     HWND hwnd; 58     MSG  msg; 59     LPCSTR classname="牟柏旭"; 60     WNDCLASSEX ex; 61      62     ex.style=CS_HREDRAW | CS_VREDRAW; 63     ex.cbSize=sizeof(WNDCLASSEX); 64     ex.cbClsExtra=0; 65     ex.cbWndExtra=0; 66     ex.hbrBackground=CreateSolidBrush(RGB(171,171,226)); 67     ex.hCursor=NULL; 68     ex.hIcon=NULL; 69     ex.hIconSm=NULL; 70     ex.hInstance=hlnstance; 71     ex.lpszMenuName=0; 72     ex.lpszClassName=classname; 73     ex.lpfnWndProc=MyWndProc;  //消息过程函数  消息回调函数 74  75     //注册 76      RegisterClassEx(&ex); 77      //创建 78      hwnd= CreateWindow(classname,"mubaixu",WS_OVERLAPPEDWINDOW,50,50,620,630,NULL,NULL, 79             hlnstance,NULL); 80      SetTimer (hwnd, 1, 500, NULL) ; 81  82      //最后一个为是否创建多文档,比如code可以打开多个程序 83  84  85       ShowWindow(hwnd,nCmdShow); 86  87  88  89     h_back=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP1)); 90     h_apple=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP2)); 91     h_body=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP3)); 92     h_l=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP6)); 93     h_r=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP4)); 94     h_u=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP5)); 95     h_d=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP7)); 96     initSnake(); 97     srand((unsigned int )time(0)); 98       while(GetMessage(&msg,NULL,0,0)){ 99           100           TranslateMessage(&msg);101 102           DispatchMessage(&msg);103           //调用MyWndProc,系统调用104         105           //指定接收哪个窗口的消息,如果为NULL用于接收所有窗口106           // 后两个为过滤107       }108 109 110 111     return 0;112 }    113 114 int xx=200;115 HDC tmp_dc;116 HBITMAP tmp_bitmap;117 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){118     int x,y;119 120 121 //    char tx[10];122     //char ty[10];123     HDC hdc;124     //temp_hdc;125     //HBRUSH newbrush;126     //HBRUSH oldbrush;127     PAINTSTRUCT ps;128 129 //    HBITMAP hbitmap ;130 131     switch (Msg)132     {133 134     case WM_KEYDOWN:135             switch (wParam)136             {137                  case VK_UP:138                       g_fx=1;139                      break;140                  case VK_DOWN:141                        g_fx=3;142                      break;143                  case VK_RIGHT:144                      g_fx=0;145                      break;146                  case VK_LEFT:147                       g_fx=2;148                      break;149 150                 default:151                     break;152             }153         break;154 155     /*case WM_MOUSEMOVE:156         hdc = GetDC (hWnd) ;157 158         x = LOWORD (lParam) ;159         y = HIWORD (lParam) ;160 161         LineTo (tmp_dc, x, y) ;162         BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY);163         ReleaseDC (hWnd, hdc) ;164 165         break;*/166     case WM_LBUTTONDOWN:167     168     169         hdc = GetDC (hWnd) ;170    171 172      //   Rectangle(hdc, 150,150,200,200);173 174         //创建临时的hdc和位图,并且把原图传过去175         tmp_dc=CreateCompatibleDC(hdc);176         tmp_bitmap=CreateCompatibleBitmap(hdc,620,630);177         SelectObject(tmp_dc,tmp_bitmap);178         BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY);179 180         x = LOWORD (lParam) ;181         y = HIWORD (lParam) ;182         //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/183 184         MoveToEx (tmp_dc, x, y, NULL) ;185 186         187         ReleaseDC (hWnd, hdc) ;188         break;189 190     case WM_LBUTTONUP:191         hdc = GetDC (hWnd) ;192 193         x = LOWORD (lParam) ;194         y = HIWORD (lParam) ;195 196         LineTo (tmp_dc, x, y) ;197         BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY);198 199         DeleteObject(tmp_dc);200         DeleteObject(tmp_bitmap);201         ReleaseDC (hWnd, hdc) ;202 203         break;204     case WM_CLOSE:205         PostQuitMessage(0);206 207         break;208     209     case WM_PAINT:210         hdc = BeginPaint (hWnd, &ps) ;211 212         show(hdc);213 214         //Rectangle(hdc, 50,50,100,100);215         //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ;216         //temp_hdc = CreateCompatibleDC (hdc) ;217         //SelectObject(temp_hdc,h_back);    218         ////Rectangle(temp_hdc, 10,10,50,50);219     220         //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ;221 222         //xx+=20;223         //Ellipse( hdc, xx, 300, xx+70, 340);224 225           EndPaint (hWnd, &ps) ;226         break;227 228     case WM_TIMER:229 230 231 232         //pa();233         if(iseat()){234             Newapple();235             add(-20,-20);236         }237         if(isziyao()||iszhuang()){238             PostQuitMessage(0);239         }240 241         hdc = GetDC (hWnd) ;242             //show(hdc);重绘243         /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动244         oldbrush=(HBRUSH)SelectObject(hdc,newbrush);245          Rectangle( hdc, 0, 0,600,600);246           247         DeleteObject(newbrush);248         SelectObject(hdc,oldbrush);249         xx+=20;250         Ellipse( hdc, xx, 300, xx+70, 340);251             Rectangle(hdc, 50,50,100,100);*/252         ReleaseDC (hWnd, hdc) ;253         break;254     default:255         break;256     }257     //系统默认的消息处理函数258     return DefWindowProc(hWnd,Msg,wParam,lParam);259 }260 261 262 263  void add(int h,int l){264       node * pnode=(node *)malloc(sizeof(node));265       pnode->h=h;266       pnode->l=l;267       pnode->plast=NULL;268       pnode->pnext=NULL;269       if(top==NULL){270         top=pnode;271       }272       else{273           pnode->plast=end;274           end->pnext=pnode;275       }276 277       end=pnode;278  }279 280 281  void initSnake(){282     int i;283      for( i=8;i<=10;i++){284          add(i,10);285      }286 287  }288 289  void show(HDC hdc){290     HDC temp_hdc;291     temp_hdc = CreateCompatibleDC (hdc) ;292     SelectObject(temp_hdc,h_back);    293     294     BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ;295     DeleteObject(temp_hdc);296     showApple( hdc);297     showSnake( hdc);298  }299  void showApple(HDC hdc){300     HDC temp_hdc;301     temp_hdc = CreateCompatibleDC (hdc) ;302     SelectObject(temp_hdc,h_apple);    303     304     BitBlt (hdc, apple.l*30+30,apple.h*30+30, 30, 30, temp_hdc, 0, 0, SRCCOPY) ;305     DeleteObject(temp_hdc);306  }307  void showSnake(HDC hdc){308     HDC temp_hdc;309     Snake *ptop=top;310     temp_hdc = CreateCompatibleDC (hdc) ;311     switch (g_fx)312     {313 314     case 0:315         SelectObject(temp_hdc,h_r);    316         break;317     case 1:318         SelectObject(temp_hdc,h_u);    319         break;320     case 2:321         SelectObject(temp_hdc,h_l);    322         break;323     case 3:324         SelectObject(temp_hdc,h_d);    325         break;326     default:327         break;328     }329 330     331     BitBlt (hdc, top->l*30+30,top->h*30+30  , 30, 30, temp_hdc, 0, 0, SRCCOPY) ;332     DeleteObject(temp_hdc);333 334     335     temp_hdc = CreateCompatibleDC (hdc) ;336     SelectObject(temp_hdc,h_body);    337     ptop=ptop->pnext;338     while(ptop){339         340         BitBlt (hdc, ptop->l*30+30,ptop->h*30+30  , 30, 30, temp_hdc, 0, 0, SRCCOPY) ;341         ptop=ptop->pnext;342     }343     DeleteObject(temp_hdc);344 345  }346 347  void pa(){348     Snake *jend=end;349     while(jend->plast!=NULL)350     {351         jend->h=jend->plast->h;352         jend->l=jend->plast->l;353 354         jend=jend->plast;355     }356     switch(g_fx)357      {358      case 0:359          top->l++;360          break;361      case 1:362          top->h--;363          break;364      case 2:365          top->l--;366          break;367      case 3:368          top->h++;369          break;370 371      }372  }373  boolean iseat(){374      if(top->h==apple.h &&top->l==apple.l )375          return TRUE;376      return FALSE;377  }378  void Newapple(){379      apple.l=rand()%18;380      apple.h=rand()%18;381  }382  boolean isziyao(){383      Snake *pbody=top->pnext;384      while(pbody!=NULL){385          if(top->h==pbody->h &&top->l==pbody->l )386              return TRUE;387          pbody=pbody->pnext;388      }389     return FALSE;390  }391  boolean iszhuang(){392      if(top->h>17||top->l<0||top->l>17||top->h<0)393     {394         return TRUE;395     }396      return FALSE;397  }

 

 

技术分享

 

 此时如果加上WM_MOUSEMOVE 就会变成了一纸笔

 1 case WM_MOUSEMOVE: 2         hdc = GetDC (hWnd) ; 3  4         x = LOWORD (lParam) ; 5         y = HIWORD (lParam) ; 6  7         LineTo (tmp_dc, x, y) ; 8         BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 9 10         ReleaseDC (hWnd, hdc) ;11 12         break;

 

 

 

技术分享

 

 在第一张图中,我们可以多次画很多条线,我们怎么才能在图面上显示一条线?

我们可以再次创建一个src_hdc和map,来存储原来的图片,每次画新的线的时候,我们都将其src给hdc,在通过hdc给tmp_hcd,这样就能保证每次画新的线的时候旧的线就没了

 1     case WM_LBUTTONDOWN: 2      3      4         hdc = GetDC (hWnd) ; 5             x = LOWORD (lParam) ; 6         y = HIWORD (lParam) ; 7         if(type==0){ 8             begin_x=x; 9             begin_y=y;10             src_dc=CreateCompatibleDC(hdc);11             src_bitmap=CreateCompatibleBitmap(hdc,620,630);12             SelectObject(src_dc,src_bitmap);13             BitBlt(src_dc,0,0,620,630,hdc,0,0,SRCCOPY);14             type=1;15         }16 17 18      //   Rectangle(hdc, 150,150,200,200);19 20         if(1==type){21             //创建临时的hdc和位图,并且把原图传过去22             BitBlt(hdc,0,0,620,630,src_dc,0,0,SRCCOPY);//把原hdc弄干净23             tmp_dc=CreateCompatibleDC(hdc);24             tmp_bitmap=CreateCompatibleBitmap(hdc,620,630);25             SelectObject(tmp_dc,tmp_bitmap);26             BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY);27 28     29             //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/30 31             MoveToEx (tmp_dc, x, y, NULL) ;32 33         }34         ReleaseDC (hWnd, hdc) ;35         break;36 37     case WM_LBUTTONUP:38         hdc = GetDC (hWnd) ;39 40         x = LOWORD (lParam) ;41         y = HIWORD (lParam) ;42 43 44         LineTo (tmp_dc, x, y) ;45         BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY);46         47 48 49         DeleteObject(tmp_dc);50         DeleteObject(tmp_bitmap);51         ReleaseDC (hWnd, hdc) ;52 53         break;

 

 

 

技术分享

 

 

加上下面的每次只能画一条随意弯曲的线

 1     //case WM_MOUSEMOVE: 2     //    hdc = GetDC (hWnd) ; 3  4     //    x = LOWORD (lParam) ; 5     //    y = HIWORD (lParam) ; 6  7     //    LineTo (tmp_dc, x, y) ; 8     //    BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 9 10     //    ReleaseDC (hWnd, hdc) ; 11 12     //    break;

技术分享

 

我们可以设置begin_x,begin_y来标定我们起始点,之后我们在moucemove里面画4根线

 

就画出了一个矩形

    case WM_MOUSEMOVE:        hdc = GetDC (hWnd) ;        x = LOWORD (lParam) ;        y = HIWORD (lParam) ;        BitBlt(tmp_dc,0,0,620,630,src_dc,0,0,SRCCOPY);        //1根线        MoveToEx (tmp_dc, begin_x, begin_y, NULL) ;        LineTo (tmp_dc,begin_x, y) ;        //2根        MoveToEx (tmp_dc, begin_x, begin_y, NULL) ;        LineTo (tmp_dc, x, begin_y) ;        //3根        MoveToEx (tmp_dc, x, begin_y, NULL) ;        LineTo (tmp_dc, x, y) ;        //4根        MoveToEx (tmp_dc, begin_x,y, NULL) ;        LineTo (tmp_dc, x, y) ;        BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY);        ReleaseDC (hWnd, hdc) ;         break;

 

 

 

 

技术分享

 

 

 下面就是画线不重复的代码

技术分享
  1 #include<Windows.h>  2 #include "resource.h"  3 #include <time.h>  4 typedef char boolean;  5 #define TRUE 1  6 #define FALSE 0  7   8   9  10  11  LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM); 12  13  typedef struct E{ 14     int h,l; 15     struct E *pnext; 16     struct E *plast; 17  }Apple,Snake,node; 18   19  Apple apple={5,6}; 20  Snake *top=NULL; 21  Snake *end=NULL; 22  23  24  25  26  27 HBITMAP h_back; 28 HBITMAP h_apple; 29 HBITMAP h_body; 30 HBITMAP h_l; 31 HBITMAP h_r; 32 HBITMAP h_u; 33 HBITMAP h_d; 34 int g_fx=1; 35 void add(int h,int l); 36 void initSnake(); 37 void show(HDC hdc); 38 void showApple(HDC hdc); 39 void showSnake(HDC hdc); 40  41 void pa(); 42 boolean iseat(); 43 void Newapple(); 44 boolean isziyao(); 45 boolean iszhuang(); 46  47  48  49  50  51  52 int WINAPI  WinMain(HINSTANCE hlnstance,HINSTANCE hPrelnstance, 53             LPSTR lpCmdLine,int nCmdShow){        //callback 54  55  56     //设计 57     HWND hwnd; 58     MSG  msg; 59     LPCSTR classname="牟柏旭"; 60     WNDCLASSEX ex; 61      62     ex.style=CS_HREDRAW | CS_VREDRAW; 63     ex.cbSize=sizeof(WNDCLASSEX); 64     ex.cbClsExtra=0; 65     ex.cbWndExtra=0; 66     ex.hbrBackground=CreateSolidBrush(RGB(171,171,226)); 67     ex.hCursor=NULL; 68     ex.hIcon=NULL; 69     ex.hIconSm=NULL; 70     ex.hInstance=hlnstance; 71     ex.lpszMenuName=0; 72     ex.lpszClassName=classname; 73     ex.lpfnWndProc=MyWndProc;  //消息过程函数  消息回调函数 74  75     //注册 76      RegisterClassEx(&ex); 77      //创建 78      hwnd= CreateWindow(classname,"mubaixu",WS_OVERLAPPEDWINDOW,50,50,620,630,NULL,NULL, 79             hlnstance,NULL); 80      SetTimer (hwnd, 1, 500, NULL) ; 81  82      //最后一个为是否创建多文档,比如code可以打开多个程序 83  84  85       ShowWindow(hwnd,nCmdShow); 86  87  88  89     h_back=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP1)); 90     h_apple=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP2)); 91     h_body=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP3)); 92     h_l=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP6)); 93     h_r=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP4)); 94     h_u=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP5)); 95     h_d=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP7)); 96     initSnake(); 97     srand((unsigned int )time(0)); 98       while(GetMessage(&msg,NULL,0,0)){ 99           100           TranslateMessage(&msg);101 102           DispatchMessage(&msg);103           //调用MyWndProc,系统调用104         105           //指定接收哪个窗口的消息,如果为NULL用于接收所有窗口106           // 后两个为过滤107       }108 109 110 111     return 0;112 }    113 114 int xx=200;115 HDC tmp_dc;116 HBITMAP tmp_bitmap;117 HDC src_dc;118 HBITMAP src_bitmap;119 int type=0;120 int begin_x=0;121 int begin_y=0;122 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){123     int x,y;124 125 126 //    char tx[10];127     //char ty[10];128     HDC hdc;129     //temp_hdc;130     //HBRUSH newbrush;131     //HBRUSH oldbrush;132     PAINTSTRUCT ps;133 134 //    HBITMAP hbitmap ;135 136     switch (Msg)137     {138 139     case WM_KEYDOWN:140             switch (wParam)141             {142                  case VK_UP:143                       g_fx=1;144                      break;145                  case VK_DOWN:146                        g_fx=3;147                      break;148                  case VK_RIGHT:149                      g_fx=0;150                      break;151                  case VK_LEFT:152                       g_fx=2;153                      break;154 155                 default:156                     break;157             }158         break;159 160     //case WM_MOUSEMOVE:161     //    hdc = GetDC (hWnd) ;162 163     //    x = LOWORD (lParam) ;164     //    y = HIWORD (lParam) ;165 166     //    if(type==1){167 168     //    BitBlt(tmp_dc,0,0,620,630,src_dc,0,0,SRCCOPY);169     //    //1根线170     //    MoveToEx (tmp_dc, begin_x, begin_y, NULL) ;171     //    LineTo (tmp_dc,begin_x, y) ;172 173     //    //2根174     //    MoveToEx (tmp_dc, begin_x, begin_y, NULL) ;175     //    LineTo (tmp_dc, x, begin_y) ;176     //    //3根177     //    MoveToEx (tmp_dc, x, begin_y, NULL) ;178     //    LineTo (tmp_dc, x, y) ;179     //    //4根180     //    MoveToEx (tmp_dc, begin_x,y, NULL) ;181     //    LineTo (tmp_dc, x, y) ;182 183 184     //    }185     //    BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY);186 187     //    ReleaseDC (hWnd, hdc) ; 188 189     //    break;190     case WM_LBUTTONDOWN:191     192     193         hdc = GetDC (hWnd) ;194             x = LOWORD (lParam) ;195         y = HIWORD (lParam) ;196         if(type==0){197             begin_x=x;198             begin_y=y;199             src_dc=CreateCompatibleDC(hdc);200             src_bitmap=CreateCompatibleBitmap(hdc,620,630);201             SelectObject(src_dc,src_bitmap);202             BitBlt(src_dc,0,0,620,630,hdc,0,0,SRCCOPY);203 204             type=1;205         }206 207 208      //   Rectangle(hdc, 150,150,200,200);209 210         if(1==type){211             //创建临时的hdc和位图,并且把原图传过去212             BitBlt(hdc,0,0,620,630,src_dc,0,0,SRCCOPY);//把原hdc弄干净213             tmp_dc=CreateCompatibleDC(hdc);214             tmp_bitmap=CreateCompatibleBitmap(hdc,620,630);215             SelectObject(tmp_dc,tmp_bitmap);216             BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY);217 218     219             //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/220 221             MoveToEx (tmp_dc, x, y, NULL) ;222 223         }224         ReleaseDC (hWnd, hdc) ;225         break;226 227     case WM_LBUTTONUP:228         hdc = GetDC (hWnd) ;229 230         x = LOWORD (lParam) ;231         y = HIWORD (lParam) ;232 233         if(type==1){234             //画线结束235             LineTo (tmp_dc, x, y) ;236             BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY);237             DeleteObject(tmp_dc);238             DeleteObject(tmp_bitmap);239             //抠图240         }241 242         ReleaseDC (hWnd, hdc) ;243 244         break;245     case WM_CLOSE:246         PostQuitMessage(0);247 248         break;249     250     case WM_PAINT:251         hdc = BeginPaint (hWnd, &ps) ;252 253         show(hdc);254 255         //Rectangle(hdc, 50,50,100,100);256         //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ;257         //temp_hdc = CreateCompatibleDC (hdc) ;258         //SelectObject(temp_hdc,h_back);    259         ////Rectangle(temp_hdc, 10,10,50,50);260     261         //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ;262 263         //xx+=20;264         //Ellipse( hdc, xx, 300, xx+70, 340);265 266           EndPaint (hWnd, &ps) ;267         break;268 269     case WM_TIMER:270 271 272 273         //pa();274         if(iseat()){275             Newapple();276             add(-20,-20);277         }278         if(isziyao()||iszhuang()){279             PostQuitMessage(0);280         }281 282         hdc = GetDC (hWnd) ;283             //show(hdc);重绘284         /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动285         oldbrush=(HBRUSH)SelectObject(hdc,newbrush);286          Rectangle( hdc, 0, 0,600,600);287           288         DeleteObject(newbrush);289         SelectObject(hdc,oldbrush);290         xx+=20;291         Ellipse( hdc, xx, 300, xx+70, 340);292             Rectangle(hdc, 50,50,100,100);*/293         ReleaseDC (hWnd, hdc) ;294         break;295     default:296         break;297     }298     //系统默认的消息处理函数299     return DefWindowProc(hWnd,Msg,wParam,lParam);300 }301 302 303 304  void add(int h,int l){305       node * pnode=(node *)malloc(sizeof(node));306       pnode->h=h;307       pnode->l=l;308       pnode->plast=NULL;309       pnode->pnext=NULL;310       if(top==NULL){311         top=pnode;312       }313       else{314           pnode->plast=end;315           end->pnext=pnode;316       }317 318       end=pnode;319  }320 321 322  void initSnake(){323     int i;324      for( i=8;i<=10;i++){325          add(i,10);326      }327 328  }329 330  void show(HDC hdc){331     HDC temp_hdc;332     temp_hdc = CreateCompatibleDC (hdc) ;333     SelectObject(temp_hdc,h_back);    334     335     BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ;336     DeleteObject(temp_hdc);337     showApple( hdc);338     showSnake( hdc);339  }340  void showApple(HDC hdc){341     HDC temp_hdc;342     temp_hdc = CreateCompatibleDC (hdc) ;343     SelectObject(temp_hdc,h_apple);    344     345     BitBlt (hdc, apple.l*30+30,apple.h*30+30, 30, 30, temp_hdc, 0, 0, SRCCOPY) ;346     DeleteObject(temp_hdc);347  }348  void showSnake(HDC hdc){349     HDC temp_hdc;350     Snake *ptop=top;351     temp_hdc = CreateCompatibleDC (hdc) ;352     switch (g_fx)353     {354 355     case 0:356         SelectObject(temp_hdc,h_r);    357         break;358     case 1:359         SelectObject(temp_hdc,h_u);    360         break;361     case 2:362         SelectObject(temp_hdc,h_l);    363         break;364     case 3:365         SelectObject(temp_hdc,h_d);    366         break;367     default:368         break;369     }370 371     372     BitBlt (hdc, top->l*30+30,top->h*30+30  , 30, 30, temp_hdc, 0, 0, SRCCOPY) ;373     DeleteObject(temp_hdc);374 375     376     temp_hdc = CreateCompatibleDC (hdc) ;377     SelectObject(temp_hdc,h_body);    378     ptop=ptop->pnext;379     while(ptop){380         381         BitBlt (hdc, ptop->l*30+30,ptop->h*30+30  , 30, 30, temp_hdc, 0, 0, SRCCOPY) ;382         ptop=ptop->pnext;383     }384     DeleteObject(temp_hdc);385 386  }387 388  void pa(){389     Snake *jend=end;390     while(jend->plast!=NULL)391     {392         jend->h=jend->plast->h;393         jend->l=jend->plast->l;394 395         jend=jend->plast;396     }397     switch(g_fx)398      {399      case 0:400          top->l++;401          break;402      case 1:403          top->h--;404          break;405      case 2:406          top->l--;407          break;408      case 3:409          top->h++;410          break;411 412      }413  }414  boolean iseat(){415      if(top->h==apple.h &&top->l==apple.l )416          return TRUE;417      return FALSE;418  }419  void Newapple(){420      apple.l=rand()%18;421      apple.h=rand()%18;422  }423  boolean isziyao(){424      Snake *pbody=top->pnext;425      while(pbody!=NULL){426          if(top->h==pbody->h &&top->l==pbody->l )427              return TRUE;428          pbody=pbody->pnext;429      }430     return FALSE;431  }432  boolean iszhuang(){433      if(top->h>17||top->l<0||top->l>17||top->h<0)434     {435         return TRUE;436     }437      return FALSE;438  }
View Code

 

抠图划线