模拟打地鼠游戏

 

#include<windows.h>
#include<time.h>
//#include "mmsystem.h"
#include "resource.h"
#include <stdio.h>
#pragma comment(lib,"msimg32.lib")
#pragma comment(lib,"winmm.lib")


HINSTANCE G_hInst=NULL; 

HDC G_BitMapBCDC=NULL;
HDC G_BitMapBKDC=NULL;
HDC G_BitMapHoleDC=NULL;
HDC G_BitMapMouseDC=NULL;
HDC G_BitMapBoomDC=NULL;

long CALLBACK WndProc(HWND hWnd, int message, int wParam, int lParam);
HDC LoadBitMapToDC(HINSTANCE hInst,char * fileName);
HDC LoadBitMapToDC(HINSTANCE hInst,char * fileName,long& width,long& height);

long BKWidth=800, BKHeight=533;
long HoleIndex=0;
long HoleWidth=0,HoleHeight=0;
long MouseWidth=0,MouseHeight=0;
long MouseX=0,MouseY=0;
long HoleX=0,HoleY=0;
static int i=0,score=0;
static int click=0,sumclick=0;
static int m=2;     //总共时间
static int second=1;   //剩余时间
double mzl=0.0;  //命中率


RECT ClientRect;

HDC LoadBitMapToDC(HINSTANCE hInst,char * fileName)
{
 HBITMAP hBitMap=(HBITMAP)LoadImage(hInst,fileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

 HDC hBitMapDC = ::CreateCompatibleDC(NULL);

    ::SelectObject(hBitMapDC,hBitMap);

 ::DeleteObject(hBitMap);

 return hBitMapDC;
}

HDC LoadBitMapToDC(HINSTANCE hInst,char * fileName,long& width,long& height)
{
  HBITMAP hBitMap =(HBITMAP)LoadImage(hInst, fileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);

     //获取位图的宽度和高度
  DIBSECTION dib;
  ::GetObject(hBitMap,sizeof(dib),&dib);
  BITMAPINFOHEADER bm=dib.dsBmih;
  width=bm.biWidth;
  height=bm.biHeight;

  HDC hBitMapDC=::CreateCompatibleDC(NULL);

  ::SelectObject(hBitMapDC,hBitMap);

  ::DeleteObject(hBitMap);

  return hBitMapDC;
}

//7、消息处理
long CALLBACK WndProc(HWND hWnd, int message, int wParam, int lParam)
{
 switch (message)
 {
     case WM_CREATE:
  {
   GetClientRect(hWnd,&ClientRect);
      SetTimer(hWnd,1,500,NULL);
   
           G_BitMapBKDC = LoadBitMapToDC(G_hInst,"bmp01.bmp",BKWidth,BKHeight);
     G_BitMapHoleDC = LoadBitMapToDC(G_hInst,"bmp02.bmp",HoleWidth,HoleHeight);
     G_BitMapMouseDC = LoadBitMapToDC(G_hInst,"bmp03.bmp",MouseWidth,MouseHeight);

    ::GetClientRect(hWnd,&ClientRect);
     break;
  }

  case WM_TIMER:
  {
   ::sndPlaySound("dodo.wav",SND_NOSTOP | SND_ASYNC);
   HDC hDc=::GetDC(hWnd);
   
   RECT rect;
   ::GetClientRect(hWnd,&rect);

   if(sumclick==0) sumclick=1;
   mzl=(double)click/sumclick*100;

   //伸展图片
      ::StretchBlt(hDc,0,0,
             rect.right-rect.left,
       rect.bottom-rect.top,
       G_BitMapBKDC,0,0,BKWidth,BKHeight,SRCCOPY); 
    
   //获取背景透明颜色
             int Color=::GetPixel(G_BitMapHoleDC,0,0);
   
   ::TransparentBlt(hDc,200,270,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,400,270,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,600,270,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,250,370,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,450,370,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,650,370,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,300,470,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,492,470,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
   ::TransparentBlt(hDc,700,470,HoleWidth,HoleHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,Color);
  
   //地鼠随机出现
   ::srand(time(NULL));

      HoleIndex = ::rand()%8;
   Color=::GetPixel(G_BitMapHoleDC,0,0);

   for(int i=0;i<9;i++)
   {
    switch(HoleIndex)
    {
     case 0: MouseX=200; MouseY=270; break;
     case 1: MouseX=400; MouseY=270; break;
     case 2: MouseX=600; MouseY=270; break;
     case 3: MouseX=250; MouseY=370; break;
                    case 4: MouseX=450; MouseY=370; break;
     case 5: MouseX=650; MouseY=370; break;
                    case 6: MouseX=300; MouseY=470; break;
     case 7: MouseX=492; MouseY=470; break;
                    case 8: MouseX=700; MouseY=470; break;
    }

    Color = ::GetPixel(G_BitMapMouseDC,0,0);
    ::TransparentBlt(hDc,MouseX,MouseY-10,MouseWidth,MouseHeight,G_BitMapMouseDC,0,0,MouseWidth,MouseHeight,Color);
   }

   char a[10];  //限定时间
   char b[10];  //剩余时间
   char c[10];  //命中率
   char d[10];  //得分

   sprintf(a," %d 分",m);
   SetBkColor(hDc,RGB(255,255,0));
   TextOut(hDc,142,63,a,strlen(a));

   second--;
   sprintf(b," %02d 秒",second);
   TextOut(hDc,132,135,b,strlen(b));

   if(second<0)
   {
    int result; 
    KillTimer(hWnd,1);
    result = MessageBox(NULL,"GOME OVER /n/n点确定后/n再按Enter键可重新开始","游戏结束",MB_OK);
    second=120;  
    score=0;
    click=0;
    sumclick=0;
   }
  
   

   sprintf(c," %.2lf%%",mzl);
   TextOut(hDc,122,212,c,strlen(c));
   
   sprintf(d,"%2d",score);
   TextOut(hDc,98,290,d,strlen(d));
 
   ReleaseDC(hWnd,hDc); 
   break;
  }
  case WM_KEYDOWN:
  {
   switch(wParam)
   {
    case VK_RETURN: SetTimer(hWnd,1,1000,NULL);

    break;
   }
  }
  case WM_LBUTTONDOWN:
  {
   int X=LOWORD(lParam);
   int Y = HIWORD(lParam);

   HDC hDc=::GetDC(hWnd);
      if(X>=MouseX && X<=MouseX+MouseWidth && Y>=MouseY && Y<=MouseY+MouseHeight)
   {
    ::sndPlaySound("habi.wav", SND_ASYNC | SND_SYNC);
    Sleep(500);
    ::TransparentBlt(hDc,MouseX,MouseY,MouseWidth,MouseHeight,G_BitMapHoleDC,0,0,HoleWidth,HoleHeight,NULL);
    score+=5;
    click++; 
   }
   else
   {
    ::TransparentBlt(hDc,MouseX,MouseY,MouseWidth,MouseHeight,G_BitMapMouseDC,0,0,HoleWidth,HoleHeight,NULL);
    ::sndPlaySound("bfire.wav", SND_ASYNC | SND_SYNC);
   }
   
   sumclick++;
   break;
  }
 
  case WM_SIZE:
  {
   ::GetClientRect(hWnd,&ClientRect);
   break;
  }
  case WM_DESTROY:
  {
  
   ::DeleteObject(G_BitMapBKDC);
   G_BitMapBKDC=NULL;
   ::DeleteObject(G_BitMapHoleDC);
   G_BitMapHoleDC=NULL;
   
   PostQuitMessage(0);
   ::KillTimer(hWnd,1);

   break;
  }
 
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

int _stdcall WinMain(HINSTANCE hInstance,
    HINSTANCE hPrelnstance,
    LPSTR lpCmdLine,
    int nShowCmd)
{
    G_hInst = hInstance;

 //1、设置窗体属性
 WNDCLASSEX wcex;
   
 // 结构体的大小
 wcex.cbSize = sizeof(WNDCLASSEX);
 // 窗体类的样式
 wcex.style = CS_HREDRAW | CS_VREDRAW;
 // *窗体回调函数
 wcex.lpfnWndProc = (WNDPROC)WndProc;
   // 窗体类额外字节数,通常为0
 wcex.cbClsExtra  = 0;
 // 窗体实例额外字节数,通常为0
 wcex.cbWndExtra  = 0; 
 // *应用程序实例句柄
 wcex.hInstance  = hInstance;     
 wcex.hIcon   = NULL;       // 窗体大图标
 wcex.hCursor  = LoadCursor(NULL,IDC_NO);//LoadIcon( G_hInst, MAKEINTRESOURCE(IDI_ICON1) );//::LoadIcon(G_hInst, "IDI_MYICON");       // // 窗体鼠标光标样式
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+7);  // 窗体背景色
 wcex.lpszMenuName = NULL;       // 窗体主菜单
 // *窗体类名称
 wcex.lpszClassName = "Bekty";     
 wcex.hIconSm  =NULL;//LoadIcon( G_hInst, MAKEINTRESOURCE(IDI_ICON2) );// NULL;//LoadIcon(G_hInst,"IDI_MYICON");       // 窗体小图标
 
 //2、注册窗体
 RegisterClassEx(&wcex);

 //3、创建窗体
 HWND hWnd = CreateWindow(
  "Bekty",    // *窗体类名称
  "地鼠游戏城",    // 窗体标题名
  WS_OVERLAPPEDWINDOW, // 窗体外观样式
  0,   // 窗体在屏幕上的水平位置
  0,      // 窗体在屏幕上的垂直位置
  CW_USEDEFAULT,   // 窗口的宽度
  0,      // 窗口的高度
  NULL,     // 父窗体句柄
  NULL,     // 窗口菜单句柄
  hInstance,    // *创建窗体的应用程序实例句柄
  NULL);     // 创建窗体时的额外信息,通常为NULL
 
 if (NULL == hWnd)
 {
  return FALSE;
 }

 //4、显示窗体
 ShowWindow(hWnd, nShowCmd);

 //5、 更新窗体
 UpdateWindow(hWnd);

 //6、主消息循环
 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0)) // 从消息队列中获取消息
 {
  TranslateMessage(&msg);  // 转换消息
  DispatchMessage(&msg);  // 分发消息给回调函数
 }

 return msg.wParam;
 
}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值