这游戏好久以前写的了,扒出来供初学者参考学习。
本游戏是在Windows平台上,以C++语言为编程基础,实现俄罗斯方块功能。
工具
VS2013
项目框架:
利用Manage管理Block与Map的交互。方块的初始化、旋转等定义成虚函数(多态),Win32 GDI进行渲染 WIN32消息控制…
链接: https://download.csdn.net/download/kikityan/11195549
MyMap.cpp:
#include "stdafx.h"
CMyMap::CMyMap()
{
_zeroMap();
}
CMyMap::~CMyMap()
{
}
void CMyMap::_zeroMap()
{
memset(m_mapArr, 0, sizeof(char)*ROW*COL);
}
void CMyMap::initMap()
{
_zeroMap();
for (int i = 0; i < ROW; i += ROW - 1)
for (int j = 0; j < COL; j++)
{
m_mapArr[i][j] = Map_frame;
}
for (int i = 0; i < COL; i += COL - 1)
for (int j = 0; j < ROW; j++)
{
m_mapArr[j][i] = Map_frame;
}
}
void CMyMap::DrawMap(HDC hdc)
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
switch (m_mapArr[i][j])
{
case Map_NULL:
//cout << " ";
break;
case Map_frame:
//cout << "* ";
{
RECT r = { j*Map_SIZE, i*Map_SIZE, (j + 1)*Map_SIZE, (i + 1)*Map_SIZE };
HBRUSH hb = CreateSolidBrush(0xff1493);
HBRUSH hb1 = CreateSolidBrush(0x787878);
FillRect(hdc,&r,hb);
FrameRect(hdc,&r,hb1);
DeleteObject(hb);
DeleteObject(hb1);
}
break;
case Map_Block:
//cout << "# ";
{
RECT r = { j*Map_SIZE, i*Map_SIZE, (j + 1)*Map_SIZE, (i + 1)*Map_SIZE };
HBRUSH hb = CreateSolidBrush(0xee82ee);
HBRUSH hb1 = CreateSolidBrush(0xff00ff);
FillRect(hdc, &r, hb);
FrameRect(hdc, &r, hb1);
DeleteObject(hb);
DeleteObject(hb1);
}
break;
}
//printf("\n");
}
}
void CMyMap::SetMapVal(int row, int col, int val)
{
m_mapArr[row][col] = val;
}
int CMyMap::clearNum()
{
int num = 0;
for (int i = ROW-2; i >=4; i--)
{
bool isclear = true;
for (int j = 1; j < COL - 1; j++)
{
if (m_mapArr[i][j]==Map_NULL)
{
isclear = false;
break;
}
}
if (isclear)
{
num++;//score
for (int m = i; m >=4; --m)
{
for (int n = 1; n < COL - 1; ++n)
m_mapArr[m][n] = m_mapArr[m - 1][n];
}
++i;
}
}
return num;
}
Block.cpp:
#include "stdafx.h"
MyBlock::MyBlock()
{
memset(m_blockArr, 0, sizeof(M_Point)*Block_NUM);
m_BlockType = 0;
}
MyBlock::~MyBlock()
{
}
void MyBlock::initBlock()
{
}
void MyBlock::ChangeBlock()
{
}
bool MyBlock::_isChange(Parr p)
{
return true;
}
void MyBlock::setBlockDown()
{
for (int i = 0; i < Block_NUM; i++)
{
m_blockArr[i].row++;
}
}
void MyBlock::setBlockMove(bool left)
{
if (left)//减少运算量
{
for (int i = 0; i < Block_NUM; i++)
m_blockArr[i].col--;
}
else
{
for (int i = 0; i < Block_NUM; i++)
m_blockArr[i].col++;
}
}
void _6::initBlock()
{
m_BlockType = rand() % 2;
switch (m_BlockType)
{
case 0:
m_blockArr[0].row = 2;
m_blockArr[0].col = 5;
m_blockArr[1].row = 2;
m_blockArr[1].col = 6;
/*m_blockArr[2].row = NULL;
m_blockArr[2].col = NULL;
m_blockArr[3].row = NULL;
m_blockArr[3].col = NULL;*/
break;
case 1:
m_blockArr[0].row = 2;
m_blockArr[0].col = 6;
m_blockArr[1].row = 1;
m_blockArr[1].col = 6;
/*m_blockArr[2].row = NULL;
m_blockArr[2].col = NULL;
m_blockArr[3].row = NULL;
m_blockArr[3].col = NULL;*/
break;
}
}
void _6::ChangeBlock()
{
switch (m_BlockType)
{
case 0:
m_blockArr[1].row = m_blockArr[0].row - 1;
m_blockArr[1].col = m_blockArr[0].col;
m_BlockType = 1;
break;
case 1:
m_blockArr[1].row = m_blockArr[0].row;
m_blockA