测试系统 :Ubuntu~22.04
效果预览
①安装库:
这里只说下Ubuntu的,其他系统可以自己编译源码或者安装。
源码位置:http://ftp.gnu.org/pub/gnu/ncurses/
sudo apt-get install libncurses5-dev libncursesw5-dev -y
②代码
#include <ncurses.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <unistd.h>
using namespace std;
// 方块形状
vector<vector<vector<int>>> tetrominoes = {
{
{
1, 1, 1, 1}}, // I
{
{
1, 1}, {
1, 1}}, // O
{
{
1, 1, 0}, {
0, 1, 1}}, // Z
{
{
0, 1, 1}, {
1, 1, 0}}, // S
{
{
1, 1, 1}, {
0, 1, 0}}, // T
{
{
1, 1, 1}, {
1, 0, 0}}, // L
{
{
1, 1, 1}, {
0, 0, 1}} // J
};
int width = 10, height = 20;
int score = 0;
bool game_over = false;
int delay_us = 50000; // 每次循环的延迟(微秒)
// 游戏界面
vector<vector<int>> board(height, vector<int>(width, 0));
// 初始化颜色对
void initColors() {
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLACK); // 白色方块
init_pair(2, COLOR_CYAN, COLOR_BLACK); // 预测落点的浅蓝色影子
}
// 检查方块是否可以放置
bool checkPosition(const vector<vector<int>>& tetromino, int x, int y) {
for (int i = 0; i < tetromino.size(); ++i) {
for (int j = 0; j < tetromino[i].size(); ++j) {
if (tetromino[i][j]) {
int newX = x + j;
int newY = y + i;