fankuaidlg.cpp

来自「俄罗斯方块的主要功能都具有,和手机上一样,在EVC平台上开发的,特别适合车载导航」· C++ 代码 · 共 925 行 · 第 1/2 页

CPP
925
字号
				CurrentBrick[4].x=3;
			
				break;
		case 4:
				CurrentBrick[0].x=0;
				CurrentBrick[0].y=4;
				CurrentBrick[1].x=0;
				CurrentBrick[1].y=5;
				CurrentBrick[2].x=0;
				CurrentBrick[2].y=6;
				CurrentBrick[3].x=1;
				CurrentBrick[3].y=6;
				CurrentBrick[4].x=4;
			
				break;
		case 5:
				CurrentBrick[0].x=1;
				CurrentBrick[0].y=4;
				CurrentBrick[1].x=1;
				CurrentBrick[1].y=5;
				CurrentBrick[2].x=1;
				CurrentBrick[2].y=6;
				CurrentBrick[3].x=0;
				CurrentBrick[3].y=6;
				CurrentBrick[4].x=5;
			
				break;
		case 6:
				CurrentBrick[0].x=0;
				CurrentBrick[0].y=4;
				CurrentBrick[1].x=1;
				CurrentBrick[1].y=4;
				CurrentBrick[2].x=1;
				CurrentBrick[2].y=5;
				CurrentBrick[3].x=1;
				CurrentBrick[3].y=3;
				CurrentBrick[4].x=6;
			
				break;
		case 0:
				CurrentBrick[0].x=0;
				CurrentBrick[0].y=4;
				CurrentBrick[1].x=0;
				CurrentBrick[1].y=5;
				CurrentBrick[2].x=1;
				CurrentBrick[2].y=4;
				CurrentBrick[3].x=1;
				CurrentBrick[3].y=5;
				CurrentBrick[4].x=0;
			
				break;


	}
	BrickAtBottom=FALSE;
	RefreshBricks();
	myDraw();
   
}

void CFankuaiDlg::myDraw()
{
	int i;
	CDC* dc=GetDC();
	if(GameState!=RUNNING) 
	{
		RefreshAll();
		return;
	}
	else
	{
		for(i=0;i<4;i++)//在方块数组中标明当前方块
		{
			MatrixOfBricks[CurrentBrick[i].x][CurrentBrick[i].y]=1;
		}
		if(!EraseALine)//如果尚未消行
		{
			if(!BrickAtBottom)
				for(i=0;i<4;i++)// 清除移动前的方块痕迹
				{
					dc->FillSolidRect(LEFTMARGIN+LastPositionOfBrick[i].y*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+LastPositionOfBrick[i].x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,HIGHLIGHTCOLOR);
				//	dc->Draw3dRect(LEFTMARGIN+LastPositionOfBrick[i].y*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+LastPositionOfBrick[i].x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,clrTopLeft, HIGHLIGHTCOLOR);
				}
			for(i=0;i<4;i++)//重绘正在下落的方块
			{
				dc->FillSolidRect(LEFTMARGIN+CurrentBrick[i].y*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+CurrentBrick[i].x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,ColorOfCurrentBrick);
//				dc->Draw3dRect(LEFTMARGIN+CurrentBrick[i].y*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+CurrentBrick[i].x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,clrTopLeft, ColorOfCurrentBrick);
			}


		}
		else
		{
			RefreshAll();
			EraseALine=FALSE;
		}

	}
	ReleaseDC(dc);
}

void CFankuaiDlg::RefreshAll()
{
	CDC* dc=GetDC();
	TRACE(L"GETDC:%x\n",dc);
	TRACE(L"CYCAPTION:%x\n",::GetSystemMetrics(SM_CYCAPTION));
	/////全部重绘//////////
	for(int x=0;x<20;x++)
	{
		for(int j=0;j<10;j++)
		{
			if((MatrixOfBricks[x][j]==1)||(MatrixOfBricks[x][j]==2))
			{
				dc->FillSolidRect(LEFTMARGIN+j*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,ColorOfCurrentBrick);
//				dc->Draw3dRect(LEFTMARGIN+j*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,clrTopLeft, ColorOfCurrentBrick);
				TRACE(L"Draw Bricks\n");
			}
			else
			{

				dc->FillSolidRect(LEFTMARGIN+j*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,HIGHLIGHTCOLOR);
//				dc->Draw3dRect(LEFTMARGIN+j*WIDTHOFBRICKS,TOPMARGIN+::GetSystemMetrics(SM_CYCAPTION)+x*HEIGHTOFBRICKS,WIDTHOFBRICKS,HEIGHTOFBRICKS,clrTopLeft, HIGHLIGHTCOLOR);
				TRACE(L"Draw Background\n");
			}
		}
	}
	ReleaseDC(dc);
	///////////////////////
}

