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

📄 pengpenggamedlg.cpp

📁 一个用VC实现的小游戏程序源代码。有兴趣分享一下
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if(m_Ball[i]==-1)
			notin[k++]=i;
	if(k<=3)return -1;
	else
		return notin[rand()%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;
	DisplayScore(410,118,m_nScore);


}

void CPengpengGameDlg::OnOK() 
{
	// TODO: Add extra validation here

}

BOOL CPengpengGameDlg::FindSuccLine(int pos, int type, CUIntArray *arrSave)
{
	arrSave->RemoveAll();
	int a=0,b=0,selCol=-1;
	arrSave->Add(pos);
	switch(type)
	{
	case 0:                 ////////////////查找竖的格子//////////////////
		if(m_Ball[pos]==2)
		{
			while( pos-9*(a+1)>=0 && m_Ball[pos-9*(a+1)]!=-1)
			{
				if(m_Ball[pos-9*(a+1)]!=2 )
				{
					selCol=m_Ball[pos-9*(a+1)];
					break;
				}
				arrSave->Add(pos-9*(a+1));
				a++;
			}
			if(selCol==-1)
			while( pos+9*(b+1)<81 && m_Ball[pos+9*(b+1)]!=-1)
			{
				if(m_Ball[pos+9*(b+1)]!=2 )
				{
					selCol=m_Ball[pos+9*(b+1)];
					break;
				}
				arrSave->Add(pos+9*(b+1));
				b++;
			}
			if(a>=4||b>=4)return TRUE;
			if(selCol==-1)
				if(a+b>=4)return TRUE;
			arrSave->RemoveAll();
			arrSave->Add(pos);
			a=0;b=0;
			if(selCol!=-1)
			{
				while( (pos-9*(a+1)>=0) && (m_Ball[pos-9*(a+1)]==selCol || 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)]==selCol || m_Ball[pos+9*(b+1)]==2) ){arrSave->Add(pos+9*(b+1));b++;}
			
				if(arrSave->GetSize()<5)
				{
					b=0;
					arrSave->RemoveAll();
					arrSave->Add(pos);
					while( pos+9*(b+1)<81 && m_Ball[pos+9*(b+1)]!=-1)
					{
						if(m_Ball[pos+9*(b+1)]!=2 )
						{
							selCol=m_Ball[pos+9*(b+1)];
							break;
						}
						b++;
					}
					b=0;
					while( (pos+9*(b+1)<81) && (m_Ball[pos+9*(b+1)]==selCol || m_Ball[pos+9*(b+1)]==2) ){arrSave->Add(pos+9*(b+1));b++;}


				}
			}
		}
		else
		{
			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++;}
		}
		break;


	case 1:                 ////////////////查找横的格子//////////////////
		if(m_Ball[pos]==2)
		{
			while( pos-(a+1)>=0 && ((pos-(a+1))/9==pos/9) && m_Ball[pos-(a+1)]!=-1)
			{
				if(m_Ball[pos-(a+1)]!=2 )
				{
					selCol=m_Ball[pos-(a+1)];
					break;
				}
				arrSave->Add(pos-(a+1));
				a++;
			}
			if(selCol==-1)
			while( pos+(b+1)<81 && ((pos+(b+1))/9==pos/9) && m_Ball[pos+(b+1)]!=-1)
			{
				if(m_Ball[pos+(b+1)]!=2 )
				{
					selCol=m_Ball[pos+(b+1)];
					break;
				}
				arrSave->Add(pos+(b+1));
				b++;
			}
			if(a>=4||b>=4)return TRUE;
			if(selCol==-1)
				if(a+b>=4)return TRUE;
			arrSave->RemoveAll();
			arrSave->Add(pos);
			a=0;b=0;
			if(selCol!=-1)
			{
				while( (pos-(a+1)>=0)  && ((pos-(a+1))/9==pos/9) && (m_Ball[pos-(a+1)]==selCol || 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)]==selCol || m_Ball[pos+(b+1)]==2) ){arrSave->Add(pos+(b+1));b++;}
			
				if(arrSave->GetSize()<5)
				{
					b=0;
					arrSave->RemoveAll();
					arrSave->Add(pos);
					while( pos+(b+1)<81 && ((pos+(b+1))/9==pos/9) && m_Ball[pos+(b+1)]!=-1)
					{
						if(m_Ball[pos+(b+1)]!=2 )
						{
							selCol=m_Ball[pos+(b+1)];
							break;
						}
						
						b++;
					}
					b=0;
					while( (pos+(b+1)<81)  && ((pos+(b+1))/9==pos/9) && (m_Ball[pos+(b+1)]==selCol || m_Ball[pos+(b+1)]==2) ){arrSave->Add(pos+(b+1));b++;}
				}
			}

		}
		else
		{
			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++;}
		}
		break;
	case 2:                 ////////////////查找右斜的格子//////////////////
		if(m_Ball[pos]==2)
		{
			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 )
				{
					selCol=m_Ball[pos-10*(a+1)];
					break;
				}
				arrSave->Add(pos-10*(a+1));
				a++;
			}
			if(selCol==-1)
			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 )
				{
					selCol=m_Ball[pos+10*(b+1)];
					break;
				}
				arrSave->Add(pos+10*(b+1));
				b++;
			}
			if(a>=4||b>=4)return TRUE;
			if(selCol==-1)
				if(a+b>=4)return TRUE;
			arrSave->RemoveAll();
			arrSave->Add(pos);
			a=0;b=0;
			if(selCol!=-1)
			{
				while( (pos-10*(a+1)>=0)  && ((pos-10*(a+1))/9==(pos-10*a)/9-1) && (m_Ball[pos-10*(a+1)]==selCol || 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)]==selCol || m_Ball[pos+10*(b+1)]==2) ){arrSave->Add(pos+10*(b+1));b++;}
			
				if(arrSave->GetSize()<5)
				{
					b=0;
					arrSave->RemoveAll();
					arrSave->Add(pos);
					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 )
						{
							selCol=m_Ball[pos+10*(b+1)];
							break;
						}
						
						b++;
					}
					b=0;
					while( (pos+10*(b+1)<81)  && ((pos+10*(b+1))/9==(pos+10*b)/9+1) && (m_Ball[pos+10*(b+1)]==selCol || m_Ball[pos+10*(b+1)]==2) ){arrSave->Add(pos+10*(b+1));b++;}
				}	
			}

		}
		else
		{
			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++;}
	
		}
		break;
	case 3:                 ////////////////查找左斜的格子//////////////////

		if(m_Ball[pos]==2)
		{
			while( pos-8*(a+1)>=0 && ((pos-8*(a+1))/9==(pos-10*a)/9-1) && m_Ball[pos-8*(a+1)]!=-1)
			{
				if(m_Ball[pos-8*(a+1)]!=2 )
				{
					selCol=m_Ball[pos-8*(a+1)];
					break;
				}
				arrSave->Add(pos-8*(a+1));
				a++;
			}
			if(selCol==-1)
			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 )
				{
					selCol=m_Ball[pos+8*(b+1)];
					break;
				}
				arrSave->Add(pos+8*(b+1));
				b++;
			}
			if(a>=4||b>=4)return TRUE;
			if(selCol==-1)
				if(a+b>=4)return TRUE;
			arrSave->RemoveAll();
			arrSave->Add(pos);
			a=0;b=0;
			if(selCol!=-1)
			{
				while( (pos-8*(a+1)>=0)  && ((pos-8*(a+1))/9==(pos-8*a)/9-1) && (m_Ball[pos-8*(a+1)]==selCol || 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)]==selCol || m_Ball[pos+8*(b+1)]==2) ){arrSave->Add(pos+8*(b+1));b++;}
				if(arrSave->GetSize()<5)
				{
					b=0;
					arrSave->RemoveAll();
					arrSave->Add(pos);
					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 )
						{
							selCol=m_Ball[pos+8*(b+1)];
							break;
						}
						b++;
					}
					b=0;
					while( (pos+8*(b+1)<81)  && ((pos+8*(b+1))/9==(pos+8*b)/9+1) && (m_Ball[pos+8*(b+1)]==selCol || m_Ball[pos+8*(b+1)]==2) ){arrSave->Add(pos+8*(b+1));b++;}
				}	
			}

		}
		else
		{
			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++;}
		}
		break;


	}
	return (arrSave->GetSize()>=5);//(a+b>=5);

}



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);


}

void CPengpengGameDlg::DisplayScore(int x,int y,int score)
{

	CClientDC dc(this);
	CString str;
	str.Format("%07d",score);
	for( int i=0;i<7;i++ )
		dc.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;
	if(m_nScore>m_arrHero.GetAt(0).Score)
		RedrawWindow();
	DisplayScore(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;
							
}

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)  )
		{
			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 )
		{
			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:
		for(i=0;i<5;i++)
		{		
			str1.Format("第%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:
		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;
		}
	}

	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;
}


⌨️ 快捷键说明

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