📄 tetriscontrol.cpp
字号:
//
//TetrisControl.cpp
//
// -- The implementation file of CTetrisControl class
//
///////////////////////////////////////////////////////
#include "stdafx.h"
#include "TetrisControl.h"
#include "ShapeFile.h"
CBasicShape CTetrisControl::m_ShapeList[SHAPES_NUMBER][4];
int CTetrisControl::m_nScoreLevel[BLOCKS_IN_SHAPE];
int CTetrisControl::m_nShapeRotateState[SHAPES_NUMBER];
CTetrisControl::CTetrisControl(int nBlockColorsNum)
{
m_nBlockColorsNum = nBlockColorsNum;
m_nCurShapeType = -1;
m_nCurShapeDir = -1;
m_nHighestBlock = 0;
m_nInitFloors = 0;
for(int i = 0; i < BLOCKS_IN_SHAPE; i++)
m_nScoreLevel[i] = 0;
InitWalls();
}
CTetrisControl::~CTetrisControl()
{
}
void CTetrisControl::InitWalls()
{
for(int j = 0; j < VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS * 2; j++)
for(int i = 0; i < WALL_THICKNESS; i++)
{
m_Blocks[i][j].m_nStatus = HAVE_BLOCK;
m_Blocks[HORIZONTAL_BLOCKS_NUMBER + WALL_THICKNESS + i][j].m_nStatus = HAVE_BLOCK;
}
for(int i = WALL_THICKNESS; i < HORIZONTAL_BLOCKS_NUMBER + WALL_THICKNESS; i++)
for(int j = 0; j < WALL_THICKNESS; j++)
{
m_Blocks[i][j].m_nStatus = NO_BLOCK;
m_Blocks[i][VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS + j] = HAVE_BLOCK;
}
}
BOOL CTetrisControl::InitBasicShapes()
{
int nShapeListIndex = 0;
int nBlockNum;
for(int i = 0; i < SHAPES_NUMBER; i++)
{
nBlockNum = nShapeList[nShapeListIndex++];
for(int j = 0; j < 4; j++)
{
m_ShapeList[i][j].m_bBlocks = nBlockNum;
for(int k = 0; k < nBlockNum; k++)
{
m_ShapeList[i][j].m_ptBlocks[k].x = nShapeList[nShapeListIndex++];
m_ShapeList[i][j].m_ptBlocks[k].y = nShapeList[nShapeListIndex++];
}
}
}
CalcShapeRotateState();
return TRUE;
}
BOOL CTetrisControl::InitScoreLevel(const int nScoreLevel[])
{
for(int i = 0; i < BLOCKS_IN_SHAPE; i++)
m_nScoreLevel[i] = nScoreLevel[i];
return TRUE;
}
void CTetrisControl::SetInitFloors(int nInitFloors)
{
m_nInitFloors = nInitFloors;
}
BOOL CTetrisControl::InitFloors(int nInitFloors, int nBlockSeed)
{
if(nInitFloors < 0 || nInitFloors > VERTICAL_BLOCKS_NUMBER)
return FALSE;
m_nHighestBlock = nInitFloors;
for(int i = 0; i < VERTICAL_BLOCKS_NUMBER; i++)
m_nCurBlocksInFloor[i] = 0;
// initialize the exist blocks
m_nBlockSeed = nBlockSeed;
BYTE nStatus;
for(int j = 0; j < nInitFloors; j++)
{
CalcBlocksStatusInFloor();
for(int i = WALL_THICKNESS; i < HORIZONTAL_BLOCKS_NUMBER + WALL_THICKNESS; i ++)
{
// choose a existence status randomly
if((nStatus = m_bBlocksStatusInFloor[i - WALL_THICKNESS])== HAVE_BLOCK)
m_nCurBlocksInFloor[j]++;
m_Blocks[i][VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS - 1 - j].m_nStatus = nStatus;
// choose a color randomly
m_nBlockSeed = Rand(m_nBlockSeed);
m_Blocks[i][VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS - 1 - j].m_nColor
= m_nBlockSeed * m_nBlockColorsNum / OWN_RAND_MAX;
m_Blocks[i][VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS - 1 - j].m_nStyle
= int(rand()*32/RAND_MAX);
}
}
// clear other positions
for(; j < VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS; j++)
for(int i = WALL_THICKNESS; i < HORIZONTAL_BLOCKS_NUMBER + WALL_THICKNESS; i ++)
m_Blocks[i][VERTICAL_BLOCKS_NUMBER + WALL_THICKNESS - 1 - j].m_nStatus = NO_BLOCK;
return TRUE;
}
void CTetrisControl::CalcShapeRotateState()
{
for(int i = 0; i < SHAPES_NUMBER; i++)
{
if(m_ShapeList[i][0].m_bBlocks == 1)
{
m_nShapeRotateState[i] = 1;
break;
}
if(m_ShapeList[i][0] == m_ShapeList[i][2] && m_ShapeList[i][1] == m_ShapeList[i][3])
{
if(m_ShapeList[i][0] == m_ShapeList[i][1])
m_nShapeRotateState[i] = 4;
else
m_nShapeRotateState[i] = 2;
}
else
m_nShapeRotateState[i] = 4;
}
}
void CTetrisControl::Start(int nSeed)
{
// reset some members
m_nFloorsBuffer = 0;
m_nShapeSeed = nSeed;
m_fSmoothDown = 0;
m_fSmoothRotate = 0;
m_bIsGameOver = FALSE;
m_nCurDelFloors = 0;
m_bIsWillFix = TRUE;
InitFloors(m_nInitFloors, nSeed);
m_nCurScore = 0;
for(int i = 0; i < BLOCKS_IN_SHAPE; i++)
m_nCramedFloors[i] = -1;
CreateShape(FALSE);
CreateShape();
}
void CTetrisControl::CreateShape(BOOL bIsNext)
{
m_RotateClockwise = FALSE;
m_fSmoothRotate = 0;
m_nShapeSeed = Rand(m_nShapeSeed);
int nShapeType = m_nShapeSeed * (SHAPES_NUMBER-1) / OWN_RAND_MAX;
int nDirection = DIRECTION_UP;
if(bIsNext)
{
m_NextShape = CreateSpecificShape(nShapeType, nDirection);
m_nNextShapeType = nShapeType;
m_nNextShapeDir = nDirection;
}
else
{
m_CurShape = CreateSpecificShape(nShapeType, nDirection);
m_nCurShapeType = nShapeType;
m_nCurShapeDir = nDirection;
}
}
CShape CTetrisControl::CreateSpecificShape(int nShapeType, int nDirection)
{
int nMidPosX = (HORIZONTAL_BLOCKS_NUMBER - 1) / 2;
CShape shape;
shape = m_ShapeList[nShapeType][nDirection];
shape.m_nColor = nShapeType;
shape.m_ptPos = CPoint(nMidPosX, 0);
return shape;
}
BOOL CTetrisControl::OnLeft()
{
int x, y;
for(int i = 0; i < m_CurShape.m_bBlocks; i++)
{
x = m_CurShape.m_ptPos.x + WALL_THICKNESS + m_CurShape.m_ptBlocks[i].x - 1;
y = m_CurShape.m_ptPos.y + WALL_THICKNESS + m_CurShape.m_ptBlocks[i].y;
if(m_Blocks[x][y].m_nStatus == HAVE_BLOCK)
return FALSE;
}
m_CurShape.m_ptPos.x--;
return TRUE;
}
BOOL CTetrisControl::OnRight()
{
int x, y;
for(int i = 0; i < m_CurShape.m_bBlocks; i++)
{
x = m_CurShape.m_ptPos.x + WALL_THICKNESS + m_CurShape.m_ptBlocks[i].x + 1;
y = m_CurShape.m_ptPos.y + WALL_THICKNESS + m_CurShape.m_ptBlocks[i].y;
if(m_Blocks[x][y].m_nStatus == HAVE_BLOCK)
return FALSE;
}
m_CurShape.m_ptPos.x++;
return TRUE;
}
BOOL CTetrisControl::OnRotate()
{
CShape shape;
int nDirection;
if((nDirection = m_nCurShapeDir + 1) == 4)
nDirection = 0;
shape = m_ShapeList[m_nCurShapeType][nDirection];
int x, y;
for(int i = 0; i < m_CurShape.m_bBlocks; i++)
{
x = m_CurShape.m_ptPos.x + WALL_THICKNESS + shape.m_ptBlocks[i].x;
y = m_CurShape.m_ptPos.y + WALL_THICKNESS + shape.m_ptBlocks[i].y;
if(m_Blocks[x][y].m_nStatus == HAVE_BLOCK)
return FALSE;
}
m_CurShape = m_ShapeList[m_nCurShapeType][nDirection];
m_nCurShapeDir = nDirection;
return TRUE;
}
BOOL CTetrisControl::OnStartSmoothRotate()
{
BOOL bResult = OnRotate();
if(bResult)
{
if(m_nShapeRotateState[m_nCurShapeType] == 4)
m_fSmoothRotate--;
if(m_nShapeRotateState[m_nCurShapeType] == 2)
{
if(m_RotateClockwise)
m_fSmoothRotate++;
else
m_fSmoothRotate--;
m_RotateClockwise=!m_RotateClockwise;
}
}
return bResult;
}
float CTetrisControl::OnSmoothRotate(float fSmoothInc)
{
if(m_fSmoothRotate < 0)
{
m_fSmoothRotate += fSmoothInc;
if(m_fSmoothRotate < -1)
m_fSmoothRotate += fSmoothInc;
if(m_fSmoothRotate < -2)
m_fSmoothRotate += fSmoothInc;
if(m_fSmoothRotate > 0)
m_fSmoothRotate = 0;
}
else if(m_fSmoothRotate > 0)
{
m_fSmoothRotate -= fSmoothInc;
if(m_fSmoothRotate < 0)
m_fSmoothRotate = 0;
}
return m_fSmoothRotate;
}
int CTetrisControl::OnDown()
{
int x, y;
for(int i = 0; i < m_CurShape.m_bBlocks; i++)
{
x = m_CurShape.m_ptPos.x + WALL_THICKNESS + m_CurShape.m_ptBlocks[i].x;
y = m_CurShape.m_ptPos.y + WALL_THICKNESS + m_CurShape.m_ptBlocks[i].y + 1;
if(m_Blocks[x][y].m_nStatus == HAVE_BLOCK)
break;
}
if(i < m_CurShape.m_bBlocks) // the shape has been blocked
{
FixCurShape();
m_nCurDelFloors = CalcDeletedFloors();
if(m_nCurDelFloors > 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -