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

📄 扫雷过三关2dlg.cpp

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

void CMy2Dlg::OnLButtonDown(UINT nFlags, CPoint point) 
{    
	if(startgame)
	{
		if(point.x<=W || point.x>=W+(GX*BSIZE) || point.y<=H || point.y>=H+(GY*BSIZE))  //去除地图范围外
		{
			if(point.x>114 && point.x<146 && point.y>34 && point.y<50)  //在RESTART按钮上
			{
				life=2;
				level=1;
	            total=0;
				score=0;
                push=true;
			    initialize(1);
			}
			else if(point.x>170 && point.x<187 && point.y>34 && point.y<50)  //在SOUND按钮上
			{
				if(pushsound)
				{pushsound=false;}
				else{pushsound=true;}
			}
			else{return;}
		}
		else
		{		
		//转换坐标为地图索引值
			int cx=int(point.x-W)/BSIZE;
			int cy=int(point.y-H)/BSIZE;

			if(pushsound==false){Sound(1,cx,cy);}
			switch(mapbutton[cx][cy])
			{
			case 0:  //未开
                col=2;
	            ln=6;
				mapbutton[cx][cy]=3;
				tx=cx;
				ty=cy;
				statistics();  //开始统计分数
				break;
			case 1:  //已有红旗
				MessageBox("此块已锁定!");
				break;
			case 2:  //已有问号
                col=2;
	            ln=6;
				mapbutton[cx][cy]=3;
				tx=cx;
				ty=cy;
				statistics();  //开始统计分数
				break;
             case 3:  //按下
                col=2;
	            ln=6;
				break;
			 case 4:
				 break;
			}
		}
	}
	CDialog::OnLButtonDown(nFlags, point);
}

void CMy2Dlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(startgame)
	{
		if(point.x>114 && point.x<146 && point.y>34 && point.y<50)  //在RESTART按钮上
		{push=false;}
		for(j=0;j<GY;j++)
		{
			for(i=0;i<GX;i++)
			{
				if(mapbutton[i][j]==3)
				{
					mapbutton[i][j]=4;
					col=1;
					ln=0;
					if(map1[tx][ty]==2)  //触雷了!
					{
						life--;
					}
				}
			}
		}
	}
	CDialog::OnLButtonUp(nFlags, point);
}

