7色球游戏

初次玩这个游戏是刚入大学的时候,我手机上带的小游戏。后来手机丢了,就再也没有玩过了。

前天逛csdn的时候看到这个文章:http://student.csdn.net/space.php?uid=108270&do=blog&id=34114&page=2#content

 

心血来潮,想自己用VC++实现一个,权当是怀旧了。。

 

7色球游戏_第1张图片

效果就是这个样子了。所有的颜色都可以订制:背景色,球颜色,网格颜色

代码只实现了基本的功能,没有写计分之类的代码。

 

我的开发环境是:vs2008 + sp1 + windows 7

如果你想编译下面的代码,必须得有vs2010 或者 vs2008 安装sp1

 

代码封装的比较完整,你只需要提供一个可供在上面绘图的 GUI 窗体,

使用调用这个代码:

BOOL CGamePanel::InitializeGamePanel(HWND hWndClient,CRect &rect,int row);

初始化一些设置。

响应重绘的时候调用:void CGamePanel::DrawGamePanel()

响应鼠标消息调用:void CGamePanel::OnClick(const CPoint &pt);

 

就可以得到一个 七色球游戏画面了

SetGridColor 等函数可以设置一些界面信息。。背景色我没有提供接口,跟SetGridColor一样实现。

 

此代码中的两个方格之间的最短路径查找代码如下:

