📄 els.c.txt
字号:
//head file Diamond.h
// Diamond.h: interface for the CDiamond class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DIAMOND_H__1B1DE2E7_A3B8_4D43_9079_AF2AC021F899__INCLUDED_)
#define AFX_DIAMOND_H__1B1DE2E7_A3B8_4D43_9079_AF2AC021F899__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class IGame; //inteface of game ,one mean a player
class IGameController; //inteface of game controller,control communication of players
typedef CTypedPtrList<CObList,IGame*> CGames; //all player in the game
class IGameController
{
//construction/destruction
public:
virtual ~IGameController(){};
//public interface
public:
virtual void OnEraseLines(IGame*,long) = 0 ; //when a player erase lines,call it
virtual void OnGameOver(IGame*) = 0 ; //when a player dies ,call it
};
//a block of a diamond
struct CBlock
{
long m_x ;
long m_y ;
};
//
class IGame:public CObject
{
//construction/destruction
public:
virtual ~IGame(){};
//public interface
public:
virtual void OnKey(long) = 0 ; //recieve key event
virtual void Start() = 0 ; //recieve start event
virtual bool IsOver()=0 ; //if the player die?
virtual void OnEraseLines(IGame* fromGame,long lines)=0; /* called by the game controller when other player erase lines .*/
virtual void Draw()=0; // draw self
};
//draw inteface
class IDrawDrv
{
//construction/destruction
public:
virtual ~IDrawDrv(){};
//public interface
public:
virtual ShowBlocks(long x,long y,long xs,long ys)=0; //show blocks
virtual HideBlocks(long x,long y,long xs,long ys)=0; //hide blocks
};
//implement of IDrawDrv of Windows
class CDrawDrvWindowsImp :public IDrawDrv
{
//construction/destruction
public:
CDrawDrvWindowsImp(POINT,COLORREF,COLORREF,long,CDC*);
virtual ~CDrawDrvWindowsImp(){};
private:
CDrawDrvWindowsImp(const CDrawDrvWindowsImp&);
void operator=(const CDrawDrvWindowsImp&);
//public interface
public:
virtual ShowBlocks(long x,long y,long xs,long ys);
virtual HideBlocks(long x,long y,long xs,long ys);
//implements
private:
POINT m_OrgPoint ;
COLORREF m_BlockColor; //block color
COLORREF m_BgColor; // bg color
long m_BlockSize; // pts of a block
CDC* m_pDC;
CBrush m_BlockBrush;
CBrush m_BorderBrush;
CBrush m_BgBrush;
};
//
class CGameController:public IGameController
{
//construction/destruction
public :
CGameController();
~CGameController();
private:
CGameController(const CGameController&);
void operator=(const CGameController&);
//public interface
public:
virtual void OnEraseLines(IGame*,long) ;//called by players ,when them erase lines
virtual void OnGameOver(IGame*); //called by players,when them die
void Start(CDC*); //start game!
void Pause(); //pause game,or restart game
void End(); //end game
void OnDraw(); //paint event
void OnKey(long key); //key event
//implements
private:
CGames m_Games;
CDC* m_pDC;
enum GamesStatus
{
STATUS_TO_START,
STATUS_PLAYING,
STATUS_PAUSE,
STATUS_END
};
GamesStatus m_Status;
void Clean();
};
class CKeyController //key config ,player can custome their key
{
//construction/destruction
public:
CKeyController(){};
~CKeyController(){};
private:
void operator=(const CKeyController&);
//public interface
public:
long RIGHT;
long LEFT ;
long DOWN;
long NEXT;
long DROP;
long START;
friend class CGame;
friend class CGameController;
//implements
};
//diamond
class CDiamond
{
//construction/destruction
public:
static CDiamond* CreateAnInstance(long x,long y); //random create a diamond
CDiamond(const CDiamond&);
~CDiamond();
private :
CDiamond();
void operator=(const CDiamond&);
//public interface
public:
long GetBlockCount(); //get blocks count, to support other type diamonds
CBlock* GetBlock(long); // get a block ,by index
void Left(); // left a diamond
void Right(); // right a diamond
void Down(); //down a diamond
void Next(); //retate a diamond
void Draw(IDrawDrv*,bool); //draw a diamond
long GetStartX(){return m_x;}; //get start x position
long GetStartY(){return m_y;}; // get start y position
//implements
private:
long m_x ;
long m_y ;
long m_index;
long m_Blocks;
CBlock* m_pBlocks;
};
//player of game
class CGame:public IGame
{
//construction/destruction
public:
CGame(IGameController*,IDrawDrv*,CKeyController,long wBlocks,long hBlocks);
virtual ~CGame();
private:
CGame(const CGame&);
void operator=(const CGame&);
//public interface
public:
virtual void OnKey(long); //recieve key event
virtual void Start(); //recieve start event
virtual bool IsOver(); //if is die?
virtual void OnEraseLines(IGame* fromGame,long lines);
void Draw();
//implements
private:
void Init(); //init
void LeftDiamond();
void RightDiamond();
bool CanDown();
void DownDiamond();
void NextDiamond();
void DropDiamond();
long EraseLines();
long GetBlockInfo(long,long); //get block info of game ,1 mean is have block,0 mean no block,
void SetBlockInfo(long x,long y,long v);
void OnDownADiamond(); //called when a diamond can not down more
bool IsValidDiamond(CDiamond*); //if the diamond is valid ?
void Clean(); //resource clean
void Show();//debug //debug use,to print data struct of the player
void OnGameOver();
private:
long m_WidthBlocks ;
long m_HeightBlocks ;
long* m_pBlocks;
CDiamond* m_pCurrDiamond;
IGameController* m_pController;
IDrawDrv* m_pDraw;
CKeyController m_KeyCtl;
enum GameStatus
{
STATUS_PLAYING,
STATUS_GAMEOVER
};
GameStatus m_Status ;
};
#endif // !defined(AFX_DIAMOND_H__1B1DE2E7_A3B8_4D43_9079_AF2AC021F899__INCLUDED_)
//implement Diamond.cpp
// Diamond.cpp: implementation of the CDiamond class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MDiamond.h"
#include "Diamond.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//block data
CBlock g_Blocks0[] = {{0,-1},{0,-2},{1,-1},{1,-2}};
CBlock g_Blocks1[] = {{0,-1},{1,-1},{1,-2},{1,-3}};
CBlock g_Blocks2[] = {{0,-2},{1,-2},{2,-2},{2,-1}};
CBlock g_Blocks3[] = {{1,-1},{1,-2},{1,-3},{2,-3}};
CBlock g_Blocks4[] = {{0,-1},{1,-1},{2,-1},{0,-2}};
CBlock g_Blocks5[] = {{1,-1},{1,-2},{1,-3},{2,-1}};
CBlock g_Blocks6[] = {{0,-1},{1,-1},{2,-1},{2,-2}};
CBlock g_Blocks7[] = {{1,-1},{1,-2},{1,-3},{0,-3}};
CBlock g_Blocks8[] = {{0,-1},{0,-2},{1,-2},{2,-2}};
CBlock g_Blocks9[] = {{0,-1},{1,-1},{2,-1},{1,-2}};
CBlock g_Blocks10[] = {{0,-2},{1,-1},{1,-2},{1,-3}};
CBlock g_Blocks11[] = {{1,-1},{0,-2},{1,-2},{2,-2}};
CBlock g_Blocks12[] = {{1,-1},{1,-2},{1,-3},{2,-2}};
CBlock g_Blocks13[] = {{0,-1},{0,-2},{1,-2},{1,-3}};
CBlock g_Blocks14[] = {{0,-2},{1,-2},{1,-1},{2,-1}};
CBlock g_Blocks15[] = {{1,-1},{1,-2},{0,-2},{0,-3}};
CBlock g_Blocks16[] = {{0,-1},{1,-1},{1,-2},{2,-2}};
CBlock g_Blocks17[] = {{0,-1},{1,-1},{2,-1},{3,-1}};
CBlock g_Blocks18[] = {{2,-1},{2,-2},{2,-3},{2,-4}};
CBlock* g_Blocks[] = {g_Blocks0,
g_Blocks1,
g_Blocks2,
g_Blocks3,
g_Blocks4,
g_Blocks5,
g_Blocks6,
g_Blocks7,
g_Blocks8,
g_Blocks9,
g_Blocks10,
g_Blocks11,
g_Blocks12,
g_Blocks13,
g_Blocks14,
g_Blocks15,
g_Blocks16,
g_Blocks17,
g_Blocks18};
long g_BlockNexts[] = {0,2,3,4,1,6,7,8,5,10,11,12,9,14,13,16,15,18,17};
//CDiamond
CDiamond::CDiamond()
{
}
CDiamond* CDiamond::CreateAnInstance(long x,long y)
{
static int LastIndex = 0 ;
CDiamond* d = new CDiamond();
srand((unsigned)time(NULL)+LastIndex);
long index = rand() % 19 ;
//long index = 17;
d->m_Blocks = 4 ;
d->m_index = index ;
d->m_x = x;
d->m_y = y;
d->m_pBlocks = g_Blocks ;
LastIndex = index ;
return d;
}
CDiamond::CDiamond(const CDiamond& d)
{
m_x = d.m_x ;
m_y = d.m_y ;
m_index = d.m_index;
m_Blocks = d.m_Blocks ;
m_pBlocks = d.m_pBlocks ;
}
CDiamond::~CDiamond()
{
}
long CDiamond::GetBlockCount()
{
return m_Blocks ;
}
CBlock* CDiamond::GetBlock(long i)
{
return &m_pBlocks;
}
void CDiamond::Left()
{
m_x -- ;
}
void CDiamond::Right()
{
m_x ++ ;
}
void CDiamond::Next()
{
m_index = g_BlockNexts;
m_pBlocks = g_Blocks;
}
void CDiamond::Down()
{
m_y -- ;
}
void CDiamond::Draw(IDrawDrv* d,bool bShow=true)
{
long count = GetBlockCount();
for(int i=0;i<count;i++)
{
CBlock* b = GetBlock(i);
if(bShow)
{
d->ShowBlocks(m_x+b->m_x,m_y+b->m_y,1,1);
}
else
{
d->HideBlocks(m_x+b->m_x,m_y+b->m_y,1,1);
}
}
}
//CGame
CGame::CGame(IGameController* p,IDrawDrv* d,CKeyController key,long wBlocks,long hBlocks)
: m_WidthBlocks(wBlocks),m_HeightBlocks(hBlocks),m_KeyCtl(key)
{
m_pController = p ;
//pointers,need to be clean
m_pBlocks = NULL;
m_pCurrDiamond = NULL;
m_pDraw = d ;
Init();
}
void CGame::OnKey(long keycode)
{
if(keycode==-1) //-1 mean down
{
DownDiamond();
}
if(keycode==m_KeyCtl.LEFT)
{
LeftDiamond();
}
if(keycode==m_KeyCtl.RIGHT)
{
RightDiamond();
}
if(keycode==m_KeyCtl.DOWN)
{
DownDiamond();
}
if(keycode==m_KeyCtl.NEXT)
{
NextDiamond();
}
if(keycode==m_KeyCtl.DROP)
{
DropDiamond();
}
}
CGame::~CGame()
{
Clean();
}
void CGame::Init()
{
if(NULL==m_pBlocks)
{
long count = m_WidthBlocks * m_HeightBlocks ;
m_pBlocks = new long;
for(int i=0 ;i<count;i++)
{
m_pBlocks = 0;
}
}
if(NULL==m_pCurrDiamond)
{
m_pCurrDiamond = CDiamond::CreateAnInstance(m_WidthBlocks/2,m_HeightBlocks);
}
}
void CGame::Clean()
{
if(m_pCurrDiamond!=NULL) delete m_pCurrDiamond;
if(m_pBlocks!=NULL) delete [] m_pBlocks;
if(m_pDraw!=NULL) delete m_pDraw ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -