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

📄 pengpenggamedlg.cpp

📁 我写的搞怪碰碰球!里面游戏性还是蛮好玩的!对于学习游戏编程来说也是一个好代码!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			pnt[data[tail]]=data[head];
			if(data[tail]==to)break;
			bcheck[data[head]+1]=1;
			tail++;
		}
		head++;//队列头向前搜索



	if(head>=tail)break;//已经走队列尾了就跳出循环
	}//while(head>=tail);

	

	 if(data[tail]==to)//是不是到过达目标了
	 {
		arrSave->RemoveAll();//清空
		arrSave->Add(to);//第一个就是开始点
		int a=pnt[to];   
		arrSave->Add(a);
		//根据父结点找出路径
		while(a!=from)
		{
			arrSave->Add(pnt[a]);
			a=pnt[a];
			
		}
		return TRUE;

	 }
	 else
		 return FALSE;


}

//////////////////////////////////////////////////////////////////////
//      重载OnTimer
//	 来定时动画选中的效果
//   
//	 		
//  
/////////////////////////////////////////////////////////////////////

void CPengpengGameDlg::OnTimer(UINT nIDEvent) 
{

	if(nIDEvent==1313)//我们的
	{
		CClientDC dc(this);
		if(m_nSelected%2)
			dc.BitBlt(20+m_nSelected%9*40+2,60+m_nSelected/9*40+2,36,38,&m_dcBallHei,2+(rand()%5+1)*40,m_Ball[m_nSelected]*40,SRCCOPY);
		else
			dc.BitBlt(20+m_nSelected%9*40+2,60+m_nSelected/9*40+2,36,38,&m_dcBallAn,2+(rand()%5+1)*40,m_Ball[m_nSelected]*40,SRCCOPY);
	
	}

	CDialog::OnTimer(nIDEvent);
}
//////////////////////////////////////////////////////////////////////
//      
//	 返回随机一个还没有球子的位置
//    
//	 		
//  
/////////////////////////////////////////////////////////////////////

int CPengpengGameDlg::RandFindAt()
{
	int i,k=0,notin[81];
	for(i=0;i<81;i++)
		if(m_Ball[i]==-1)
			notin[k++]=i;//统计是空的球子的数量及位置
	if(k<=3)return -1;//如果没有足够三个了就返回-1说明已经没有位置了
	else
		return notin[rand()%k];//不然在K个位置里随机一个返回

}
//////////////////////////////////////////////////////////////////////
//      
//	 初始化棋盘及球子的信息
//    
//	 		
//  
/////////////////////////////////////////////////////////////////////

void CPengpengGameDlg::ReStart()
{
	int i;
	for( i=0;i<81;i++)
		m_Ball[i]=-1;	//设置为空
	if(m_nSelected!=-1)	//如果有选中
	{
		KillTimer(1313);//关闭动画效果
		m_nSelected=-1;

	}
	for( i=0;i<5;i++)
		m_Ball[RandFindAt()]=rand()%8;//先随机出五个球子出来
	for( i=0;i<3;i++)
		m_NextBall[i]=rand()%8;			//先随机三个下次要出来的球子
	m_nScore=0;		//得0分

}

void CPengpengGameDlg::OnOK() 
{

	// TODO: Add extra validation here
//郁闷中
}
//////////////////////////////////////////////////////////////////////
//        重要:判断某个位置是否满足削球子的
//	 pos 要判断的位置
//  type 要判断的方向 
//    arrSave 保存球子位置的链表
//	 			
//  如果满足削球子返回TRUE,不然返回FALSE
/////////////////////////////////////////////////////////////////////