BOOL CBallContainer::FindMinPath(IN QCoor &bc1, IN QCoor &bc2/*, OUT QPath &path*/) { ASSERT(bc1 != bc2); /* path.clear();*/ QBallCoorSet s0; s0.insert(bc1); QVecSet vs; vs.push_back(s0); QMultiPath mulPath; mulPath.push_back(vs); TRACE(_T("查找从<%d,%d> 到 <%d,%d>的路径:/n"),bc1.row,bc1.col,bc2.row,bc2.col); return FindPath(bc2,vs,mulPath); } void CBallContainer::TraceCoorSet(const QBallCoorSet &s) { TRACE(_T("/n--------------------------------/n")); for (QBallCoorSet::const_iterator itr = s.begin(); itr != s.end(); ++itr) { TRACE(_T("<%d,%d>/n"),itr->row,itr->col); } TRACE(_T("/n--------------------------------/n")); } BOOL CBallContainer::FindPath(const QCoor &dest,QVecSet &vsCoors,QMultiPath &mulPath) { QVecSet vs; for (QVecSet::size_type i = 0; i < vsCoors.size(); i++) { for (QBallCoorSet::iterator itr = vsCoors[i].begin(); itr != vsCoors[i].end(); ++itr) { QBallCoorSet sx; // add 4 neighbour balls if (IsNeighbour(dest,*itr)) { TRACE(_T("到达目的地/n")); return TRUE; } Add4Way(sx,*itr); #ifdef _DEBUG TRACE(_T("<%d,%d>添加四周路径:"),itr->row,itr->col); TraceCoorSet(sx); #endif // excluse the coordinates which are already exist in paths set ExcluseCoors(sx,mulPath); #ifdef _DEBUG TRACE(_T("排除已有路径:")); TraceCoorSet(sx); #endif // add new coors if (sx.size() > 0) { vs.push_back(sx); } } } if (vs.size() > 0) { mulPath.push_back(vs); return FindPath(dest,vs,mulPath); } return FALSE; }

 

广度优先遍历。使用了递归实现,特别是各个容器的数据频繁插入删除,效率不怎么好。

如果谁有兴趣,可以实现一个更好点的路径查找。


 

不废话了。我就直接贴代码吧。

头文件:

#pragma once // mfc sp1 #include <afxdrawmanager.h> #include <QBDef.h> #include <XTrace.h> // std c++ #include <algorithm> #include <iterator> #include <vector> #include <stack> #include <set> using std::set; using std::stack; using std::vector; // ball's coordinate in the gamepanel struct QCoor { typedef QCoor self; QCoor(int r=-1,int c=-1) {row = r; col = c;} int row; int col; bool operator==(const self & bc) { return ((row == bc.row) && (col == bc.col)); } bool operator!=(const self& bc) { return !(*this == bc); } bool operator<(const self& bc)const { if (row == bc.row) return (col < bc.col); else return (row < bc.row); } }; class CDrawMan : public CDrawingManager { public: CDrawMan(HWND hWnd): CDrawingManager(*CDC::FromHandle(::GetDC(hWnd))) { m_hWnd = hWnd; } ~CDrawMan() { if (::IsWindow(m_hWnd)) { ::ReleaseDC(m_hWnd,m_dc.GetSafeHdc()); } } private: HWND m_hWnd; }; // ball struct QBall { typedef QBall self; QBall() { ZeroMemory(this,sizeof(QBall)); } COLORREF color; BOOL exist; // QBallCoor coor; bool operator==(const self& ball) { return (exist && ball.exist && (color == ball.color)); } // bool operator!=(const self& ball) // { // return !(*this == ball); // } }; #define NULL_QBALL QBall() #define NULL_COOR QCoor() ////////////////////////////////////////////////////////////////////////// class CBallContainer { public: typedef set<QCoor> QBallCoorSet; typedef vector<QBallCoorSet> QVecSet; typedef vector<QVecSet> QMultiPath; typedef vector<QCoor> QVecCoor; typedef vector<vector<QBall> > BallContainer; public: CBallContainer(void); ~CBallContainer(void); public: void GetCoors(QVecCoor &vec,BOOL bCoorExistBall=TRUE); protected: // trace helper void TraceCoorSet(const QBallCoorSet &s); public: // move ball to another place void MoveBallToCoor(const QCoor &src,const QCoor &dest); BOOL SetRowCol(int row); // find a way from bc1 to bc2 BOOL FindMinPath(IN QCoor &bc1, IN QCoor &bc2/*, OUT QPath &path*/); BOOL FindPath(const QCoor &dest,QVecSet &vsCoors,QMultiPath &mulPath); // find continuous ball paths that with argument ball BOOL FindContinousBallWith(IN const QCoor &coor, OUT QVecCoor & path); // add 4 neighbourhood void Add4Way(QBallCoorSet & sx,const QCoor ballcoor); // is neighbour BOOL IsNeighbour(const QCoor &b1,const QCoor &b2); // ball exist BOOL IsBallExist(QCoor ballcoor); BOOL IsValidCoor(QCoor ballcoor); // ball from coor BOOL BallFromCoor(const QCoor& coor,QBall &ball); // set - void ExcluseCoors(IN_OUT QBallCoorSet &setSrc, IN const QBallCoorSet &setExcluse); // find path helper void ExcluseCoors(IN_OUT QBallCoorSet &setSrc, IN const QMultiPath &coors); // add ball void AddBall(const QCoor &coor,COLORREF clr); // remove balls void RemoveBalls(QVecCoor & coors); private: int m_Row; BallContainer m_Balls; }; ////////////////////////////////////////////////////////////////////////// // present a game block in a GUI client class CGamePanel { public: CGamePanel(void); ~CGamePanel(void); public: // initialize game panel BOOL InitializeGamePanel(HWND hWndClient,CRect &rect,int row); // set colors BOOL Set7Colors(const COLORREF color[]); // free resource void FreeResource(); // generate three balls randomly void Generate3Ball(); // get BallCoor by coordinate CRect RectFromCoor(const QCoor &ball); BOOL CoorFromPt(const CPoint &pt,QCoor &ballcoor); BOOL BallFromPt(const CPoint &pt,QBall &ball); // Click in panel void OnClick(IN const CPoint &pt); // set Grid void SetGridColor(COLORREF clrGrid); // draw Grid pieces that in rect void DrawGrid(); // draw game panel void DrawGamePanel(); // draw all balls void DrawAllCoors(); void DrawRightBlock(); void Draw3RandomBall(); // draw ball void DrawCoor(const QCoor &coor); // hit ball void HitCoor(const QCoor &ball); // add 3 random ball void Add3RanomBall(); void CheckFullLine(const QCoor &coor); private: int m_iNewBall; QCoor m_CurCoor; HWND m_hWndClient; COLORREF m_clrGrid; COLORREF m_clrBK; CRect m_RectPanel; CRect m_RectRight; int m_GridWidth; COLORREF m_3Ball[3]; // current 3 random ball COLORREF m_7Color[7]; // 7 colors CBallContainer m_BallContainer; };

 

实现文件:

#include "StdAfx.h" #include "ColorBall.h" ////////////////////////////////////////////////////////////////////////// const COLORREF g_BALL7COLOR[] = { COLOR_PUREBLUE, COLOR_PUREGREEN, COLOR_PURERED, COLOR_PLUM, COLOR_BROWN, COLOR_PEACHPUFF, COLOR_ORANGE }; ////////////////////////////////////////////////////////////////////////// CBallContainer::CBallContainer(void) { } CBallContainer::~CBallContainer(void) { } BOOL CBallContainer::SetRowCol(int row) { if (row < 10) return FALSE; m_Row = row; for (int r = 0; r < row; r++) { vector<QBall> vecCol; for (int c = 0; c < row; c++) { vecCol.push_back(NULL_QBALL); } TRACE(_T("VecCol size:%d/n"),vecCol.size()); m_Balls.push_back(vecCol); } TRACE(_T("BallContainer Row:%d Col:%d/n"),m_Balls.size(),m_Balls[0].size()); return TRUE; } void CBallContainer::AddBall(const QCoor &coor,COLORREF clr) { ASSERT(IsValidCoor(coor)); ASSERT(!IsBallExist(coor)); int r = coor.row; int c = coor.col; m_Balls[r][c].color = clr; m_Balls[r][c].exist = clr; } void CBallContainer::RemoveBalls(QVecCoor & coors) { QVecCoor::size_type len = coors.size(); for(QVecCoor::size_type i = 0; i < len; i++) { QCoor &coor = coors[i]; if (IsValidCoor(coor)) { m_Balls[coor.row][coor.col].exist = false; } } } BOOL CBallContainer::IsValidCoor(QCoor ballcoor) { if (ballcoor.row >= 0 && ballcoor.row < m_Row) { return (ballcoor.col >= 0 && ballcoor.col < m_Row); } return FALSE; } void CBallContainer::MoveBallToCoor(const QCoor &src,const QCoor &dest) { ASSERT(IsValidCoor(dest)); ASSERT(!IsBallExist(dest)); ASSERT(IsBallExist(src)); int rSrc = src.row; int cSrc = src.col; int rDest = dest.row; int cDest = dest.col; m_Balls[rDest][cDest] = m_Balls[rSrc][cSrc]; m_Balls[rSrc][cSrc] = NULL_QBALL; } void CBallContainer::GetCoors(QVecCoor &vec,BOOL bCoorExistBall/*=TRUE*/) { for (int r = 0; r < m_Row; r++) { for (int c = 0; c < m_Row; c++) { if (m_Balls[r][c].exist == bCoorExistBall) { vec.push_back(QCoor(r,c)); } } } } BOOL CBallContainer::IsBallExist(QCoor ballcoor) { if (IsValidCoor(ballcoor)) return m_Balls[ballcoor.row][ballcoor.col].exist; return FALSE; } BOOL CBallContainer::BallFromCoor(const QCoor& coor,QBall &ball) { if (IsBallExist(coor)) { ball = m_Balls[coor.row][coor.col]; return TRUE; } ZeroMemory(&ball,sizeof(QBall)); return FALSE; } BOOL CBallContainer::FindContinousBallWith(IN const QCoor &coor, OUT QVecCoor & path) { if (!IsBallExist(coor)) return FALSE; path.clear(); path.push_back(coor); int r,c; QBall ball; BallFromCoor(coor,ball); //--------------------------------------------------------- // horizon r = coor.row; QVecCoor vTemp; // to left for (c = coor.col - 1; (c >= 0) && (ball == m_Balls[r][c]); c--) { vTemp.push_back(QCoor(r,c)); } // to right for (c = coor.col + 1; (c < m_Row) && (ball == m_Balls[r][c]); c++) { vTemp.push_back(QCoor(r,c)); } if (vTemp.size() >= 4) std::copy(vTemp.begin(),vTemp.end(),std::back_insert_iterator<QVecCoor>(path)); vTemp.clear(); //--------------------------------------------------------- // verical c = coor.col; // to up for (r = coor.row - 1; (r >= 0) && (ball == m_Balls[r][c]); r--) { vTemp.push_back(QCoor(r,c)); } // to down for (r = coor.row + 1; (r < m_Row) && (ball == m_Balls[r][c]); r++) { vTemp.push_back(QCoor(r,c)); } if (vTemp.size() >= 4) std::copy(vTemp.begin(),vTemp.end(),std::back_insert_iterator<QVecCoor>(path)); vTemp.clear(); //--------------------------------------------------------- // lefttop to rightbottom // to lefttop for (r = coor.row - 1 ,c = coor.col - 1; (r >= 0) && (c >= 0) && (ball == m_Balls[r][c]); r--,c--) { vTemp.push_back(QCoor(r,c)); } // to bottom right for (r = coor.row + 1,c = coor.col + 1; (r < m_Row) && (c < m_Row) && (ball == m_Balls[r][c]); r++,c++) { vTemp.push_back(QCoor(r,c)); } if (vTemp.size() >= 4) std::copy(vTemp.begin(),vTemp.end(),std::back_insert_iterator<QVecCoor>(path)); vTemp.clear(); //--------------------------------------------------------- // leftbottom to righttop // to leftbottom for (r = coor.row + 1 ,c = coor.col - 1; (r < m_Row) && (c >= 0) && (ball == m_Balls[r][c]); r++,c--) { vTemp.push_back(QCoor(r,c)); } // to right top for (r = coor.row - 1,c = coor.col + 1; (r >= 0) && (c < m_Row) && (ball == m_Balls[r][c]); r--,c++) { vTemp.push_back(QCoor(r,c)); } if (vTemp.size() >= 4) std::copy(vTemp.begin(),vTemp.end(),std::back_insert_iterator<QVecCoor>(path)); vTemp.clear(); return path.size() > 4; } BOOL CBallContainer::FindMinPath(IN QCoor &bc1, IN QCoor &bc2/*, OUT QPath &path*/) { ASSERT(bc1 != bc2); /* path.clear();*/ QBallCoorSet s0; s0.insert(bc1); QVecSet vs; vs.push_back(s0); QMultiPath mulPath; mulPath.push_back(vs); TRACE(_T("查找从<%d,%d> 到 <%d,%d>的路径:/n"),bc1.row,bc1.col,bc2.row,bc2.col); return FindPath(bc2,vs,mulPath); } void CBallContainer::TraceCoorSet(const QBallCoorSet &s) { TRACE(_T("/n--------------------------------/n")); for (QBallCoorSet::const_iterator itr = s.begin(); itr != s.end(); ++itr) { TRACE(_T("<%d,%d>/n"),itr->row,itr->col); } TRACE(_T("/n--------------------------------/n")); } BOOL CBallContainer::FindPath(const QCoor &dest,QVecSet &vsCoors,QMultiPath &mulPath) { QVecSet vs; for (QVecSet::size_type i = 0; i < vsCoors.size(); i++) { for (QBallCoorSet::iterator itr = vsCoors[i].begin(); itr != vsCoors[i].end(); ++itr) { QBallCoorSet sx; // add 4 neighbour balls if (IsNeighbour(dest,*itr)) { TRACE(_T("到达目的地/n")); return TRUE; } Add4Way(sx,*itr); #ifdef _DEBUG TRACE(_T("<%d,%d>添加四周路径:"),itr->row,itr->col); TraceCoorSet(sx); #endif // excluse the coordinates which are already exist in paths set ExcluseCoors(sx,mulPath); #ifdef _DEBUG TRACE(_T("排除已有路径:")); TraceCoorSet(sx); #endif // add new coors if (sx.size() > 0) { vs.push_back(sx); } } } if (vs.size() > 0) { mulPath.push_back(vs); return FindPath(dest,vs,mulPath); } return FALSE; } void CBallContainer::ExcluseCoors(IN_OUT QBallCoorSet &setSrc, IN const QMultiPath &coors) { for (QMultiPath::size_type i = 0; i < coors.size(); i++) { for (size_t j = 0; j < coors[i].size(); j++) { ExcluseCoors(setSrc,coors[i][j]); } } } void CBallContainer::ExcluseCoors(IN_OUT QBallCoorSet &setSrc, IN const QBallCoorSet &setExcluse) { for (QBallCoorSet::const_iterator itr = setExcluse.begin(); itr != setExcluse.end(); ++itr) { setSrc.erase(*itr); } } BOOL CBallContainer::IsNeighbour(const QCoor &b1,const QCoor &b2) { ASSERT(IsValidCoor(b1)); ASSERT(IsValidCoor(b2)); if (b1.row == b2.row) return (abs(b1.col - b2.col) == 1); if (b1.col == b2.col) return (abs(b1.row - b2.row) == 1); return FALSE; } void CBallContainer::Add4Way(QBallCoorSet & sx, const QCoor ballcoor) { int row = ballcoor.row; int col = ballcoor.col; QBall ball; // up if (row > 0) { if (!(m_Balls[row-1][col].exist)) sx.insert(QCoor(row-1,col)); } // left if (col > 0) { if (!(m_Balls[row][col-1].exist)) sx.insert(QCoor(row,col-1)); } // right if (col < m_Row - 1) { if (!(m_Balls[row][col+1].exist)) sx.insert(QCoor(row,col+1)); } // down if (row < m_Row - 1) { if (!(m_Balls[row+1][col].exist)) sx.insert(QCoor(row+1,col)); } } ////////////////////////////////////////////////////////////////////////// CGamePanel::CGamePanel(void) { } CGamePanel::~CGamePanel(void) { FreeResource(); } void CGamePanel::FreeResource() { } // initialize game panel BOOL CGamePanel::InitializeGamePanel(HWND hWndClient,CRect &rect,int row) { ASSERT(::IsWindow(hWndClient)); m_hWndClient = hWndClient; FreeResource(); //init m_RectRight = rect; m_RectPanel = rect; // init ASSERT(row >= 10); m_BallContainer.SetRowCol(row); // the rect will be justified m_GridWidth = m_RectPanel.Width() / (row + 4); m_RectPanel.right = (m_GridWidth * row) + m_RectPanel.left; // in pixel m_RectPanel.bottom = m_RectPanel.Width() + m_RectPanel.top; m_RectRight = m_RectPanel; m_RectRight.left = m_RectPanel.right + m_GridWidth; m_RectRight.right = m_RectRight.left + 3 * m_GridWidth; //-------------------------------------------- Set7Colors(g_BALL7COLOR); Generate3Ball(); m_iNewBall = 1; DrawGamePanel(); m_clrBK = COLOR_LIGHTPINK; return TRUE; } BOOL CGamePanel::Set7Colors(const COLORREF color[]) { for (int i = 0; i < 7; i++) { for (int j = 0; j < i; j++) { if (color[i] == color[j]) return FALSE; } m_7Color[i] = color[i]; } return TRUE; } void CGamePanel::Generate3Ball() { for (int i = 0; i < 3; i++) { m_3Ball[i] = m_7Color[rand()%7]; } } void CGamePanel::SetGridColor(COLORREF clrGrid) { m_clrGrid = clrGrid; DrawGrid(); } void CGamePanel::DrawGrid() { CDrawMan dm(m_hWndClient); // draw V line for (int x = m_RectPanel.left; x <= m_RectPanel.right; x += m_GridWidth) { dm.DrawLine(x,m_RectPanel.top, x, m_RectPanel.bottom, m_clrGrid); } // draw H line for (int y = m_RectPanel.top; y <= m_RectPanel.bottom; y += m_GridWidth) { dm.DrawLine(m_RectPanel.left, y, m_RectPanel.right, y, m_clrGrid); } } CRect CGamePanel::RectFromCoor(const QCoor &coor) { if (!m_BallContainer.IsValidCoor(coor)) { return CRect(0,0,0,0); } int row = coor.row; int col = coor.col; return CRect(m_RectPanel.top + col * m_GridWidth, m_RectPanel.left + row * m_GridWidth, m_RectPanel.top + (col + 1) * m_GridWidth + 1, m_RectPanel.left + (row + 1) * m_GridWidth + 1 ); } ////////////////////////////////////////////////////////////////////////// // Draw Functions void CGamePanel::DrawCoor(const QCoor &coor) { if (!m_BallContainer.IsValidCoor(coor)) return ; CDrawMan dm(m_hWndClient); CRect rect = RectFromCoor(coor); // draw grid and background dm.DrawRect(rect,m_clrBK,m_clrGrid); // draw ball QBall ball; if (m_BallContainer.BallFromCoor(coor,ball)) { rect.DeflateRect(5,5); dm.DrawEllipse(rect,ball.color,RANDOM_COLOR); } } void CGamePanel::DrawAllCoors() { CBallContainer::QVecCoor vec; m_BallContainer.GetCoors(vec); for (CBallContainer::QVecCoor::size_type i = 0; i < vec.size(); i++) { DrawCoor(vec[i]); } // draw current select ball if (m_CurCoor != NULL_COOR) HitCoor(m_CurCoor); } void CGamePanel::DrawGamePanel() { DrawGrid(); DrawAllCoors(); DrawRightBlock(); } void CGamePanel::DrawRightBlock() { CDrawMan dm(m_hWndClient); dm.DrawRect(m_RectRight,RGB(224,245,250),COLOR_PURERED); Draw3RandomBall(); } void CGamePanel::Draw3RandomBall() { CRect rect = m_RectRight; rect.bottom = rect.top + m_GridWidth; CDrawMan dm(m_hWndClient); for (int i = 0; i < 3; i++) { rect.left = m_RectRight.left + i * m_GridWidth; rect.right = rect.left + m_GridWidth; CRect rc = rect; if (m_iNewBall == i) { dm.DrawRect(rect,COLOR_WHITE,COLOR_PURERED); rc.DeflateRect(2,2); dm.DrawEllipse(rc,m_3Ball[i],COLOR_PURERED); } else { dm.DrawRect(rect,COLOR_WHITE,COLOR_PURERED); rc.DeflateRect(5,5); dm.DrawEllipse(rc,m_3Ball[i],COLOR_PUREBLUE); } } } // hitted coor,if this coor has a ball , // then draw a bigger ball void CGamePanel::HitCoor(const QCoor &coor) { DrawCoor(coor); QBall ball; if (m_BallContainer.BallFromCoor(coor,ball)) { CRect rect = RectFromCoor(coor); rect.DeflateRect(3,3); CDrawMan dm(m_hWndClient); dm.DrawEllipse(rect,ball.color,COLOR_PURERED); } } ////////////////////////////////////////////////////////////////////////// BOOL CGamePanel::BallFromPt(const CPoint &pt,QBall &ball) { QCoor coor; if (CoorFromPt(pt,coor)) return m_BallContainer.BallFromCoor(coor,ball); return FALSE; } BOOL CGamePanel::CoorFromPt(const CPoint &pt,QCoor &coor) { if (m_RectPanel.PtInRect(pt)) { coor.row = (pt.y - m_RectPanel.top) / m_GridWidth; coor.col = (pt.x - m_RectPanel.left) / m_GridWidth; return TRUE; } return FALSE; } void CGamePanel::OnClick(const CPoint &pt) { if (m_RectRight.PtInRect(pt)) { // hit the 3 ball?? CRect rect = m_RectRight; rect.bottom = rect.top + m_GridWidth; if (rect.PtInRect(pt)) { // unselect balls DrawCoor(m_CurCoor); m_CurCoor = NULL_COOR; // draw the 3 balls; m_iNewBall = (pt.x - rect.left) / m_GridWidth; Draw3RandomBall(); } } else if (m_RectPanel.PtInRect(pt)) { // 只有点击在游戏面板中才做反应 QCoor coorHitted; if (CoorFromPt(pt,coorHitted)) { if ((coorHitted != NULL_COOR) && (coorHitted == m_CurCoor)) return ; // the same ball if (m_BallContainer.IsBallExist(coorHitted)) { // hitted a ball HitCoor(coorHitted); if (m_CurCoor != NULL_COOR) DrawCoor(m_CurCoor); // set the current ball m_CurCoor = coorHitted; // now unselect the 3 ball m_iNewBall = -1; Draw3RandomBall(); } else { // the coor has no ball if (m_CurCoor != NULL_COOR) { // current select ball ASSERT(m_BallContainer.IsBallExist(m_CurCoor)); // can move the ball to the coorHitted? if (m_BallContainer.FindMinPath(m_CurCoor,coorHitted)) {// 这两个坐标之间有通路 // 移动球到刚点击的地方 m_BallContainer.MoveBallToCoor(m_CurCoor,coorHitted); // 重绘两个坐标 // now there is no current select ball DrawCoor(m_CurCoor); m_CurCoor = NULL_COOR; // draw the coorhitted ball DrawCoor(coorHitted); // line is full? CheckFullLine(coorHitted); Add3RanomBall(); } } else if (m_iNewBall >= 0 && m_iNewBall < 3) { // add new ball ASSERT(m_iNewBall >= 0 && m_iNewBall < 3); m_BallContainer.AddBall(coorHitted,m_3Ball[m_iNewBall]); // after add ,show it DrawCoor(coorHitted); CheckFullLine(coorHitted); // add 3 balls Add3RanomBall(); } } } } } void CGamePanel::CheckFullLine(const QCoor &coor) { CBallContainer::QVecCoor path; if (m_BallContainer.FindContinousBallWith(coor,path)) { m_BallContainer.RemoveBalls(path); for (CBallContainer::QVecCoor::size_type i = 0; i < path.size(); i++) { DrawCoor(path[i]); } } } void CGamePanel::Add3RanomBall() { // and add 3 random ball at random place CBallContainer::QVecCoor coorEmpty; m_BallContainer.GetCoors(coorEmpty,FALSE); CBallContainer::QVecCoor::size_type len = coorEmpty.size(); if (len < 3) { // that is it,Game Over return ; } // 1 QCoor coorAdd = coorEmpty[len/2]; m_BallContainer.AddBall(coorAdd,m_3Ball[1]); // after added ,show it DrawCoor(coorAdd); CheckFullLine(coorAdd); // 2 /* coorAdd = coorEmpty[len/4]; m_BallContainer.AddBall(coorAdd,m_3Ball[2]); // after added ,show it DrawCoor(coorAdd); CheckFullLine(coorAdd); */ // 3 coorAdd = coorEmpty[len*2/3]; m_BallContainer.AddBall(coorAdd,m_3Ball[2]); // after added ,show it DrawCoor(coorAdd); CheckFullLine(coorAdd); // generate another 3 ball Generate3Ball(); Draw3RandomBall(); m_iNewBall = -1; }

 

工程文件就不发了。

 

写了两天,还得做好多事情,挺累的,不想再写了。谁要是看不懂的话,可以在下面留言,

如果有兴趣的话也可以到我的另外一个博客:www.nogod.info 去看看 (这算个广告了。)

你可能感兴趣的:(null,iterator,Random,insert,Path,colors)