📄 els.c.txt
字号:
void CGame::Start()
{
Init();
m_Status = STATUS_PLAYING;
}
bool CGame::IsOver()
{
return (STATUS_GAMEOVER==m_Status) ;
}
void CGame::OnGameOver()
{
m_Status = STATUS_GAMEOVER;
}
void CGame::OnEraseLines(IGame* fromGame,long lines)
{
static int seed = 0 ;
int i ;
int j ;
int AddLines = lines - 1;
//random add lines to self
//check if die
for(i=0;i<m_WidthBlocks;i++)
{
for(j=(m_HeightBlocks-AddLines);j<m_HeightBlocks;j++)
{
if(1==GetBlockInfo(i,j))
{
OnGameOver();
return ;
}
}
}
//add lines
for(i=0;i<m_WidthBlocks;i++)
{
for(j=m_HeightBlocks-1;j>=AddLines;j--)
{
SetBlockInfo(i,j,GetBlockInfo(i,j-AddLines));
}
}
for(i=0;i<m_WidthBlocks;i++)
{
srand((unsigned)time(NULL)+seed);
long v = rand() % 2 ;
for(j=0;j<AddLines;j++)
{
SetBlockInfo(i,j,v);
}
seed ++ ;
}
//fixed
for(i=0;i<AddLines;i++)
{
srand((unsigned)time(NULL)+seed);
int x = rand() % m_WidthBlocks ;
SetBlockInfo(x,i,1);
seed++;
x = m_WidthBlocks - 1 - x ;
SetBlockInfo(x,i,0);
}
Draw();
}
long CGame::GetBlockInfo(long x,long y)
{
if(y>=m_HeightBlocks) return -1 ; //out of game panel
long pos = y * m_WidthBlocks + x ;
return m_pBlocks ;
}
void CGame::SetBlockInfo(long x,long y,long v)
{
if(x>=m_WidthBlocks || y>=m_HeightBlocks) return ; //out of game panel
long pos = y * m_WidthBlocks + x ;
m_pBlocks = v ;
}
bool CGame::IsValidDiamond(CDiamond* d)
{
long x = d->GetStartX();
long y = d->GetStartY();
CBlock* b;
for(int i=0;i<d->GetBlockCount();i++)
{
b = d->GetBlock(i);
if((x+b->m_x)<0) return false; //left check
if((x+b->m_x)>=m_WidthBlocks) return false; //right check
if((y+b->m_y)>=m_HeightBlocks) return false; //top check
if((y+b->m_y)<0) return false; //bottom check
if(1==GetBlockInfo(x+b->m_x,y+b->m_y)) return false; //block check
}
return true;
}
void CGame::LeftDiamond()
{
CDiamond t(*m_pCurrDiamond);
t.Left();
if(IsValidDiamond(&t))
{
m_pCurrDiamond->Draw(m_pDraw,false);
m_pCurrDiamond->Left();
m_pCurrDiamond->Draw(m_pDraw);
}
}
void CGame::RightDiamond()
{
CDiamond t(*m_pCurrDiamond);
t.Right();
if(IsValidDiamond(&t))
{
m_pCurrDiamond->Draw(m_pDraw,false); //hide
m_pCurrDiamond->Right();
m_pCurrDiamond->Draw(m_pDraw);
}
}
void CGame::NextDiamond()
{
CDiamond t(*m_pCurrDiamond);
t.Next();
if(IsValidDiamond(&t))
{
m_pCurrDiamond->Draw(m_pDraw,false); //
m_pCurrDiamond->Next();
m_pCurrDiamond->Draw(m_pDraw);
}
}
bool CGame::CanDown()
{
CDiamond t(*m_pCurrDiamond);
t.Down();
return IsValidDiamond(&t);
}
void CGame::DropDiamond()
{
m_pCurrDiamond->Draw(m_pDraw,false);
while(CanDown())
{
m_pCurrDiamond->Down();
}
OnDownADiamond();
}
void CGame::DownDiamond()
{
if(!CanDown())
{
OnDownADiamond();
}
else
{
m_pCurrDiamond->Draw(m_pDraw,false);
m_pCurrDiamond->Down();
m_pCurrDiamond->Draw(m_pDraw);
}
}
void CGame::OnDownADiamond()
{
CBlock* b ;
for(int i=0;i<m_pCurrDiamond->GetBlockCount();i++)
{
b = m_pCurrDiamond->GetBlock(i);
SetBlockInfo(m_pCurrDiamond->GetStartX() + b->m_x,
m_pCurrDiamond->GetStartY() + b->m_y,
1);
}
long lines = EraseLines();
if(lines>0)
{
m_pController->OnEraseLines(this,lines);
}
delete m_pCurrDiamond;
m_pCurrDiamond = CDiamond::CreateAnInstance(m_WidthBlocks/2,m_HeightBlocks);
if(!IsValidDiamond(m_pCurrDiamond))
{
OnGameOver();
return ;
}
Draw();
}
long CGame::EraseLines()
{
long count = m_WidthBlocks * m_HeightBlocks ;
long* pNewBlocks = new long;
for(int ii=0 ;ii<count;ii++)
{
pNewBlocks = 0;
}
bool bErase ;
int lines = 0 ;
int fillLines = 0 ;
for(int i=0 ;i<m_HeightBlocks;i++)
{
bErase = true ;
for(int j=0;j<m_WidthBlocks;j++)
{
if(0==GetBlockInfo(j,i))
{
bErase = false;
break;
}
}
if(bErase)
{
lines++;
}
else
{
memcpy(&pNewBlocks,&m_pBlocks,sizeof(long)*m_WidthBlocks);
fillLines ++;
}
}
delete []m_pBlocks ;
m_pBlocks = pNewBlocks ;
return lines ;
}
void CGame::Show()
{
CString s = "" ;
CString a ;
for(int i=0;i<m_HeightBlocks;i++)
{
for(int j=0;j<m_WidthBlocks;j++)
{
if(1==GetBlockInfo(j,i))
{
s += "1," ;
}
else
{
s += "0," ;
}
}
s += "\n" ;
}
AfxMessageBox(s);
}
void CGame::Draw()
{
for(int i=0;i<m_HeightBlocks;i++)
{
for(int j=0;j<m_WidthBlocks;j++)
{
if(GetBlockInfo(j,i)==1)
{
m_pDraw->ShowBlocks(j,i,1,1);
}
else
{
m_pDraw->HideBlocks(j,i,1,1);
}
}
}
m_pCurrDiamond->Draw(m_pDraw);
}
//=class:CDrawDrvWindowsImp=//
CDrawDrvWindowsImp::CDrawDrvWindowsImp(POINT p,COLORREF block_color,COLORREF bg_color,long block_size,CDC* pDC)
:m_OrgPoint(p),m_BlockColor(block_color),m_BgColor(bg_color),m_BlockSize(block_size),m_BlockBrush(block_color),m_BgBrush(bg_color),m_BorderBrush(RGB(255,255,255))
{
m_pDC = pDC;
}
CDrawDrvWindowsImp::ShowBlocks(long x,long y,long xs,long ys)
{
RECT rect;
rect.left = m_OrgPoint.x + x * m_BlockSize;
rect.top = m_OrgPoint.y - ( y + 1) * m_BlockSize ;
rect.right = rect.left + xs * m_BlockSize ;
rect.bottom = rect.top + ys * m_BlockSize ;
m_pDC->FillRect(&rect,&m_BorderBrush);
rect.left ++ ;
rect.top ++;
m_pDC->FillRect(&rect,&m_BlockBrush);
}
CDrawDrvWindowsImp::HideBlocks(long x,long y,long xs,long ys)
{
RECT rect;
rect.left = m_OrgPoint.x + x * m_BlockSize;
rect.top = m_OrgPoint.y - ( y + 1) * m_BlockSize ;
rect.right = rect.left + xs * m_BlockSize ;
rect.bottom = rect.top + ys * m_BlockSize ;
m_pDC->FillRect(&rect,&m_BgBrush);
}
//CGameController
CGameController::CGameController()
{
m_Status = STATUS_TO_START;
}
CGameController::~CGameController()
{
Clean();
}
void CGameController::Clean()
{
POSITION pos ;
pos = m_Games.GetHeadPosition();
IGame* p;
while(pos)
{
p = m_Games.GetNext(pos);
delete p ;
}
}
void CGameController::OnEraseLines(IGame* pFromGame,long lines)
{
if(lines<2)
{
return ;
}
POSITION pos ;
pos = m_Games.GetHeadPosition();
IGame* p;
while(pos)
{
p = m_Games.GetNext(pos);
if(p!=pFromGame)
{
if(!p->IsOver())
{
p->OnEraseLines(pFromGame,lines);
}
}
}
}
void CGameController::OnGameOver(IGame* pGame)
{
POSITION pos ;
pos = m_Games.GetHeadPosition();
IGame* p;
bool bEnd = true;
while(pos)
{
p = m_Games.GetNext(pos);
if(p->IsOver())
{
bEnd = false;
}
}
if(bEnd)
{
End();
}
}
void CGameController::Start(CDC* pDC)
{
IGame* pGame;
IDrawDrv* pDraw;
long BlockSize;
long WidthBlocks;
long HeightBlocks;
COLORREF BlockColor;
COLORREF BgColor;
POINT StartPos ;
CKeyController key ;
//game0
BlockSize = 18 ;
WidthBlocks = 14;
HeightBlocks = 20 ;
BlockColor = RGB(255,0,0);
BgColor = RGB(160,0,0);
StartPos.x = 10 ;
StartPos.y = 60 + BlockSize * HeightBlocks ;
key.DOWN = 'S' ;
key.LEFT = 'A' ;
key.RIGHT ='D';
key.NEXT = 'W';
key.DROP = 'G' ;
pDraw = new CDrawDrvWindowsImp(StartPos,BlockColor,BgColor,BlockSize,pDC);
pGame = new CGame(this,pDraw,key,WidthBlocks,HeightBlocks);
m_Games.AddTail(pGame);
//game1
BlockSize = 18 ;
WidthBlocks = 14;
HeightBlocks = 20 ;
BlockColor = RGB(255,0,0);
BgColor = RGB(160,0,0);
StartPos.x = 350 ;
StartPos.y = 60 + BlockSize * HeightBlocks ;
key.DOWN = 40 ;
key.LEFT = 37 ;
key.RIGHT = 39;
key.NEXT = 38;
key.DROP = 'L' ;
pDraw = new CDrawDrvWindowsImp(StartPos,BlockColor,BgColor,BlockSize,pDC);
pGame = new CGame(this,pDraw,key,WidthBlocks,HeightBlocks);
m_Games.AddTail(pGame);
m_Status = STATUS_PLAYING;
}
void CGameController::Pause()
{
if(m_Status == STATUS_PAUSE)
{
m_Status = STATUS_PLAYING;
}
else
{
m_Status = STATUS_PAUSE;
}
}
void CGameController::End()
{
m_Status = STATUS_END;
Clean();
}
void CGameController::OnDraw()
{
POSITION pos ;
pos = m_Games.GetHeadPosition();
IGame* p;
while(pos)
{
p = m_Games.GetNext(pos);
if(!p->IsOver())
{
p->Draw();
}
}
}
void CGameController::OnKey(long keycode)
{
if(m_Status!=STATUS_PLAYING) return ;
POSITION pos ;
pos = m_Games.GetHeadPosition();
IGame* p;
while(pos)
{
p = m_Games.GetNext(pos);
if(!p->IsOver())
{
p->OnKey(keycode);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -