c++俄罗斯方块源代码
时间: 2023-06-27 21:06:11 浏览: 59
以下是一个简单的 C++ 俄罗斯方块游戏源代码,仅供参考:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int Width = 20; // 游戏区域宽度
const int Height = 25; // 游戏区域高度
int score = 0; // 得分
char screen[Height][Width];
// 方块结构体
struct Block
{
int x, y;
char shape[4][4];
};
// 方块数组,共7种不同形状的方块
Block blocks[7] =
{
{ 0, 0, {
{ ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ' },
{ '*', '*', '*', '*' },
{ ' ', ' ', ' ', ' ' }
}
},
{ 0, 0, {
{ '*', '*', ' ', ' ' },
{ '*', '*', ' ', ' ' },
{ ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ' }
}
},
{ 0, 0, {
{ '*', ' ', ' ', ' ' },
{ '*', ' ', ' ', ' ' },
{ '*', '*', ' ', ' ' },
{ ' ', ' ', ' ', ' ' }
}
},
{ 0, 0, {
{ ' ', ' ', '*', ' ' },
{ '*', '*', '*', ' ' },
{ ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ' }
}
},
{ 0, 0, {
{ ' ', '*', '*', ' ' },
{ '*', '*', ' ', ' ' },
{ ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ' }
}
},
{ 0, 0, {
{ '*', '*', ' ', ' ' },
{ ' ', '*', '*', ' ' },
{ ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ' }
}
},
{ 0, 0, {
{ '*', '*', '*', ' ' },
{ ' ', ' ', '*', ' ' },
{ ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ' }
}
}
};
// 随机生成一个新方块
void newBlock(Block& blk)
{
int index = rand() % 7;
blk = blocks[index];
blk.x = Width / 2 - 2;
blk.y = 0;
}
// 将方块画到屏幕上
void drawBlock(const Block& blk)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (blk.shape[i][j] == '*')
{
screen[blk.y + i][blk.x + j] = blk.shape[i][j];
}
}
}
}
// 将屏幕清空
void clearScreen()
{
system("cls");
memset(screen, ' ', sizeof(screen));
for (int i = 0; i < Width; i++)
{
screen[Height - 1][i] = '-';
}
}
// 将屏幕画出来
void renderScreen()
{
cout << "Score: " << score << endl;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
cout << screen[i][j];
}
cout << endl;
}
}
// 判断方块是否可以向下移动
bool canMoveDown(const Block& blk)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (blk.shape[i][j] == '*')
{
if (blk.y + i >= Height - 1 || screen[blk.y + i + 1][blk.x + j] != ' ')
{
return false;
}
}
}
}
return true;
}
// 将方块向下移动一格
void moveDown(Block& blk)
{
blk.y++;
}
// 将方块向左移动一格
void moveLeft(Block& blk)
{
blk.x--;
}
// 将方块向右移动一格
void moveRight(Block& blk)
{
blk.x++;
}
// 将方块旋转90度
void rotate(Block& blk)
{
Block temp = blk;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
blk.shape[j][3 - i] = temp.shape[i][j];
}
}
}
// 将方块固定在屏幕上,更新得分,并检查是否有行已经填满
void fixBlock(const Block& blk)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (blk.shape[i][j] == '*')
{
screen[blk.y + i][blk.x + j] = blk.shape[i][j];
}
}
}
// 检查是否有行已经填满
for (int i = Height - 2; i >= 0; i--)
{
bool full = true;
for (int j = 0; j < Width; j++)
{
if (screen[i][j] == ' ')
{
full = false;
break;
}
}
if (full)
{
score += 10;
// 将上面的所有行向下移动一格
for (int k = i; k > 0; k--)
{
memcpy(screen[k], screen[k - 1], Width);
}
i++;
}
}
}
// 游戏循环
void gameLoop()
{
Block curBlock, nextBlock;
newBlock(curBlock);
newBlock(nextBlock);
while (true)
{
clearScreen();
drawBlock(curBlock);
renderScreen();
if (!canMoveDown(curBlock))
{
fixBlock(curBlock);
curBlock = nextBlock;
newBlock(nextBlock);
}
else
{
moveDown(curBlock);
}
if (_kbhit())
{
char ch = _getch();
switch (ch)
{
case 'a':
if (curBlock.x > 0)
{
moveLeft(curBlock);
}
break;
case 'd':
if (curBlock.x < Width - 4)
{
moveRight(curBlock);
}
break;
case 'w':
rotate(curBlock);
break;
case 's':
while (canMoveDown(curBlock))
{
moveDown(curBlock);
}
fixBlock(curBlock);
curBlock = nextBlock;
newBlock(nextBlock);
break;
}
}
Sleep(50);
}
}
int main()
{
gameLoop();
return 0;
}
```
注意:以上源代码可能存在一些不足或者bug,需要根据实际情况进行修改。