⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 扫雷过三关2dlg.cpp

📁 区别于微软的扫雷,共有3关,其中添加了很多有趣的元素
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 扫雷过三关2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "扫雷过三关2.h"
#include "扫雷过三关2Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy2Dlg dialog
CString name;

CMy2Dlg::CMy2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy2Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy2Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy2Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy2Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_DESTROY()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy2Dlg message handlers

BOOL CMy2Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	a=200;
	b=0;
	level=1; 
	life=2;
    total=0;
	score=0;
	push=false;
	pushsound=false;
	startgame=false;
////////////////////////////
	cdc1.CreateCompatibleDC(0);  //创建与指定设备兼容的内存设备上下文环境
    cdc2.CreateCompatibleDC(0);
	backp=new CBitmap;
	time_scorep=new CBitmap;
	buttonp=new CBitmap; 
	buttonp2=new CBitmap; 
	bugfood=new CBitmap;  
	map=new CBitmap;
	numberp=new CBitmap;
	temp=new CBitmap;  //空位图

//===============载入位图================================
	CClientDC  dc(this);  //建立当前屏幕设备环境
   
	buttonp->m_hObject=(HBITMAP)::LoadImage(NULL,"..\\扫雷过三关2\\Resource\\块.bmp",IMAGE_BITMAP,68,119,
           LR_LOADFROMFILE);
	buttonp2->m_hObject=(HBITMAP)::LoadImage(NULL,"..\\扫雷过三关2\\Resource\\push.bmp",IMAGE_BITMAP,102,17,
           LR_LOADFROMFILE);
	backp->m_hObject=(HBITMAP)::LoadImage(NULL,"..\\扫雷过三关2\\Resource\\1.bmp",IMAGE_BITMAP,160,80,
           LR_LOADFROMFILE);
    time_scorep->m_hObject=(HBITMAP)::LoadImage(NULL,"..\\扫雷过三关2\\Resource\\time_score.bmp",IMAGE_BITMAP,130,22,
           LR_LOADFROMFILE);
	temp->CreateCompatibleBitmap(&dc,300,200); //创建与指定的设备环境相关的设备兼容的位图
//========================================================
	mFile.Open("..\\扫雷过三关2\\ReadMe.txt",CFile::modeRead,&mExcept);  //读方式打开
	newFont.CreateFont(-25,0,0,0,FW_NORMAL,  
	false,false,false,
	GB2312_CHARSET,OUT_DEFAULT_PRECIS,
	CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
	DEFAULT_PITCH | FF_MODERN,"楷体");  //创建一种有特殊性的逻辑字体
	SetTimer(1,50,NULL);	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMy2Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMy2Dlg::OnOK() 
{
	if(startgame==false)
	{
		startgame=true;
		KillTimer(1);
		mFile.Close();
		initialize(level);
	}
}

void CMy2Dlg::OnCancel() 
{
	CDialog::OnCancel();
}

