分享一个C++写的俄罗斯方块游戏
本帖最后由 Ingran 于 2019-3-25 23:31 编辑分享一下当初学习C++时候写的一个图形化的俄罗斯方块,
部分代码:
void CManage::InitGame()
{
SetGameSpeed();
m_MapDate.InitMap();
m_BlockDate.InitBlock();
m_begTime = timeGetTime() / 1000.0f;
}
void CManage::UpdateGame()
{
//清零,移动,下落(落不动消行),写入新方块
_GetKey();
MyPos const* temp = m_BlockDate.GetBlockHead();
//清零
for (int i = 0; i < BLOCK_NUM; i++)
m_MapDate.SetMapVal((temp + i)->x, (temp + i)->y, 0);
m_endTime = timeGetTime() / 1000.0f;
//移动
switch (key)
{
case 'w':
case 'W':
{
bool isChange = true;
switch (m_BlockDate.GetBlockType())
{
case 0://长条
switch (m_BlockDate.GetBlockChangeType())
{
case 0:
if (m_MapDate.GetMapVal((temp + 0)->x + 1, (temp + 0)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x + 1, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x - 1, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x - 2, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 2)->x - 1, (temp + 2)->y) != 0
|| m_MapDate.GetMapVal((temp + 2)->x - 2, (temp + 2)->y) != 0
|| m_MapDate.GetMapVal((temp + 3)->x - 1, (temp + 3)->y) != 0)
isChange = false;
break;
case 1:
if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 2) != 0
|| m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 1) != 0
|| m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 2) != 0
|| m_MapDate.GetMapVal((temp + 3)->x, (temp + 3)->y + 1) != 0)
isChange = false;
break;
}
break;
case 1://田字
//不变
break;
case 2://Z字
switch (m_BlockDate.GetBlockChangeType())
{
case 0:
if (m_MapDate.GetMapVal((temp + 0)->x + 1, (temp + 0)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x - 1, (temp + 1)->y + 1) != 0
)
isChange = false;
break;
case 1:
if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 2)->x + 1, (temp + 2)->y) != 0
)
isChange = false;
break;
}
break;
case 3://L字
switch (m_BlockDate.GetBlockChangeType())
{
case 0:
if (m_MapDate.GetMapVal((temp + 0)->x + 1, (temp + 0)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 2) != 0
|| m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 1) != 0
|| m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 2) != 0
|| m_MapDate.GetMapVal((temp + 3)->x, (temp + 3)->y + 1) != 0
)
isChange = false;
break;
case 1:
if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y + 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x - 1, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x - 2, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 2)->x - 1, (temp + 2)->y) != 0
|| m_MapDate.GetMapVal((temp + 2)->x - 2, (temp + 2)->y) != 0
|| m_MapDate.GetMapVal((temp + 3)->x - 1, (temp + 3)->y) != 0
)
isChange = false;
break;
case 2:
if (m_MapDate.GetMapVal((temp + 0)->x - 1, (temp + 0)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 2) != 0
|| m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y - 2) != 0
|| m_MapDate.GetMapVal((temp + 3)->x, (temp + 3)->y - 1) != 0
)
isChange = false;
break;
case 3:
if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y - 1) != 0
|| m_MapDate.GetMapVal((temp + 1)->x + 1, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 1)->x + 2, (temp + 1)->y) != 0
|| m_MapDate.GetMapVal((temp + 2)->x + 1, (temp + 2)->y) != 0
|| m_MapDate.GetMapVal((temp + 2)->x + 2, (temp + 2)->y) != 0
|| m_MapDate.GetMapVal((temp + 3)->x + 1, (temp + 3)->y) != 0
)
isChange = false;
break;
}
break;
}
if (isChange)
m_BlockDate.SetBlockChange();
}
key = 0;
break;
case 'a':
case 'A':
if (isMove(0, -1))
m_BlockDate.SetBlockLeft();
key = 0;
break;
case 'd':
case 'D':
if (isMove(0, 1))
m_BlockDate.SetBlockLeft(false);
key = 0;
break;
case 's':
case 'S':
for (int i = 0; i < 2; i++)
{
if (isMove(1, 0))
m_BlockDate.SetBlockDown();
else
break;
}
key = 0;
break;
}
//下落
if (m_endTime - m_begTime >= m_fSpeed)
{
if (isMove(1, 0))
m_BlockDate.SetBlockDown();
else
{
for (int i = 0; i < BLOCK_NUM; i++)
m_MapDate.SetMapVal((temp + i)->x, (temp + i)->y, 2);
//消行
m_MapDate.ClearBlock();
////覆盖之前分数
//setbkmode(OPAQUE);
//settextcolor(BLACK);
//outtextxy(25 * 2, 25 * ROW, " ");
m_BlockDate.InitBlock();
}
m_begTime = m_endTime;
}
//写入新方块
for (int i = 0; i < BLOCK_NUM; i++)
m_MapDate.SetMapVal((temp + i)->x, (temp + i)->y, 2);
}
void CManage::ShowGame()
{
m_MapDate.ShowMap();
}
void CManage::SetGameSpeed()
{
switch (m_Gamelv)
{
case 1:
m_fSpeed = 1.0f;
break;
case 2:
m_fSpeed = 0.6f;
break;
case 3:
m_fSpeed = 0.2f;
break;
}
}
源代码下载:
graphics.h头文件:
看起来很清爽:lol xxdg 发表于 2019-3-25 20:19
错误 4 error C1083: 无法打开包括文件: “graphics.h”: No such file or directory stdafx.h 17 1 C++俄 ...
需要下载easyX图像库 感谢分享,有空学习一下 支持一个
感谢分享, 被置顶了,受宠若惊。{:1_907:} 下载下来学习一下. 感觉蛮精炼的.结构也很清晰. 缥缈的心情 发表于 2019-3-24 22:36
下载下来学习一下. 感觉蛮精炼的.结构也很清晰.
谢谢支持 支持分享精神!