BOOL CPengpengGameDlg::FindSuccLine(int pos, int type, CUIntArray *arrSave)
{
	if(m_Ball[pos]==-1)return FALSE;//pos位置是空就返回FALSE
	arrSave->RemoveAll();       
	int a=0,b=0;
	arrSave->Add(pos);//先记录自己位置
	switch(type)
	{
	case 0:                 ////////////////查找竖的格子//////////////////

		while( 
				(pos-9*(a+1)>=0) && //没有越界
				//上一个是相同或者是白色发光的球子
				(m_Ball[pos-9*(a+1)]==m_Ball[pos] || m_Ball[pos-9*(a+1)]==2) 
			)
			{
				arrSave->Add(pos-9*(a+1));//记录好位置
				a++;//继续向上判断
			}
		while( 
				(pos+9*(b+1)<81) && //没有越界
				//下一个是相同或者是白色发光的球子
				(m_Ball[pos+9*(b+1)]==m_Ball[pos] || m_Ball[pos+9*(b+1)]==2) 
				)
				{
					arrSave->Add(pos+9*(b+1));
					b++;//继续向下判断
				}
		if( m_Ball[pos]==2)//如果移动的是白色球子
		{
			a=0;b=0;
			while( pos-9*(a+1)>=0 && m_Ball[pos-9*(a+1)]!=-1)//没有越界且不是为空的
			{
				if(m_Ball[pos-9*(a+1)]!=2 )//向上查找直到不是白色球子
				{
					FindSuccLine(pos-9*(a+1),type,arrSave);//递归调用
					break;
				}
				a++;//继续向上判断
			}
			if(arrSave->GetSize()<5)//如果还是不满足
			while( pos+9*(b+1)<81 && m_Ball[pos+9*(b+1)]!=-1)//没有越界且不是为空的
			{
				if(m_Ball[pos+9*(b+1)]!=2 )//就向下查找不是白色球子
				{
					FindSuccLine(pos+9*(b+1),type,arrSave);//递归调用
					break;
				}
				b++;//继续向下判断
			}
		}
		break;
		///////以下三个方向查找原理基本相同.....

	case 1:                 ////////////////查找横的格子//////////////////
		while( (pos-(a+1)>=0) && ((pos-(a+1))/9==pos/9) && (m_Ball[pos-(a+1)]==m_Ball[pos]  || m_Ball[pos-(a+1)]==2) ){arrSave->Add(pos-(a+1));a++;}
		while( (pos+(b+1)<81) && ((pos+(b+1))/9==pos/9) && (m_Ball[pos+(b+1)]==m_Ball[pos]  || m_Ball[pos+(b+1)]==2) ){arrSave->Add(pos+(b+1));b++;}
		if( m_Ball[pos]==2)
		{
			a=0;b=0;
			while( pos-(a+1)>=0 && ((pos-(a+1))/9==pos/9) && m_Ball[pos-(a+1)]!=-1)
			{
				if(m_Ball[pos-(a+1)]!=2 )
				{
					FindSuccLine(pos-(a+1),type,arrSave);
					break;
				}
				a++;
			}
			if(arrSave->GetSize()<5)
			while( pos+(b+1)<81 && ((pos+(b+1))/9==pos/9) && m_Ball[pos+(b+1)]!=-1)
			{
				if(m_Ball[pos+(b+1)]!=2 )
				{
					FindSuccLine(pos+(b+1),type,arrSave);
					break;
				}
				b++;
			}
		}		
		break;
	case 2:                 ////////////////查找右斜的格子//////////////////
		while( (pos-10*(a+1)>=0) && ((pos-10*(a+1))/9==(pos-10*a)/9-1) && (m_Ball[pos-10*(a+1)]==m_Ball[pos] || m_Ball[pos-10*(a+1)]==2) ){arrSave->Add(pos-10*(a+1));a++;}
		while( (pos+10*(b+1)<81) && ((pos+10*(b+1))/9==(pos+10*b)/9+1) && (m_Ball[pos+10*(b+1)]==m_Ball[pos] || m_Ball[pos+10*(b+1)]==2) ){arrSave->Add(pos+10*(b+1));b++;}
		if( m_Ball[pos]==2)
		{
			a=0;b=0;
			while( pos-10*(a+1)>=0 && ((pos-10*(a+1))/9==(pos-10*a)/9-1) && m_Ball[pos-10*(a+1)]!=-1)
			{
				if(m_Ball[pos-10*(a+1)]!=2 )
				{
					FindSuccLine(pos-10*(a+1),type,arrSave);
					break;
				}
				a++;
			}
			if(arrSave->GetSize()<5)
			while( pos+10*(b+1)<81 && ((pos+10*(b+1))/9==(pos+10*b)/9+1) && m_Ball[pos+10*(b+1)]!=-1)
			{
				if(m_Ball[pos+10*(b+1)]!=2 )
				{
					FindSuccLine(pos+10*(b+1),type,arrSave);
					break;
				}
				b++;
			}
		}			
		break;
	case 3:                 ////////////////查找左斜的格子//////////////////
		while( (pos-8*(a+1)>=0) && ((pos-8*(a+1))/9==(pos-8*a)/9-1) && (m_Ball[pos-8*(a+1)]==m_Ball[pos] || m_Ball[pos-8*(a+1)]==2) ){arrSave->Add(pos-8*(a+1));a++;}
		while( (pos+8*(b+1)<81) && ((pos+8*(b+1))/9==(pos+8*b)/9+1) && (m_Ball[pos+8*(b+1)]==m_Ball[pos] || m_Ball[pos+8*(b+1)]==2) ){arrSave->Add(pos+8*(b+1));b++;}
		if( m_Ball[pos]==2)
		{
			a=0;b=0;
			while( pos-8*(a+1)>=0 && ((pos-8*(a+1))/9==(pos-8*a)/9-1) && m_Ball[pos-8*(a+1)]!=-1)
			{
				if(m_Ball[pos-8*(a+1)]!=2 )
				{
					FindSuccLine(pos-8*(a+1),type,arrSave);
					break;
				}
				a++;
			}
			if(arrSave->GetSize()<5)
			while( pos+8*(b+1)<81 && ((pos+8*(b+1))/9==(pos+8*b)/9+1) && m_Ball[pos+8*(b+1)]!=-1)
			{
				if(m_Ball[pos+8*(b+1)]!=2 )
				{
					FindSuccLine(pos+8*(b+1),type,arrSave);
					break;
				}
				b++;
			}
		}			
		break;

	}
	return (arrSave->GetSize()>=5);//返回是否大于等于五个以上的

}