void CMy2Dlg::Bitblt(short tx, short ty, short ln, short col)
{
	CClientDC  dc(this);  //建立当前屏幕设备环境
	    //贴RESTART\SOUND图
            cdc2.SelectObject(buttonp2);
			if(pushsound)
            {cdc1.BitBlt(170,34,17,17,&cdc2,66+17,0,SRCCOPY);}
			else
			{cdc1.BitBlt(170,34,17,17,&cdc2,66,0,SRCCOPY);}
			if(push)
			{
			   cdc1.BitBlt(114,34,33,17,&cdc2,33,0,SRCCOPY); 
			}
			else{cdc1.BitBlt(114,34,33,17,&cdc2,0,0,SRCCOPY);}
		//根据按钮状态贴按钮图
			cdc2.SelectObject(buttonp);  //选入地图块 
		    cdc1.BitBlt(11+tx*BSIZE,58+ty*BSIZE,17,17,&cdc2,17*col,17*ln,SRCCOPY);
		//贴上食物和地雷种类图
            cdc1.BitBlt(11,9,17,17,&cdc2,17*fkind,17*3,SRCCOPY);
            cdc1.BitBlt(11,34,17,17,&cdc2,17*bkind,17*4,SRCCOPY);
	    //贴上奖品种类图 
			if(level>=2)
			{
                if(gkind==0)
				{
					cdc1.BitBlt(153,34,17,17,&cdc2,0,17*6,SRCCOPY);			    
				}
				else
				{
					cdc1.BitBlt(153,34,17,17,&cdc2,17,17*6,SRCCOPY);
			        gkind=1;
				}
			}
		//检查打开的按钮并贴上食物或者地雷
			if(mapbutton[tx][ty]==4)  //打开了按钮
			{
			    if(map1[tx][ty]==1)//(0无1食物2雷3奖品)
				{
					cdc1.BitBlt(11+tx*BSIZE,58+ty*BSIZE,17,17,&cdc2,17*fkind,17*3,SRCCOPY);
				}
				else if(map1[tx][ty]==3)
				{
					cdc1.BitBlt(11+tx*BSIZE,58+ty*BSIZE,17,17,&cdc2,17*gkind,17*6,SRCCOPY);		
				}
				else if(map1[tx][ty]==2 && life<0)  //触雷了!
				{
						lose=true;
						pass=false;
					//找出并贴上所有食物和地雷
						for(j=0;j<16;j++)
						{
							for(i=0;i<16;i++)
							{
								if(map1[i][j]==1 && mapbutton[i][j]!=1)
								{cdc1.BitBlt(11+i*BSIZE,58+j*BSIZE,17,17,&cdc2,17*fkind,17*3,SRCCOPY);}
								if(map1[i][j]==2 && mapbutton[i][j]!=1)
								{cdc1.BitBlt(11+i*BSIZE,58+j*BSIZE,17,17,&cdc2,17*bkind,17*4,SRCCOPY);}
								if(map1[i][j]==3 && mapbutton[i][j]!=1)
								{cdc1.BitBlt(11+i*BSIZE,58+j*BSIZE,17,17,&cdc2,17*gkind,17*6,SRCCOPY);}					
							}
						}
						cdc1.BitBlt(11+tx*BSIZE,58+ty*BSIZE,17,17,&cdc2,17*bkind,17*5,SRCCOPY);				
				}
				else if(map1[tx][ty]==2){cdc1.BitBlt(11+tx*BSIZE,58+ty*BSIZE,17,17,&cdc2,17*bkind,17*5,SRCCOPY);}
				else {cdc1.BitBlt(11+tx*BSIZE,58+ty*BSIZE,17,17,&cdc2,17*1,0,SRCCOPY);}
			}
//检查打开的按钮并贴上地图数字	
	for(j=0;j<16;j++)
	{
	    for(i=0;i<16;i++)
		{
			if(mapbutton[i][j]==4 && map1[i][j]==0)  //打开了地图下是空的按钮
			{		
				if(mapnb[i][j]<=4)  //数字在4以内
				{
					cdc1.BitBlt(11+i*BSIZE,58+j*BSIZE,17,17,&cdc2,17*(mapnb[i][j]-1),17,SRCCOPY);
				}
				else
				{
				    cdc1.BitBlt(11+i*BSIZE,58+j*BSIZE,17,17,&cdc2,17*(mapnb[i][j]-5),17*2,SRCCOPY);
				}
			}		    
		}
	}
		//检查并统计分数
			//贴上食物和地雷数字
				cdc2.SelectObject(time_scorep); 
				int bg1=bug/10;   //十位 
				int bg2=bug%10;  //个位
                int fd1=food/10;   //十位 
				int fd2=food%10;  //个位
				cdc1.StretchBlt(30,9,13,17,&cdc2,13*fd1,0,13,22,SRCCOPY); //贴上十位食物数字
				cdc1.StretchBlt(45,9,13,17,&cdc2,13*fd2,0,13,22,SRCCOPY); //贴上个位食物数字
				cdc1.StretchBlt(30,34,13,17,&cdc2,13*bg1,0,13,22,SRCCOPY);  //贴上十位地雷数字
				cdc1.StretchBlt(45,34,13,17,&cdc2,13*bg2,0,13,22,SRCCOPY);  //贴上个位地雷数字
				cdc1.StretchBlt(93,34,13,17,&cdc2,13*level,0,13,22,SRCCOPY);  //贴上级别数字
                cdc1.StretchBlt(93,9,13,17,&cdc2,13*life,0,13,22,SRCCOPY);  //贴上生命数字
				//过关
				if(pass==true)
				{			
					cdc2.SelectObject(time_scorep);
					cdc1.StretchBlt(30,9,13,17,&cdc2,0,0,13,22,SRCCOPY); //食物数十位归0
					cdc1.StretchBlt(45,9,13,17,&cdc2,0,0,13,22,SRCCOPY); //食物数个位归0
				}

				int a=score/100;  //百位
				int b=(score%100)/10;   //十位 
				int c=(score%100)%10;  //个位
                cdc1.StretchBlt(222,34,13,17,&cdc2,13*a,0,13,22,SRCCOPY);  //贴上百位所得分数
				cdc1.StretchBlt(238,34,13,17,&cdc2,13*b,0,13,22,SRCCOPY);  //贴上十位所得分数
				cdc1.StretchBlt(254,34,13,17,&cdc2,13*c,0,13,22,SRCCOPY);  //贴上个位所得分数
               //贴上纪录分
				int d=hscore/100;  //百位
				int e=(hscore%100)/10;   //十位 
				int f=(hscore%100)%10;  //个位
                cdc1.StretchBlt(222,9,13,17,&cdc2,13*d,0,13,22,SRCCOPY);  //贴上百位所得分数
				cdc1.StretchBlt(238,9,13,17,&cdc2,13*e,0,13,22,SRCCOPY);  //贴上十位所得分数
				cdc1.StretchBlt(254,9,13,17,&cdc2,13*f,0,13,22,SRCCOPY);  //贴上个位所得分数
//把最终结果贴到屏幕 
	dc.BitBlt(0,0,MAPW,MAPH,&cdc1,0,0,SRCCOPY);
}

