zoukankan      html  css  js  c++  java
  • 贪吃蛇

      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 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){
    116 //    int x,y;
    117 
    118 
    119 //    char tx[10];
    120     //char ty[10];
    121     HDC hdc;
    122     //temp_hdc;
    123     //HBRUSH newbrush;
    124     //HBRUSH oldbrush;
    125     PAINTSTRUCT ps;
    126 
    127 //    HBITMAP hbitmap ;
    128 
    129     switch (Msg)
    130     {
    131 
    132     case WM_KEYDOWN:
    133             switch (wParam)
    134             {
    135                  case VK_UP:
    136                       g_fx=1;
    137                      break;
    138                  case VK_DOWN:
    139                        g_fx=3;
    140                      break;
    141                  case VK_RIGHT:
    142                      g_fx=0;
    143                      break;
    144                  case VK_LEFT:
    145                       g_fx=2;
    146                      break;
    147 
    148                 default:
    149                     break;
    150             }
    151         break;
    152 
    153     case WM_LBUTTONDOWN:
    154         /*hdc = GetDC (hWnd) ;
    155         
    156         Rectangle(hdc, 150,150,200,200);
    157         ReleaseDC (hWnd, hdc) ;*/
    158 
    159 
    160     /*    x = LOWORD (lParam) ;
    161         y = HIWORD (lParam) ;
    162         MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/
    163         break;
    164 
    165 
    166     case WM_CLOSE:
    167         PostQuitMessage(0);
    168 
    169         break;
    170     
    171     case WM_PAINT:
    172         hdc = BeginPaint (hWnd, &ps) ;
    173 
    174         show(hdc);
    175 
    176         //Rectangle(hdc, 50,50,100,100);
    177         //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ;
    178         //temp_hdc = CreateCompatibleDC (hdc) ;
    179         //SelectObject(temp_hdc,h_back);    
    180         ////Rectangle(temp_hdc, 10,10,50,50);
    181     
    182         //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ;
    183 
    184         //xx+=20;
    185         //Ellipse( hdc, xx, 300, xx+70, 340);
    186 
    187           EndPaint (hWnd, &ps) ;
    188         break;
    189 
    190     case WM_TIMER:
    191 
    192 
    193 
    194         pa();
    195         if(iseat()){
    196             Newapple();
    197             add(-20,-20);
    198         }
    199         if(isziyao()||iszhuang()){
    200             PostQuitMessage(0);
    201         }
    202 
    203         hdc = GetDC (hWnd) ;
    204             show(hdc);
    205         /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动
    206         oldbrush=(HBRUSH)SelectObject(hdc,newbrush);
    207          Rectangle( hdc, 0, 0,600,600);
    208           
    209         DeleteObject(newbrush);
    210         SelectObject(hdc,oldbrush);
    211         xx+=20;
    212         Ellipse( hdc, xx, 300, xx+70, 340);
    213             Rectangle(hdc, 50,50,100,100);*/
    214         ReleaseDC (hWnd, hdc) ;
    215         break;
    216     default:
    217         break;
    218     }
    219     //系统默认的消息处理函数
    220     return DefWindowProc(hWnd,Msg,wParam,lParam);
    221 }
    222 
    223 
    224 
    225  void add(int h,int l){
    226       node * pnode=(node *)malloc(sizeof(node));
    227       pnode->h=h;
    228       pnode->l=l;
    229       pnode->plast=NULL;
    230       pnode->pnext=NULL;
    231       if(top==NULL){
    232         top=pnode;
    233       }
    234       else{
    235           pnode->plast=end;
    236           end->pnext=pnode;
    237       }
    238 
    239       end=pnode;
    240  }
    241 
    242 
    243  void initSnake(){
    244     int i;
    245      for( i=8;i<=10;i++){
    246          add(i,10);
    247      }
    248 
    249  }
    250 
    251  void show(HDC hdc){
    252     HDC temp_hdc;
    253     temp_hdc = CreateCompatibleDC (hdc) ;
    254     SelectObject(temp_hdc,h_back);    
    255     
    256     BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ;
    257     DeleteObject(temp_hdc);
    258     showApple( hdc);
    259     showSnake( hdc);
    260  }
    261  void showApple(HDC hdc){
    262     HDC temp_hdc;
    263     temp_hdc = CreateCompatibleDC (hdc) ;
    264     SelectObject(temp_hdc,h_apple);    
    265     
    266     BitBlt (hdc, apple.l*30+30,apple.h*30+30, 30, 30, temp_hdc, 0, 0, SRCCOPY) ;
    267     DeleteObject(temp_hdc);
    268  }
    269  void showSnake(HDC hdc){
    270     HDC temp_hdc;
    271     Snake *ptop=top;
    272     temp_hdc = CreateCompatibleDC (hdc) ;
    273     switch (g_fx)
    274     {
    275 
    276     case 0:
    277         SelectObject(temp_hdc,h_r);    
    278         break;
    279     case 1:
    280         SelectObject(temp_hdc,h_u);    
    281         break;
    282     case 2:
    283         SelectObject(temp_hdc,h_l);    
    284         break;
    285     case 3:
    286         SelectObject(temp_hdc,h_d);    
    287         break;
    288     default:
    289         break;
    290     }
    291 
    292     
    293     BitBlt (hdc, top->l*30+30,top->h*30+30  , 30, 30, temp_hdc, 0, 0, SRCCOPY) ;
    294     DeleteObject(temp_hdc);
    295 
    296     
    297     temp_hdc = CreateCompatibleDC (hdc) ;
    298     SelectObject(temp_hdc,h_body);    
    299     ptop=ptop->pnext;
    300     while(ptop){
    301         
    302         BitBlt (hdc, ptop->l*30+30,ptop->h*30+30  , 30, 30, temp_hdc, 0, 0, SRCCOPY) ;
    303         ptop=ptop->pnext;
    304     }
    305     DeleteObject(temp_hdc);
    306 
    307  }
    308 
    309  void pa(){
    310     Snake *jend=end;
    311     while(jend->plast!=NULL)
    312     {
    313         jend->h=jend->plast->h;
    314         jend->l=jend->plast->l;
    315 
    316         jend=jend->plast;
    317     }
    318     switch(g_fx)
    319      {
    320      case 0:
    321          top->l++;
    322          break;
    323      case 1:
    324          top->h--;
    325          break;
    326      case 2:
    327          top->l--;
    328          break;
    329      case 3:
    330          top->h++;
    331          break;
    332 
    333      }
    334  }
    335  boolean iseat(){
    336      if(top->h==apple.h &&top->l==apple.l )
    337          return TRUE;
    338      return FALSE;
    339  }
    340  void Newapple(){
    341      apple.l=rand()%18;
    342      apple.h=rand()%18;
    343  }
    344  boolean isziyao(){
    345      Snake *pbody=top->pnext;
    346      while(pbody!=NULL){
    347          if(top->h==pbody->h &&top->l==pbody->l )
    348              return TRUE;
    349          pbody=pbody->pnext;
    350      }
    351     return FALSE;
    352  }
    353  boolean iszhuang(){
    354      if(top->h>17||top->l<0||top->l>17||top->h<0)
    355     {
    356         return TRUE;
    357     }
    358      return FALSE;
    359  }

    资源文件

    //{{NO_DEPENDENCIES}}
    // Microsoft Visual C++ 生成的包含文件。
    // 供 win32.rc 使用
    //
    #define IDB_BITMAP1                     101
    #define IDB_BITMAP2                     102
    #define IDB_BITMAP3                     103
    #define IDB_BITMAP4                     104
    #define IDB_BITMAP5                     105
    #define IDB_BITMAP6                     106
    #define IDB_BITMAP7                     107
    
    // Next default values for new objects
    // 
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_NEXT_RESOURCE_VALUE        108
    #define _APS_NEXT_COMMAND_VALUE         40001
    #define _APS_NEXT_CONTROL_VALUE         1001
    #define _APS_NEXT_SYMED_VALUE           101
    #endif
    #endif

  • 相关阅读:
    服务端跳转和客户端跳转的区别
    jsp:include标签与include指令的区别
    jsp错误页不跳转显示500
    使用Cookie进行会话管理
    深入理解重定向和转发
    appium环境搭建(二)----搭建android开发环境
    appium环境搭建(一)----安装appium
    Fiddler实现移动端手机抓包
    黑盒测试人员必备技能
    SVN服务器搭建
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/5865354.html
Copyright © 2011-2022 走看看