//////////////////////////////////////////////////////////////////////
//      
//	 播放声音的函数
//    srcID为要播放声音资源ID号
//	 		
//  
/////////////////////////////////////////////////////////////////////

void CPengpengGameDlg::PlayMySound(UINT srcID)
{
	HRSRC hRsrc = FindResource( AfxGetResourceHandle(),
    MAKEINTRESOURCE( srcID ), "WAVE" );
    HGLOBAL hglb = LoadResource( AfxGetResourceHandle(), hRsrc );
    ::sndPlaySound( ( LPCTSTR )::LockResource( hglb ),SND_MEMORY|SND_ASYNC|SND_NODEFAULT);
    FreeResource( hglb);


}
//////////////////////////////////////////////////////////////////////
//      
//	 重画分数的函数
//    x,y为位置
//	 score为分数		
//  
/////////////////////////////////////////////////////////////////////

void CPengpengGameDlg::DisplayScore(CDC *pDC,int x,int y,int score)
{
	CString str;
	str.Format("%07d",score);
	for( int i=0;i<7;i++ )
		pDC->BitBlt(x+14*i,y,14,16,&m_dcScore,0,(str.GetAt(i)-'0')*16,SRCCOPY);


}
//////////////////////////////////////////////////////////////////////
//      
//	 闪烁满足削球子的函数
//    
//	 		
//  
/////////////////////////////////////////////////////////////////////
void CPengpengGameDlg::FlashTheCheak(CDC *pDC,CUIntArray *arrgood)
{
	PlayMySound(IDR_G2);//播放满足削球子的痛苦声音
	m_nScore=m_nScore+10+(arrgood->GetSize()-5)*5;
	//加分数(五个得10分 大于五个每个另加5分)
	DisplayScore(pDC,410,188,m_nScore);//重画分数
	int k1,k2,da;
	//闪烁
	for(k1=0;k1<7;k1++)
	{
		for(k2=0;k2<arrgood->GetSize();k2++)
		{
			da=arrgood->GetAt(k2);
		    if(k1%2)
			{
				if(da%2)
					pDC->BitBlt(20+(da%9)*40+2,60+da/9*40+2,36,38,&m_dcBallHei,2,m_Ball[da]*40,SRCCOPY);
				else
					pDC->BitBlt(20+(da%9)*40+2,60+da/9*40+2,36,38,&m_dcBallAn,2,m_Ball[da]*40,SRCCOPY);

			}
			else
				pDC->BitBlt(20+(da%9)*40+2,60+da/9*40+2,36,38,&m_dcBack,20+(da%9)*40+2,60+da/9*40+2,SRCCOPY);
		}
		Sleep(100);//闪烁速度
	}
	for(k2=0;k2<arrgood->GetSize();k2++)
		m_Ball[arrgood->GetAt(k2)]=-1;//然后把削去的球子位置设置为空
							
}
//////////////////////////////////////////////////////////////////////
//        重载OnMouseMove处理鼠标移动时
//		
//
/////////////////////////////////////////////////////////////////////
void CPengpengGameDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	BOOL bNewSelect=FALSE;
	for(int i=0;i<6;i++)
		if(m_rtSelect[i].PtInRect(point)  )//是否鼠标在6个按钮中的一个
		{
			if(m_nButtonSelect==i)return;//如果已经是在同位置就不处理
			CClientDC dc(this);
			if(m_nButtonSelect!=-1)
				RedrawWindow(m_rtSelect[m_nButtonSelect]);//恢复原来按钮图象
			if( i==0 || i==1 )//如果是右上角最小化或关闭按钮
				//用高亮度显示
				dc.BitBlt(m_rtSelect[i].left,m_rtSelect[i].top+2,32,34,&m_dcButtonSys,i*32,2,SRCCOPY);
			else//不然就是四个按钮了
				//我只是右下偏移一下它的位置而已
				dc.BitBlt( m_rtSelect[i].left-5,m_rtSelect[i].top-5,
				m_rtSelect[i].right-m_rtSelect[i].left+5,
				m_rtSelect[i].bottom-m_rtSelect[i].top+5,
				&m_dcBack,m_rtSelect[i].left-10,m_rtSelect[i].top-10,SRCCOPY);

			
			
			m_nButtonSelect=i;//记录鼠标所在按钮位置
			bNewSelect=TRUE;  //已经有新标记了
			PlayMySound(IDR_E0);//播放鼠标在按钮位置的声音
			break;
		}
		if( m_nButtonSelect!=-1 && bNewSelect==FALSE )
			//鼠标不在6个按钮 可是已经有新选了就恢复按钮的位置
		{
			RedrawWindow(m_rtSelect[m_nButtonSelect]);
			m_nButtonSelect=-1;//标记鼠标没有在任何一个按钮
		}
			
	   


	CDialog::OnMouseMove(nFlags, point);
}
//////////////////////////////////////////////////////////////////////
//        鼠标单击按钮时处理函数
//		
//
/////////////////////////////////////////////////////////////////////