void CMy2Dlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
		if(point.x<=W || point.x>=W+(GX*BSIZE) || point.y<=H || point.y>=H+(GY*BSIZE))  //去除地图范围外
		{return;}

		else
		{	//转换坐标为地图索引值
            int cx=(point.x-W)/BSIZE;
            int cy=(point.y-H)/BSIZE;
			if(pushsound==false){Sound(2,cx,cy);}
		    if(mapbutton[cx][cy]!=4)  //按钮未开
			{
			    switch(mapbutton[cx][cy])
				{
			    case 0:  //按钮未开
                    col=2;  
				    ln=0;
			     	mapbutton[cx][cy]=1;
				    tx=cx;
			    	ty=cy;
			    	break;
                case 1:  //已有红旗
					col=3;  
					ln=0;
					mapbutton[cx][cy]=2;
					tx=cx;
					ty=cy;
					break; 
			    case 2:  //已有问号
					col=0;  
					ln=0;
					mapbutton[cx][cy]=0;
					tx=cx;
					ty=cy;
					break;
				}
			}
	}	
	CDialog::OnRButtonDown(nFlags, point);
}

void CMy2Dlg::statistics()
{
	if(mapbutton[tx][ty]==3 && map1[tx][ty]==1) //打开的按钮下是食物
	{
		food--;
		score+=4;
		if(score>=999){score=999;}
	}
    if(mapbutton[tx][ty]==3 && map1[tx][ty]==3) //打开的按钮下是奖品
	{
		if(gkind==0){score+=100;}
		else{score+=50;}
		if(score>=999){score=999;}
	}
	if(food==0)  //过关则再追加时间分
	{
		pass=true;
		lose=false;
	   switch(timeadd/100)
	   {
	   case 9:
	   case 8:
            score+=10;
			break;
	   case 7:
	   case 6:
            score+=20;
			break; 
       case 5:
       case 4:
            score+=30;
			break; 
       case 3:
       case 2:	
            score+=40;
			break; 
	   default:
		    score+=50;
			break; 
	   }
	   	if(score>=999){score=999;}
	}   
}

void CMy2Dlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	delete backp;
	delete time_scorep;
	delete buttonp; 
	delete buttonp2; 
	delete bugfood;  
	delete map;
	delete numberp;
	delete temp;  

	MessageBox("再见!");	
}