void CMy2Dlg::OnTimer(UINT nIDEvent) 
{
	b+=3;  //字幕上移步长
//========================关于=====================================
	if(nIDEvent==1)  
	{
		CClientDC  dc(this);  //建立当前屏幕设备环境
		CRect rect;
	    GetWindowRect(rect);
		MoveWindow(rect.left,rect.top,480,320);
        
        cdc1.SelectObject(temp);
		cdc2.SelectObject(backp);  //选入背景图
		dc.StretchBlt(0,0,480,320,&cdc2,0,0,160,80, SRCCOPY); //把原图放大
	
//显示字体	
    dc.SelectObject(&newFont);  //选择一对象到指定的设备上下文环境中
	dc.SetTextColor(RGB(255,0,0));  //设置字体颜色
	dc.SetBkMode(TRANSPARENT);      //设置字体背景模式(透明)
	 while(mFile.ReadString(S))  //ReadString是读取每一行 
	 {		
	    dc.TextOut(10,a-b,S,lstrlen(S));  //用当前选择字符、背景颜色和正文颜色将一个字符串写到指定位置。
	    a+=30;  //下一行的显示位置
	 }
	 a=200;
     mFile.SeekToBegin();
	 if(b>47*30+200){b=0;} 	
	}
//========================进入游戏============================
	if(nIDEvent==2)  
	{	
		//自动打开空位
       Zd(tx,ty);	
	//贴图函数调用
	   Bitblt(tx,ty,ln,col);
	}
	if(nIDEvent==3)  //计时
	{
		timeadd++;
		if(timeadd>=999){KillTimer(3);}
        int a=timeadd/100;  //百位
		int b=(timeadd%100)/10;   //十位 
		int c=(timeadd%100)%10;  //个位

		cdc2.SelectObject(time_scorep);  //
        cdc1.StretchBlt(140,10,13,17,&cdc2,13*a,0,13,22,SRCCOPY);		
        cdc1.StretchBlt(156,10,13,17,&cdc2,13*b,0,13,22,SRCCOPY);
        cdc1.StretchBlt(172,10,13,17,&cdc2,13*c,0,13,22,SRCCOPY);
		if(pass==true)
		{
			KillTimer(3);
			level++;
			if(level==2)
			{MessageBox("进入第二关!","祝贺你通过!");}
            if(level==3){MessageBox("最后一关啦!加油!","祝贺你通过!");}
			if(level>3)
			{
				win=true;
                level=1;
			    MessageBox("祝贺你爆机啦!","厉害!");
			    if(MessageBox("按“确定”稍候将进入秘密空间!","秘密",MB_ICONWARNING && MB_OKCANCEL)==1)							
				{Secret();}
				MessageBox("进入第一关!","祝贺你通过!");
                WriteScore();
			    KillTimer(2);
			}
            initialize(level);
		}
		if(lose==true)
		{
			KillTimer(3);
            WriteScore();
		}
	}
	CDialog::OnTimer(nIDEvent);
}


void CMy2Dlg::initialize(short level)
{
	CClientDC  dc(this);  //建立当前屏幕设备环境
	tx=0,ty=0;
	pass=false;
	lose=false;
	win=false;
	col=0;
	ln=0;
	timeadd=0;
  	HighScore();
    if(push==true){score=0;}
//===============建立数组
   CRect rect;
   GetWindowRect(rect);
   MoveWindow(rect.left,rect.top,MAPW,MAPH+25);  //设置窗体位置、大小
   CenterWindow();
	if(level==1)  
	{	
        bug=10;
		food=10;
	}
	else if(level==2)
	{
	    bug=20;
		food=15;  
	}
	else
	{
	    bug=rand() % 11+30;  //产生30~40间的随机数
	    food=rand() % 11+15;  //产生15~25间的随机数
	}
//--------------初始化数组-------------------------
	    for(j=0;j<GY;j++)
		{
		    for(i=0;i<GX;i++)
			{
				mapbutton[i][j]=0;  //(0未开1旗2问号3按下4开)
				map1[i][j]=0;  //地图为初始状态(0无1食物2雷3戒指)
			    mapnb[i][j]=0;
			}
		}
		map->m_hObject=(HBITMAP)::LoadImage(NULL,"..\\扫雷过三关2\\Resource\\map.bmp",IMAGE_BITMAP,MAPW,MAPH,
			   LR_LOADFROMFILE);
		cdc1.SelectObject(map);  //选入地图

      Sj(level,bug,food);  //随机分布函数调用

    SetTimer(2,10,NULL);//
	SetTimer(3,1000,NULL);  //建立时钟
      Equit();//地图数字计算函数调用
}