void CFankuaiDlg::Start() 
{
	// TODO: Add your control notification handler code here
	Level=0;
    TimerInterval=5000/20;
    SetTimer(1,TimerInterval,NULL);
	GameState=RUNNING;
	InitBricks();
	GenerateABrick();
	SetDlgItemText(IDC_STATIC_NUM,L"0");
	SetDlgItemText(IDC_LEVEL,L"0");
    CDC*dc=GetDC();
    dc->FillSolidRect(180,50,100,60,RGB(0,0,255));
	
}



void CFankuaiDlg::Stop() 
{
	// TODO: Add your control notification handler code here
	GameState=STOP;
	KillTimer(1);//停计时器
	InitBricks();
	
}

void CFankuaiDlg::Exit() 
{
	// TODO: Add your control notification handler code here
	EndDialog(0);
	
}

HBRUSH CFankuaiDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(nCtlColor==CTLCOLOR_STATIC)
	{
		pDC->SelectObject(&StaticFont);
		pDC->SetTextColor(RGB(255,0,0));
	}
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}



BOOL CFankuaiDlg::PreTranslateMessage(MSG* pMsg) 
{   
	if (pMsg->message == WM_KEYDOWN)
	{
       

	// TODO: Add your message handler code here and/or call default

	
		switch(pMsg->wParam)
		{   
           case VK_RETURN:
             return true;
           case VK_SPACE:
             return true;

			case VK_LEFT://左方向
			case VK_NUMPAD4:
			
			nChar=VK_NUMPAD1;
			OnKey( nChar) ;	
			return  true;

			case VK_RIGHT://右方向
			case VK_NUMPAD6:	
			nChar=VK_NUMPAD3;
			OnKey( nChar) ;	
			return  true;
		
			case VK_DOWN://下落
			case VK_NUMPAD2:
			nChar=VK_NUMPAD7;
            OnKey( nChar) ;	
			return true;
			case VK_UP://旋转
			case VK_NUMPAD8:
			case VK_NUMPAD5:
			nChar=VK_NUMPAD9;
			OnKey( nChar) ;	
	           return  true;	

		}
	
	}
  	
	return CDialog::PreTranslateMessage(pMsg);

}



void CFankuaiDlg::OnGameOver() 
{
TCHAR m_strValue[200];
if(GameState==STOP)
			Exit();
		else
			Stop();	
    _itow(0,m_strValue,10 );
	SetDlgItemText(IDC_STATIC_NUM,m_strValue);
    SetDlgItemText(IDC_LEVEL,m_strValue);
    CDC*dc=GetDC();
    dc->FillSolidRect(180,50,100,60,RGB(0,0,255));
	return;
}

void CFankuaiDlg::OnGameStart() 
{
Start();
Number[0]=rand()%7;
Number[1]=rand()%7;
	return;
	
}


void CFankuaiDlg::OnUp() 
{	
	if(GameState==RUNNING) 
	
	{
		nChar=VK_NUMPAD9;
    OnKey( nChar) ;	
	}
	// TODO: Add your control notification handler code here
	
}

void CFankuaiDlg::OnDown() 
{
	if(GameState==RUNNING) 
	{nChar=VK_NUMPAD7;
OnKey( nChar) ;	
	}
	// TODO: Add your control notification handler code here
	
}

void CFankuaiDlg::OnLeft() 
{  if(GameState==RUNNING) 
{	
	nChar=VK_NUMPAD1;
	OnKey( nChar) ;	
}

			
	// TODO: Add your control notification handler code here
	
}

void CFankuaiDlg::OnRight() 
{	if(GameState==RUNNING) 
{nChar=VK_NUMPAD3;
    OnKey( nChar) ;	
}

	// TODO: Add your control notification handler code here
	
}