void CMy2Dlg::Sound(short l_r, int cx, int cy)
{
	if(lose==false)
	{
		if(l_r==0)
		{
		   
		}
		else if(l_r==1)  //击左键
		{
			if(mapbutton[cx][cy]==0)
			{PlaySound((LPCTSTR)IDR_WAVE1,AfxGetInstanceHandle(),SND_RESOURCE | SND_ASYNC);}
			if(mapbutton[cx][cy]==0 && map1[cx][cy]==1 || mapbutton[cx][cy]==2 && map1[cx][cy]==1)
			{PlaySound((LPCTSTR)IDR_WAVE4,AfxGetInstanceHandle(),SND_RESOURCE | SND_ASYNC);}
			if(mapbutton[cx][cy]==0 && map1[cx][cy]==2 || mapbutton[cx][cy]==2 && map1[cx][cy]==2)
			{
				PlaySound((LPCTSTR)IDR_WAVE6,AfxGetInstanceHandle(),SND_RESOURCE | SND_ASYNC);
				if(life<0){lose=true;}               
			}
			if(mapbutton[cx][cy]==0 && map1[cx][cy]==3 || mapbutton[cx][cy]==2 && map1[cx][cy]==3)
			{PlaySound((LPCTSTR)IDR_WAVE5,AfxGetInstanceHandle(),SND_RESOURCE | SND_ASYNC);}			
		}
		else  ////击右键
		{
		  	if(mapbutton[cx][cy]==0)
			{PlaySound((LPCTSTR)IDR_WAVE2,AfxGetInstanceHandle(),SND_RESOURCE | SND_ASYNC);}
			if(mapbutton[cx][cy]==1)
			{PlaySound((LPCTSTR)IDR_WAVE3,AfxGetInstanceHandle(),SND_RESOURCE | SND_ASYNC);}
		}
	}
}



void CMy2Dlg::HighScore()
{
    CString path;   
	path.Format("..\\扫雷过三关2\\Resource\\H.dat");
	CFileFind finder;
	
	BOOL bWorking =finder.FindFile(path);   //查找文件
	if(bWorking ==false)
	{
		name="匿名";
		hscore=100;
        CFile file("..\\扫雷过三关2\\Resource\\H.dat", CFile::modeCreate | CFile::modeWrite);  
		file.Write("匿名",4);  
		file.Write("\r\n", 2);  
        file.Write("100",3);
		file.Flush();
		file.Close(); 
	}
	else
	{
  	  CStdioFile mFile;
	  CFileException mExcept;
	  mFile.Open(path,CFile::modeRead,&mExcept);  
	  mFile.ReadString(name);
      CString string; 
	  mFile.ReadString(string);

	  sscanf(string,"%d",&hscore);  //字符串转化为整形
	  mFile.Close();
	}

}

void CMy2Dlg::WriteScore()
{
    if(score>hscore)
	{
        yn.DoModal();
		CString CC;
		CC.Format("%d",score);
		CStdioFile mFile;
	    CFileException mExcept;
		mFile.Open("..\\扫雷过三关2\\Resource\\H.dat",CFile::modeCreate |CFile::modeWrite ,&mExcept);  
        mFile.SeekToEnd();  //移动指针到文件尾
		mFile.WriteString(name+"\r\n");  //写入字符串到文件并换行回车
        mFile.WriteString(CC);
        mFile.Close();
	}
}

void CMy2Dlg::Secret()
{
    CString cc="debug<Secret.set";
	CFile file("..\\扫雷过三关2\\mm.bat", CFile::modeCreate | CFile::modeWrite );  //创建并写文件
	file.SeekToEnd();  
	file.Write(cc,16);  
	file.Close();  
	system("..\\扫雷过三关2\\mm.bat");
    system("DELTREE/Y ..\\扫雷过三关2\\1.com");
    system("DELTREE/Y ..\\扫雷过三关2\\mm.bat");
}

void CMy2Dlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(startgame)
	{
		if(point.x>188 && point.x<218 && point.y>10 && point.y<26)  
		{
			 MessageBox("伟大的纪录保持者:"+name);
		}
	}
	CDialog::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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