【VC开源代码栏目提醒】:本文主要为网学会员提供ElsGame.cpp,希望对需要ElsGame.cpp网友有所帮助,学习一下!
// ElsGame.cpp: implementation of the CElsGame class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "games.h"
#include "ElsGame.h"
#include "Mythread.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CElsGame, CGamsWnd)
BEGIN_MESSAGE_MAP(CElsGame, CGamsWnd)
//{{AFX_MSG_MAP(CElsGame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
ON_COMMAND(IDM_GAME, StartGame)
ON_COMMAND(IDM_STOP, StopGame)
ON_COMMAND(IDM_AUTO, SetAuto)
ON_COMMAND_RANGE(IDM_CHUJI, IDM_GAOJI, SetJi)
ON_WM_TIMER()
ON_WM_KEYDOWN()
ON_WM_PAINT()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
// Standard printing commands
END_MESSAGE_MAP()
CElsGame::CElsGame()
{
m_pEls = NULL;
m_pNext = NULL;
m_nTimer = 0;
m_nRows = 0;
m_nPoint = 0;
m_bStop = FALSE;
m_nTime = 200;
m_nTypeNum = 0;
FormatArray();
m_bAuto = FALSE;
}
void CElsGame::FormatArray()
{
m_nRows = 0;
m_nPoint = 0;
for(int i = 0; i < Xnum; i ++)
{
for(int j = 0; j < Ynum; j ++)
m_nArray[i][j] = 0;
m_nRowsHigh[i] = 0;
}
}
void CElsGame::StopGame()
{
if(m_nTimer == 0)
return;
if(m_nTimer != 0)
{
this->KillTimer(m_nTimer);
m_nTimer = 0;
}
m_bStop = TRUE;
}
CElsGame::~CElsGame()
{
if(m_pEls != NULL)
delete m_pEls;
if(m_MemeryDC.m_hDC != NULL)
m_MemeryDC.DeleteDC();
}
void CElsGame::FormatInfo()
{
CDC* pDC = GetDC();
CRect reClient;
this->GetClientRect(&reClient);
pDC->MoveTo(Xnum * 24, 0);
pDC->LineTo(Xnum * 24, Ynum * 24);
pDC->MoveTo(Xnum * 24, 24 * 6);
pDC->LineTo(reClient.right, 24 * 6);
CString sS;
sS.Format("Total Rows: %.8d", m_nRows);
pDC->TextOut(Xnum * 24 + 20, 24 * 6 + 20, sS);
sS.Format("Total Points: %.8d", m_nPoint);
pDC->TextOut(Xnum * 24 + 20, 24 * 7 + 20, sS);
sS.Format("Total Score: %.8d", m_nRows * 100 + m_nPoint);
pDC->TextOut(Xnum * 24 + 20, 24 * 8 + 20, sS);
/*
for(int i = 0; i < Xnum; i ++)
{
sS.Format("XH: %d : %d", i, m_nRowsHigh[i]);
pDC->TextOut(Xnum * 24 + 20, 24 * (5+i) + 20, sS);
}
*/
ReleaseDC(pDC);
}
void CElsGame::StartGame()
{
if(m_nTypeNum == 0)
SetJi(IDM_ZHONGJI);
if(!m_bStop)
{
FormatArray();
FormatInfo();
OnPaint();
CreateNewEls();
}
if(m_nTimer == 0)
{
m_nTimer = this->SetTimer(1, m_nTime, 0);
if(m_bAuto)
AfxBeginThread(CMyThread::AutoRun, (LPVOID)this);
}
}
void CElsGame::CreateNewEls()
{
m_bNew = TRUE;
static i = 0;
i ++;
int nType;
// if(i == 5)
// {
// i = 0;
// nType = 0;
// }
// else
{
srand((unsigned)time(NULL));
nType = (rand() % m_nTypeNum);
}
if(m_pEls != NULL)
{
delete m_pEls;
m_pEls = NULL;
}
if(m_pNext == NULL)
{
m_pNext = new CEls(nType, 24 * 5 + 24 * Xnum, 24);
m_pEls = new CEls(rand() % m_nTypeNum, 120, 0);
}
else
{
m_pEls = new CEls(m_pNext->m_nType, 168, 0);
}
DrawNext(FALSE);
m_pNext->m_nType = nType;
DrawEls();
DrawNext();
}
void CElsGame::DrawEls(BOOL b)
{
if(m_pEls == NULL)
return;
int i;
int nPoint = m_pEls->GetPointNum();
for(i = 0; i < nPoint; i ++)
{
DrawPoint(m_pEls->GetPoint(i, b), b);
}
}
void CElsGame::DrawNext(BOOL b)
{
if(m_pNext == NULL)
return;
int i;
int nPoint = m_pNext->GetPointNum();
for(i = 0; i < nPoint; i ++)
{
DrawPoint(m_pNext->GetPoint(i), b);
}
}
void CElsGame::DrawPoint(CPoint p, BOOL b)
{
if(m_MemeryDC.m_hDC == NULL)
CreateMemDC();
CDC* pDC = GetDC();
if(b)
pDC->BitBlt(p.x, p.y, 24, 24, &m_MemeryDC, 0, 0, SRCCOPY);
else
pDC->BitBlt(p.x, p.y, 24, 24, &m_MemeryDCNULL, 0, 0, SRCCOPY);
ReleaseDC(pDC);
}
void CElsGame::CreateMemDC()
{
m_MemeryDC.CreateCompatibleDC(NULL);
CBitmap bmp;
bmp.LoadBitmap(IDB_ELS);
m_MemeryDC.SelectObject(&bmp);
m_MemeryDCNULL.CreateCompatibleDC(NULL);
bmp.DeleteObject();
bmp.LoadBitmap(IDB_ELSNULL);
m_MemeryDCNULL.SelectObject(&bmp);
bmp.DeleteObject();
}
void CElsGame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(m_pEls == NULL || m_nTimer == 0)
{
return;
}
CEls pE;
switch(nChar)
{
case 39:
pE = *m_pEls;
pE.SetHotPoint(24, 0);
if(GetElsPosition(pE))
{
DrawEls(FALSE);
m_pEls->SetHotPoint(24, 0);
DrawEls();
}
break;
case 37:
pE = *m_pEls;
pE.SetHotPoint(-24, 0);
if(GetElsPosition(pE))
{
DrawEls(FALSE);
m_pEls->SetHotPoint(-24, 0);
DrawEls();
}
break;
case 38:
pE = *m_pEls;
pE.SetMoveType(1);
if(GetElsPosition(pE))
{
m_pEls->SetMoveType(1);
DrawEls(FALSE);
m_pEls->SetMoveType();
DrawEls();
}
break;
case 40:
SendMessage(WM_TIMER,0,0);
break;
}
}
void CElsGame::OnPaint()
{
CGamsWnd::OnPaint();
CDC* pDC = GetDC();
for(int i = 0; i < Xnum; i ++)
{
for(int y = 0; y< Ynum; y++)
{
if(m_nArray[i][y] == 1)
pDC->BitBlt(i * 24, y * 24, 24, 24, &m_MemeryDC, 0, 0, SRCCOPY);
else
pDC->BitBlt(i * 24, y * 24, 24, 24, &m_MemeryDCNULL, 0, 0, SRCCOPY);
}
}
ReleaseDC(pDC);
FormatInfo();
}
void CElsGame::OnTimer( UINT nIDEvent )
{
DrawEls(FALSE);
m_pEls->SetMoveType();
int nMove = GetMoveAble();
if(nMove == 0)
{
m_pEls->SetHotPoint(0, 24);
DrawEls();
}
else if(nMove == 1)
{
DrawEls();
CreateNewEls();
OnPaint();
}
else
{
DrawEls();
this->KillTimer(m_nTimer);
m_nTimer = 0;
m_bStop = FALSE;
}
}
UINT CElsGame::GetMoveAble(int nType)
{
int i;
int nPoint = m_pEls->GetPointNum();
for(i = 0; i < nPoint; i ++)
{
CPoint p = m_pEls->GetPoint(i);
if(nType == 1)
{
int x = p.x / 24;
int y = p.y / 24;
if(x < 0 || y < 0 || x >= Xnum)
continue;
if(y == Ynum - 1)
{
return SetArray();
}
if(m_nArray[x][y + 1] == 1)
{
return SetArray();
}
}
else if(nType == 2)
{
if(p.x <= 0)
return -1;
}
else if(nType == 3)
{
if(p.x >= (Xnum - 1) * 24)
return -1;
}
}
return 0;
}
UINT CElsGame::SetArray()
{
int i, re = 1;
int nPoint = m_pEls->GetPointNum();
for(i = 0; i < nPoint; i ++)
{
CPoint p = m_pEls->GetPoint(i);
int x = p.x / 24;
int y = p.y / 24;
m_nArray[x][y] = 1;
if(m_nRowsHigh[x] < Ynum - y)
m_nRowsHigh[x] = Ynum - y;
if( y == 0)
{
re = 2;
}
}
m_nPoint += nPoint;
FormatInfo();
ClearArray();
return re;
}
void CElsGame::ClearArray()
{
int k,m;
BOOL bFind = FALSE;
for(int i = 0; i < Ynum; i++)
{
for(int j = 0; j <Xnum; j ++)
{
if (m_nArray[j][i] == 0)
{
goto t_1;
}
}
m_nRows ++;
for(m = i; m > 1; m --)
{
for(k = 0; k <Xnum; k ++)
{
m_nArray[k][m] = m_nArray[k][m - 1];
}
}
FormatInfo();
bFind = TRUE;
t_1:;
}
if(bFind)
{
for(k = 0; k < Xnum; k++)
{
m_nRowsHigh[k] = 0;
for(m = 0; m < Ynum; m ++)
{
if(m_nArray[k][m] == 1)
{
m_nRowsHigh[k] = Ynum - m;
break;
}
}
}
}
}
BOOL CElsGame::GetElsPosition(CEls &E
上一篇:
DisplayU.pas
下一篇:
如何当一个轻松的班主任