void CPengpengGameDlg::ClickVirtualButton()
{

	CString str1,str2;
	int i;
	switch(m_nButtonSelect)
	{
	case 0:  //最小化
		ShowWindow(SW_SHOWMINIMIZED);
		break;
	case 1:   //关闭按钮
		if(AfxMessageBox("PKLANG提醒你:\n你真的不想玩了哈?",MB_YESNO)==IDYES)
			EndDialog(1);
		break;
	case 2:   //重新开始
		
		ReStart();
		break;
	case 3:   //荣誉榜
		str2=    "\t名次      姓名                            得分\n\n\n";
		for(i=0;i<5;i++)
		{		
			str1.Format("\t第%d名   % -30s  %07d         \n\n" ,i+1,m_arrHero.GetAt(i).Name,m_arrHero.GetAt(i).Score);       
			str2+=str1;
		
		}
		MessageBox("\n\n"+str2,"游戏排名!");


		break;
	case 4:   //游戏说明
		MessageBox("\n\n先选一个小怪怪,再点你想移动的格子,如果排成\n\n在一条横竖斜线颜色一样的小怪怪超过五个\n\n就可以消除掉,其中白色发光的小怪怪可以代替\n\n任何一种颜色,每10个得10分,多出每个加5分如果没有\n\n满足就生出三个小怪怪,所以你可要想好哦!!\n\n呵呵!!!!","游戏说明");
		break;
	case 5:   //关于PKLANG
		CAboutDlg dlg;
		dlg.DoModal();
		break;

	}
	m_nButtonSelect=-1;
	RedrawWindow();
}

void CPengpengGameDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	//继续郁闷中
//	CDialog::OnCancel();
}
//////////////////////////////////////////////////////////////////////
//        保存前五名的函数
//		
//
/////////////////////////////////////////////////////////////////////


BOOL CPengpengGameDlg::SaveHero(CString strName)
{

	PK_Hero da;

	//插入位置
	for(int i=3;i>=0;i--)
	{
		if(m_nScore>m_arrHero.GetAt(i).Score)
		{
			strcpy(da.Name,m_arrHero.GetAt(i).Name);
			da.Score=m_arrHero.GetAt(i).Score;
			m_arrHero.SetAt(i+1,da);
		}
		else
		{
			strcpy(da.Name,(LPCTSTR)strName);
			da.Score=m_nScore;
			m_arrHero.SetAt(i+1,da);
			break;
		}
	}
	if(i==-1)//是第一名
	{
		strcpy(da.Name,(LPCTSTR)strName);
		da.Score=m_nScore;
		m_arrHero.SetAt(i+1,da);

	}
	CFile fHero;
	fHero.Open("Hero.dat",CFile::modeCreate|CFile::modeWrite);
	for(i=0;i<5;i++)
		fHero.Write(&m_arrHero.GetAt(i),sizeof(PK_Hero));
	 fHero.Close();
	 return TRUE;

}



void CPengpengGameDlg::PlayMidi(CString src)
{
/*
	HWND hMCIWnd;

	hMCIWnd = MCIWndCreate(NULL,AfxGetInstanceHandle(), MCIWNDF_NOERRORDLG  , src);
	::ShowWindow(hMCIWnd,SW_HIDE);
	MCIWndPlay(hMCIWnd);
*/
 
}

⌨️ 快捷键说明

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