#include
#include
#include
#include
#include
#include
#include
#include
#define ROW 19 // 游戏区域的行数
#define COL 12 // 游戏区域的列数
#define NR 4 // 方块数据大小
#define TYPE 7 // 方块类型数
/* 按键枚举 */
enum key {
DOWN, // 上
LEFT, // 左
RIGHT, // 右
CHANGE, // 变化
STOP, // 停止
EXIT, // 退出
UNKNOW, // 未知
};
/***** 函数声明区域 ******/
void initalGameArea(void); // 初始化游戏区域
void drawBlock(char bl[NR][NR]); // 画方块
void cleanBlock(char bl[NR][NR]); // 清除方块
void turnBlock(char bl[NR][NR]); // 旋转方块
void gameEnd(void); // 结束游戏
void gameStop(void); // 暂停游戏
void showGame(void); // 显示游戏
void gameSelf(int signo); // 游戏自动运行
void checkDeleteLine(void); // 检查是否满一行
void checkGameOver(char bl[NR][NR]); // 检查是否游戏结束
int checkMove(char bl[NR][NR], int flag); // 检查方块是否可移动
void setBufferedInput(bool enable); // 获取输入
/* 全局变量区域 */
static char gameArea[ROW][COL] = {0}; // 游戏区域数据
static int startX = 4, startY = 6; // 方块出现的起始位置
static int type = 0; // 方块当前类型
static int nextType = 0; // 方块的下一种类型
static int diret = 0; // 方块的方向
char *state = "\033[32m游戏中...\033[0m"; // 游戏运行状态
static unsigned int level = 0; // 游戏等级
static unsigned int score = 0; // 游戏分数
static unsigned int maxScore = 0; // 游戏最高记录
static FILE *fp = NULL; // 用于把记录保存到文件
/* 方块数据 */
char bl[TYPE][NR][NR][NR] = {
/* 第一种方块 */
{
{ /* 第一种方向 */
{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第二种方向 */
{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第三种方向 */
{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第四种方向 */
{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
},
},
/* 第二种方块 */
{
{ /* 第一种方向 */
{2,2,2,2},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第二种方向 */
{0,2,0,0},
{0,2,0,0},
{0,2,0,0},
{0,2,0,0},
},
{ /* 第三种方向 */
{2,2,2,2},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第四种方向 */
{0,2,0,0},
{0,2,0,0},
{0,2,0,0},
{0,2,0,0},
},
},
/* 第三种方块 */
{
{ /* 第一种方向 */
{3,0,0,0},
{3,3,3,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第二种方向 */
{0,3,3,0},
{0,3,0,0},
{0,3,0,0},
{0,0,0,0},
},
{ /* 第三种方向 */
{3,3,3,0},
{0,0,3,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第四种方向 */
{0,0,3,0},
{0,0,3,0},
{0,3,3,0},
{0,0,0,0},
},
},
/* 第四种方块 */
{
{ /* 第一种方向 */
{0,0,4,0},
{4,4,4,0},
{0,0,0,0},
{0,0,0,0},
},
{ /* 第二种方向 */
{0,4,0,0},
{0,4,0,0},
{0,4,4,0},
{0,0,0,0},
},
{ /* 第三种方向 */
{0,4,4,4},
{0,4,0,0},
{0,0,0,0},
{0,0,0,0}