#include
#include
#include
using namespace std;
const int BOARD_HEIGHT = 20; // 游戏区域高度
const int BOARD_WIDTH = 10; // 游戏区域宽度
// 定义方块类型及形状(用二维数组表示方块形状)
const int TYPE_COUNT = 7;
const int BLOCK_SIZE = 4;
int block_type[TYPE_COUNT][BLOCK_SIZE][BLOCK_SIZE] =
{
{ // S 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0}
},
{ // Z 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 1, 0}
},
{ // T 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 0, 0},
{1, 1, 1, 0}
},
{ // L 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 1, 0}
},
{ // J 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 1, 0},
{1, 1, 1, 0}
},
{ // I 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 1}
},
{ // O 形方块
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0}
}
};
// 定义方块颜色
const int COLOR_COUNT = 7;
const int BLOCK_COLOR[COLOR_COUNT] =
{
31, // 红色
32, // 绿色
33, // 黄色
34, // 蓝色
35, // 紫色
36, // 青色
37 // 白色
};
// 定义游戏区域
int board[BOARD_HEIGHT][BOARD_WIDTH];
// 定义当前方块
int cur_block[BLOCK_SIZE][BLOCK_SIZE];
int cur_type, cur_x, cur_y;
// 随机生成新方块
void new_block()
{
cur_type = rand() % TYPE_COUNT;
cur_x = BOARD_WIDTH / 2 - BLOCK_SIZE / 2;
cur_y = 0;
for (int i = 0; i < BLOCK_SIZE; i++)
{
for (int j = 0; j < BLOCK_SIZE; j++)
{
cur_block[i][j] = block_type[cur_type][i][j];
}
}
}
// 判断方块是否可以放置在某个位置
bool check_valid(int x, int y)
{
for (int i = 0; i < BLOCK_SIZE; i++)
{
for (int j = 0; j < BLOCK_SIZE; j++)
{
if (cur_block[i][j] != 0)
{
int xx = i + x;
int yy = j + y;
if (xx < 0 || xx >= BOARD_HEIGHT || yy < 0 || yy >= BOARD_WIDTH || board[xx][yy] != 0)
return false;
}
}
}
return true;
}
// 将当前方块放置在游戏区域中
void put_block()
{
for (int i = 0; i < BLOCK_SIZE; i++)
{
for (int j = 0; j < BLOCK_SIZE; j++)
{
if (cur_block[i][j] != 0)
{
int xx = i + cur_x;
int yy = j + cur_y;
board[xx][yy] = BLOCK_COLOR[cur_type];
}
}
}
}
// 从游戏区域中消除当前方块
void remove_block()
{
for (int i = 0; i < BLOCK_SIZE; i++)
{
for (int j = 0; j < BLOCK_SIZE; j++)
{
if (cur_block[i][j] != 0)
{
int xx = i + cur_x;
int yy = j + cur_y;
board[xx][yy] = 0;
}
}
}
}
// 将游戏区域中满行的方块消除
void remove_filled_rows()
{
for (int i = BOARD_HEIGHT - 1; i >= 0; i--)
{
bool filled = true;
for (int j = 0; j < BOARD_WIDTH; j++)
{
if (board[i][j] == 0)
{
filled = false;
break;
}
}
if (filled)
{
for (int k = i; k >= 1; k--)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
board[k][j] = board[k-1][j];
}
}
for (int j = 0; j < BOARD_WIDTH; j++)
{
board[0][j] = 0;
}
i++;
}
}
}
// 显示游戏区域和当前方块
void display_board()
{
system("clear"); // 清屏
for (int i = 0; i < BOARD_HEIGHT; i++)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
if (board[i][j] == 0)
{
cout << " "; // 空格用于占位
}
else
{
cout << "\033[" << board[i][j] << "m" << "██" << "\033[0m"; // 输出彩色块,"\033[" 和 "m" 用于设置颜色,"\033[0m"用于还原颜色
}
}
cout << endl;
}
}
int main()
{
srand(time(NULL)); // 初始化随机数种子
// 初始化游戏区域
for (int i = 0; i < BOARD_HEIGHT; i++)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
board[i][j] = 0;
}
}
new_block(); // 随机生成新方块
while (true)
{
display_board(); // 显示游戏区域和当前方块
char c;
cin >> c;
if (c == 'a') // 左移
{
if (check_valid(cur_x, cur_y - 1))
{
remove_block();
cur_y--;
put_block();
}
}
else if (c == 'd') // 右移
{
if (check_valid(cur_x, cur_y + 1))
{
remove_block();
cur_y++;
put_block();
}
}
else if (c == 's') // 下移
{
if (check_valid(cur_x + 1, cur_y))
{
remove_block();
cur_x++;
put_block();
}
else
{
put_block();
remove_filled_rows(); // 消除满行的方块
new_block(); // 生成新方块
}
}
else if (c == 'q') // 退出游戏
{
break;
}
}
return 0;
}