void CFankuaiDlg::DrawNextBrick()
{
	CDC*dc=GetDC();
	int i;
	if(GameState==RUNNING) 
	{
		switch (Number[1])
		{
			case 1://-----//
				CFankuaiDlg::CurrentBrick[0].x=0;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=0;
				CFankuaiDlg::	CurrentBrick[1].y=5;
				CFankuaiDlg::	CurrentBrick[2].x=0;
				CFankuaiDlg::	CurrentBrick[2].y=3;
				CFankuaiDlg::	CurrentBrick[3].x=0;
				CFankuaiDlg::	CurrentBrick[3].y=2;
				CFankuaiDlg::	CurrentBrick[4].x=1;
				break;
			case 2:
				CFankuaiDlg::	CurrentBrick[0].x=0;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=0;
				CFankuaiDlg::	CurrentBrick[1].y=5;
				CFankuaiDlg::	CurrentBrick[2].x=1;
				CFankuaiDlg::	CurrentBrick[2].y=5;
				CFankuaiDlg::	CurrentBrick[3].x=1;
				CFankuaiDlg::	CurrentBrick[3].y=6;
				CFankuaiDlg::	CurrentBrick[4].x=2;

			
				break;
			case 3:
				CFankuaiDlg::	CurrentBrick[0].x=1;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=1;
				CFankuaiDlg::	CurrentBrick[1].y=5;
				CFankuaiDlg::	CurrentBrick[2].x=0;
				CFankuaiDlg::	CurrentBrick[2].y=5;
				CFankuaiDlg::	CurrentBrick[3].x=0;
				CFankuaiDlg::	CurrentBrick[3].y=6;
				CFankuaiDlg::	CurrentBrick[4].x=3;
				
				break;
			case 4:
				CFankuaiDlg::	CurrentBrick[0].x=0;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=0;
				CFankuaiDlg::	CurrentBrick[1].y=5;
				CFankuaiDlg::	CurrentBrick[2].x=0;
				CFankuaiDlg::	CurrentBrick[2].y=6;
				CFankuaiDlg::	CurrentBrick[3].x=1;
				CFankuaiDlg::	CurrentBrick[3].y=6;
				CFankuaiDlg::	CurrentBrick[4].x=4;
		
				break;
			case 5:
				CFankuaiDlg::	CurrentBrick[0].x=1;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=1;
				CFankuaiDlg::	CurrentBrick[1].y=5;
				CFankuaiDlg::	CurrentBrick[2].x=1;
				CFankuaiDlg::	CurrentBrick[2].y=6;
				CFankuaiDlg::	CurrentBrick[3].x=0;
				CFankuaiDlg::	CurrentBrick[3].y=6;
				CFankuaiDlg::	CurrentBrick[4].x=5;
				
				break;
			case 6:
				CFankuaiDlg::	CurrentBrick[0].x=0;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=1;
				CFankuaiDlg::	CurrentBrick[1].y=4;
				CFankuaiDlg::	CurrentBrick[2].x=1;
				CFankuaiDlg::	CurrentBrick[2].y=5;
				CFankuaiDlg::	CurrentBrick[3].x=1;
				CFankuaiDlg::	CurrentBrick[3].y=3;
				CFankuaiDlg::	CurrentBrick[4].x=6;
			
				break;
			case 0:
				CFankuaiDlg::	CurrentBrick[0].x=0;
				CFankuaiDlg::	CurrentBrick[0].y=4;
				CFankuaiDlg::	CurrentBrick[1].x=0;
				CFankuaiDlg::	CurrentBrick[1].y=5;
				CFankuaiDlg::	CurrentBrick[2].x=1;
				CFankuaiDlg::	CurrentBrick[2].y=4;
				CFankuaiDlg::	CurrentBrick[3].x=1;
				CFankuaiDlg::	CurrentBrick[3].y=5;
				CFankuaiDlg::	CurrentBrick[4].x=0;
		
				break;


		}
		dc->FillSolidRect(180,50,100,60,RGB(0,0,255));
		for(i=0;i<4;i++)
		{
			dc->FillSolidRect(175+	CFankuaiDlg::	CurrentBrick[i].y*CFankuaiDlg::WIDTHOFBRICKS,80+CFankuaiDlg::CurrentBrick[i].x*CFankuaiDlg::HEIGHTOFBRICKS,CFankuaiDlg::WIDTHOFBRICKS,CFankuaiDlg::HEIGHTOFBRICKS,RGB(255, 100, 140));
		}
	}	
}

void CFankuaiDlg::OnPause() 
{
if(GameState==RUNNING) 
	{
		GameState=PAUSED;
		myDraw();
	
	}
	else if(GameState==PAUSED)
		GameState=RUNNING;
	// TODO: Add your control notification handler code here
	
}


int CFankuaiDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here



	return 0;
}





void CFankuaiDlg::OnStaticNum() 
{
	// TODO: Add your control notification handler code here
	
}

void CFankuaiDlg::FullScreen()
{

  CRect  m_FullScreenRect;  //全屏区域  
  CRect  WindowRect;    
  GetWindowRect(&WindowRect);    
  CRect  ClientRect;  
  RepositionBars(0,  0xffff,  AFX_IDW_PANE_FIRST,  reposQuery,  &ClientRect);  
  ClientToScreen(&ClientRect);    
  int  nFullWidth=GetSystemMetrics(SM_CXSCREEN);  
  int  nFullHeight=GetSystemMetrics(SM_CYSCREEN);  
  m_FullScreenRect.left  =  WindowRect.left-ClientRect.left;  
  m_FullScreenRect.top=WindowRect.top-ClientRect.top;  
  m_FullScreenRect.right=WindowRect.right-ClientRect.right+nFullWidth;  
  m_FullScreenRect.bottom=WindowRect.bottom-ClientRect.bottom+nFullHeight;  
  this->SetWindowPos(&wndBottom,m_FullScreenRect.left,m_FullScreenRect.top,m_FullScreenRect.Width(),m_FullScreenRect.Height(),SWP_SHOWWINDOW); 
/////////////////////////全屏}
}

⌨️ 快捷键说明

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