void CMy2Dlg::Sj(short level, short bug, short food)
{
     short a=0,b=0;  //统计分配的食物、地雷数
     bool ok;  //分配好了吗

     srand((unsigned)time(NULL));    //产生随机数种子
//产生食物种类
     fkind=rand() % 4;             //r的范围在0~3间 
//产生雷种类 
     bkind=rand() % 4;
//产生奖品种类
	 if(level>=2)
     {
		 gkind=rand() % 5;  //产生奖品种类的不同几率(0奖杯20%,其余为戒指80%)
	 }
//随机布雷
	 do
	 {
		 int x=rand()%16;
		 int y=rand()%16;

		 map1[x][y]=2;    //为随机产生的地图块附值2(布雷)
         a=0;
		 for(j=0;j<16;j++)  //检查是否重复附值
		 {
		     for(i=0;i<16;i++)
			 {
			     if(map1[i][j]==2)
				 {a++;}
			     if(a==bug) {break;}
			 }	
			 if(a==bug){break;}
		 }		 
	 }while(a<bug);
//随机布食物
do
	 {
		 j=rand()%16;
		 i=rand()%16;

		 for(int k=0;k<16;k++)  //对未附值的地图块附值
		 {
		     for(int h=0;h<16;h++)
			 {
			     if(map1[i][j]==0)
				 {map1[i][j]=1;}
			 }	
		 }    
         b=0;
		 for(j=0;j<16;j++)  //检查是否重复附值
		 {
		     for(i=0;i<16;i++)
			 {
			     if(map1[i][j]==1)
				 {b++;}
				 if(b==food){break;}
			 }	
			 if(b==food){break;}
		 }		 
	 }while(b<food);
//随机布奖品
	 if(level>=2)  //2级以上
	 {
		 do
		 {
			 int x=rand()%16;
			 int y=rand()%16;

			 if(map1[x][y]==0)
			 {
				 map1[x][y]=3;
				 ok=true;
			 }
			 else{ok=false;}
		 }while(ok==false);
	 }
}

void CMy2Dlg::Equit()
{
    for(j=0;j<GY;j++)  
	{
	    for(i=0;i<GX;i++)
		{
			if(map1[i][j]==0)
			{
			    if(map1[i][j-1]==2 && j-1>=0){mapnb[i][j]++;} 
				if(map1[i][j+1]==2 && j+1<16){mapnb[i][j]++;}
				if(map1[i-1][j]==2 && i-1>=0){mapnb[i][j]++;}
				if(map1[i+1][j]==2 && i+1<16){mapnb[i][j]++;}

				if(map1[i-1][j-1]==2 && i-1>=0 && j-1>=0){mapnb[i][j]++;}
				if(map1[i+1][j-1]==2 && i+1<16 && j-1>=0){mapnb[i][j]++;}
				if(map1[i-1][j+1]==2 && i-1>=0 && j+1<16){mapnb[i][j]++;}
				if(map1[i+1][j+1]==2 && i+1<16 && j+1<16){mapnb[i][j]++;}			
			}
		}
	}
}

void CMy2Dlg::Zd(int tx, int ty)
{
		CClientDC  dc(this);
	cdc2.SelectObject(buttonp);  //选入地图块
    for(j=0;j<GY;j++)
	{
	    for(i=0;i<GX;i++)
		{
	       if(mapbutton[i][j]==4 && map1[i][j]==0 && mapnb[i][j]==0)  //打开的按钮是空的
		   {
		      	if(map1[i][j-1]==0 && j-1>=0)
				{
					mapbutton[i][j-1]=4;
				    cdc1.BitBlt(11+i*BSIZE,58+(j-1)*BSIZE,17,17,&cdc2,17,0,SRCCOPY);
				} 
				if(map1[i][j+1]==0 && j+1<16)
				{
					mapbutton[i][j+1]=4;
                    cdc1.BitBlt(11+i*BSIZE,58+(j+1)*BSIZE,17,17,&cdc2,17,0,SRCCOPY);				
				}
				if(map1[i-1][j]==0 && i-1>=0)
				{
					mapbutton[i-1][j]=4;
                    cdc1.BitBlt(11+(i-1)*BSIZE,58+j*BSIZE,17,17,&cdc2,17,0,SRCCOPY);			
				}
				if(map1[i+1][j]==0 && i+1<16)
				{
					mapbutton[i+1][j]=4;
                    cdc1.BitBlt(11+(i+1)*BSIZE,58+j*BSIZE,17,17,&cdc2,17,0,SRCCOPY);							
				}
		   }
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -